From a6e5c6a2cc020fe50649b23f854d03d8f5fadcc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jano=C5=A1=20Gulja=C5=A1?= Date: Fri, 8 Mar 2019 05:59:59 +0100 Subject: [PATCH] swarm/storage/localstore: fix synchronization in TestDB_gcSize (#19235) --- swarm/storage/localstore/gc_test.go | 7 ++----- swarm/storage/localstore/localstore.go | 12 ++++++++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/swarm/storage/localstore/gc_test.go b/swarm/storage/localstore/gc_test.go index c2ad774d3..8ed34384d 100644 --- a/swarm/storage/localstore/gc_test.go +++ b/swarm/storage/localstore/gc_test.go @@ -289,12 +289,9 @@ func TestDB_gcSize(t *testing.T) { } // DB.Close writes gc size to disk, so - // Instead calling Close, simulate database shutdown + // Instead calling Close, close the database // without it. - close(db.close) - db.updateGCWG.Wait() - err = db.shed.Close() - if err != nil { + if err := db.closeWithOptions(false); err != nil { t.Fatal(err) } diff --git a/swarm/storage/localstore/localstore.go b/swarm/storage/localstore/localstore.go index a66130fd3..35044115b 100644 --- a/swarm/storage/localstore/localstore.go +++ b/swarm/storage/localstore/localstore.go @@ -367,6 +367,12 @@ func New(path string, baseKey []byte, o *Options) (db *DB, err error) { // Close closes the underlying database. func (db *DB) Close() (err error) { + return db.closeWithOptions(true) +} + +// closeWithOptions provides a more control which part of closing +// is done for tests. +func (db *DB) closeWithOptions(writeGCSize bool) (err error) { close(db.close) db.updateGCWG.Wait() @@ -384,8 +390,10 @@ func (db *DB) Close() (err error) { log.Error("localstore: write gc size worker did not return after db close") } - if err := db.writeGCSize(db.getGCSize()); err != nil { - log.Error("localstore: write gc size", "err", err) + if writeGCSize { + if err := db.writeGCSize(db.getGCSize()); err != nil { + log.Error("localstore: write gc size", "err", err) + } } return db.shed.Close() }