From 337741015cc46179fa8886079a416bb2fe4f4845 Mon Sep 17 00:00:00 2001 From: siddharth Date: Sun, 28 Nov 2021 17:03:18 -0500 Subject: db: update FernDB Add `mutex` field to the FernDB struct. --- db/db.go | 4 ++++ db/db_test.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) (limited to 'db') diff --git a/db/db.go b/db/db.go index 2d6a4d5..6bc6d04 100644 --- a/db/db.go +++ b/db/db.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "path" + "sync" "ricketyspace.net/fern/file" ) @@ -15,6 +16,7 @@ import ( var dbPath string type FernDB struct { + mutex *sync.Mutex // For writes to `downloaded` // Key: feed-id // Value: feed-id's entries that were downloaded downloaded map[string][]string @@ -42,6 +44,7 @@ func Open() (*FernDB, error) { if err != nil { // db does not exist yet; create an empty one. db := new(FernDB) + db.mutex = new(sync.Mutex) db.downloaded = make(map[string][]string) return db, nil } @@ -58,6 +61,7 @@ func Open() (*FernDB, error) { // Unmarshal db into an object. db := new(FernDB) + db.mutex = new(sync.Mutex) err = json.Unmarshal(bs, &db.downloaded) if err != nil { return nil, err diff --git a/db/db_test.go b/db/db_test.go index 5a2a983..e2308c1 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -46,6 +46,14 @@ func TestOpenNewDB(t *testing.T) { return } + // Verify that 'mutex' is initialized. + if db.mutex == nil { + t.Errorf("db.mutex is nil") + return + } + db.mutex.Lock() + db.mutex.Unlock() + // Verify that 'downloaded' is initialized if db.downloaded == nil { t.Errorf("db.downloaded is nil") @@ -77,6 +85,14 @@ func TestOpenExistingDB(t *testing.T) { return } + // Verify that 'mutex' is initialized. + if db.mutex == nil { + t.Errorf("db.mutex is nil") + return + } + db.mutex.Lock() + db.mutex.Unlock() + // Validate db.downloaded. var entries, expectedEntries []string var ok bool -- cgit v1.2.3