From b0941d462afcbd4cc23254fa96b16658bc090b44 Mon Sep 17 00:00:00 2001 From: Ibrahim Jarif Date: Tue, 7 Jan 2020 18:04:02 +0530 Subject: [PATCH] Fix windows build (#1177) Fix windows build and some deepsource warnings (cherry picked from commit 0f2e6297a34efdb07001eba8531ed85e8b42189e) --- backup_test.go | 10 ++++++++-- db2_test.go | 6 +++++- db_test.go | 45 ++++++++++++++++++++++++++------------------- manifest_test.go | 1 + value.go | 2 +- value_test.go | 4 ++++ 6 files changed, 45 insertions(+), 23 deletions(-) diff --git a/backup_test.go b/backup_test.go index d2fd83972..47554caab 100644 --- a/backup_test.go +++ b/backup_test.go @@ -129,6 +129,7 @@ func TestBackupRestore2(t *testing.T) { if err != nil { t.Fatal(err) } + defer db1.Close() key1 := []byte("key1") key2 := []byte("key2") rawValue := []byte("NotLongValue") @@ -164,6 +165,7 @@ func TestBackupRestore2(t *testing.T) { if err != nil { t.Fatal(err) } + defer db2.Close() err = db2.Load(&backup, 16) if err != nil { t.Fatal(err) @@ -215,6 +217,7 @@ func TestBackupRestore2(t *testing.T) { if err != nil { t.Fatal(err) } + defer db3.Close() err = db3.Load(&backup, 16) if err != nil { @@ -322,7 +325,6 @@ func TestBackup(t *testing.T) { func TestBackupRestore3(t *testing.T) { var bb bytes.Buffer - tmpdir, err := ioutil.TempDir("", "badger-test") if err != nil { t.Fatal(err) @@ -338,7 +340,7 @@ func TestBackupRestore3(t *testing.T) { if err != nil { t.Fatal(err) } - + defer db1.Close() require.NoError(t, populateEntries(db1, entries)) _, err = db1.Backup(&bb, 0) @@ -353,6 +355,7 @@ func TestBackupRestore3(t *testing.T) { if err != nil { t.Fatal(err) } + defer db2.Close() require.NoError(t, db2.Load(&bb, 16)) // verify @@ -398,6 +401,7 @@ func TestBackupLoadIncremental(t *testing.T) { if err != nil { t.Fatal(err) } + defer db1.Close() require.NoError(t, populateEntries(db1, entries)) since, err := db1.Backup(&bb, 0) @@ -458,6 +462,8 @@ func TestBackupLoadIncremental(t *testing.T) { if err != nil { t.Fatal(err) } + defer db2.Close() + require.NoError(t, db2.Load(&bb, 16)) // verify diff --git a/db2_test.go b/db2_test.go index a43dfc8ef..805a6426a 100644 --- a/db2_test.go +++ b/db2_test.go @@ -565,7 +565,6 @@ func TestWindowsDataLoss(t *testing.T) { opt.Truncate = true db, err = Open(opt) require.NoError(t, err) - // Return after reading one entry. We're simulating a crash. // Simulate a crash by not closing db but releasing the locks. if db.dirLockGuard != nil { @@ -577,6 +576,11 @@ func TestWindowsDataLoss(t *testing.T) { // Don't use vlog.Close here. We don't want to fix the file size. Only un-mmap // the data so that we can truncate the file durning the next vlog.Open. require.NoError(t, y.Munmap(db.vlog.filesMap[db.vlog.maxFid].fmap)) + for _, f := range db.vlog.filesMap { + require.NoError(t, f.fd.Close()) + } + require.NoError(t, db.manifest.close()) + require.NoError(t, db.lc.close()) fmt.Println() fmt.Println("Third DB Open") diff --git a/db_test.go b/db_test.go index d7dde562f..c19eadd33 100644 --- a/db_test.go +++ b/db_test.go @@ -1089,13 +1089,19 @@ func TestExpiryImproperDBClose(t *testing.T) { }) require.NoError(t, err) - // Simulate a crash by not closing db0, but releasing the locks. + // Simulate a crash by not closing db0, but releasing the locks. if db0.dirLockGuard != nil { require.NoError(t, db0.dirLockGuard.release()) } if db0.valueDirGuard != nil { require.NoError(t, db0.valueDirGuard.release()) } + // We need to close vlog to fix the vlog file size. On windows, the vlog file + // is truncated to 2*MaxVlogSize and if we don't close the vlog file, reopening + // it would return Truncate Required Error. + require.NoError(t, db0.vlog.Close()) + + require.NoError(t, db0.manifest.close()) db1, err := Open(opt) require.NoError(t, err) @@ -1108,13 +1114,14 @@ func TestExpiryImproperDBClose(t *testing.T) { }) require.NoError(t, err) require.NoError(t, db1.Close()) + } func randBytes(n int) []byte { recv := make([]byte, n) in, err := rand.Read(recv) if err != nil { - log.Fatal(err) + panic(err) } return recv[:in] } @@ -1555,12 +1562,12 @@ func TestGoroutineLeak(t *testing.T) { func ExampleOpen() { dir, err := ioutil.TempDir("", "badger-test") if err != nil { - log.Fatal(err) + panic(err) } defer removeDir(dir) db, err := Open(DefaultOptions(dir)) if err != nil { - log.Fatal(err) + panic(err) } defer db.Close() @@ -1572,17 +1579,17 @@ func ExampleOpen() { }) if err != nil { - log.Fatal(err) + panic(err) } txn := db.NewTransaction(true) // Read-write txn err = txn.SetEntry(NewEntry([]byte("key"), []byte("value"))) if err != nil { - log.Fatal(err) + panic(err) } err = txn.Commit() if err != nil { - log.Fatal(err) + panic(err) } err = db.View(func(txn *Txn) error { @@ -1599,7 +1606,7 @@ func ExampleOpen() { }) if err != nil { - log.Fatal(err) + panic(err) } // Output: @@ -1610,13 +1617,13 @@ func ExampleOpen() { func ExampleTxn_NewIterator() { dir, err := ioutil.TempDir("", "badger-test") if err != nil { - log.Fatal(err) + panic(err) } defer removeDir(dir) db, err := Open(DefaultOptions(dir)) if err != nil { - log.Fatal(err) + panic(err) } defer db.Close() @@ -1634,13 +1641,13 @@ func ExampleTxn_NewIterator() { for i := 0; i < n; i++ { err := txn.SetEntry(NewEntry(bkey(i), bval(i))) if err != nil { - log.Fatal(err) + panic(err) } } err = txn.Commit() if err != nil { - log.Fatal(err) + panic(err) } opt := DefaultIteratorOptions @@ -1657,7 +1664,7 @@ func ExampleTxn_NewIterator() { return nil }) if err != nil { - log.Fatal(err) + panic(err) } fmt.Printf("Counted %d elements", count) // Output: @@ -1794,7 +1801,7 @@ func TestMain(m *testing.M) { // call flag.Parse() here if TestMain uses flags go func() { if err := http.ListenAndServe("localhost:8080", nil); err != nil { - log.Fatalf("Unable to open http port at 8080") + panic("Unable to open http port at 8080") } }() os.Exit(m.Run()) @@ -1814,12 +1821,12 @@ func ExampleDB_Subscribe() { // Open the DB. dir, err := ioutil.TempDir("", "badger-test") if err != nil { - log.Fatal(err) + panic(err) } defer removeDir(dir) db, err := Open(DefaultOptions(dir)) if err != nil { - log.Fatal(err) + panic(err) } defer db.Close() @@ -1839,7 +1846,7 @@ func ExampleDB_Subscribe() { return nil } if err := db.Subscribe(ctx, cb, prefix); err != nil && err != context.Canceled { - log.Fatal(err) + panic(err) } log.Printf("subscription closed") }() @@ -1849,11 +1856,11 @@ func ExampleDB_Subscribe() { // Write both keys, but only one should be printed in the Output. err = db.Update(func(txn *Txn) error { return txn.Set(aKey, aValue) }) if err != nil { - log.Fatal(err) + panic(err) } err = db.Update(func(txn *Txn) error { return txn.Set(bKey, bValue) }) if err != nil { - log.Fatal(err) + panic(err) } log.Printf("stopping subscription") diff --git a/manifest_test.go b/manifest_test.go index f682afe6c..d19592e5f 100644 --- a/manifest_test.go +++ b/manifest_test.go @@ -161,6 +161,7 @@ func TestOverlappingKeyRangeError(t *testing.T) { defer removeDir(dir) kv, err := Open(DefaultOptions(dir)) require.NoError(t, err) + defer kv.Close() lh0 := newLevelHandler(kv, 0) lh1 := newLevelHandler(kv, 1) diff --git a/value.go b/value.go index 707f8e7ff..d93d8b797 100644 --- a/value.go +++ b/value.go @@ -87,7 +87,7 @@ func (lf *logFile) mmap(size int64) (err error) { } func (lf *logFile) munmap() (err error) { - if lf.loadingMode != options.MemoryMap || len(lf.fmap)== 0{ + if lf.loadingMode != options.MemoryMap || len(lf.fmap) == 0 { // Nothing to do return nil } diff --git a/value_test.go b/value_test.go index fd1423d14..e190ff97e 100644 --- a/value_test.go +++ b/value_test.go @@ -774,6 +774,8 @@ func TestPenultimateLogCorruption(t *testing.T) { if db0.valueDirGuard != nil { require.NoError(t, db0.valueDirGuard.release()) } + require.NoError(t, db0.vlog.Close()) + require.NoError(t, db0.manifest.close()) opt.Truncate = true db1, err := Open(opt) @@ -793,7 +795,9 @@ func TestPenultimateLogCorruption(t *testing.T) { func checkKeys(t *testing.T, kv *DB, keys [][]byte) { i := 0 txn := kv.NewTransaction(false) + defer txn.Discard() iter := txn.NewIterator(IteratorOptions{}) + defer iter.Close() for iter.Seek(keys[0]); iter.Valid(); iter.Next() { require.Equal(t, iter.Item().Key(), keys[i]) i++