Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Fix windows build (#1177)
Browse files Browse the repository at this point in the history
Fix windows build and some deepsource warnings
  • Loading branch information
Ibrahim Jarif authored Jan 7, 2020
1 parent 64bd25f commit 711e093
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 30 deletions.
10 changes: 8 additions & 2 deletions badger/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -330,7 +333,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)
Expand All @@ -346,7 +348,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)
Expand All @@ -361,6 +363,7 @@ func TestBackupRestore3(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer db2.Close()
require.NoError(t, db2.Load(&bb, 16))

// verify
Expand Down Expand Up @@ -406,6 +409,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)
Expand Down Expand Up @@ -466,6 +470,8 @@ func TestBackupLoadIncremental(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer db2.Close()

require.NoError(t, db2.Load(&bb, 16))

// verify
Expand Down
35 changes: 26 additions & 9 deletions badger/db2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,22 +627,29 @@ func TestL0GCBug(t *testing.T) {
return []byte(fmt.Sprintf("%10d", i))
}
val := []byte{1, 1, 1, 1, 1, 1, 1, 1}
// Insert 100 entries. This will create about 50 vlog files and 2 SST files.
for i := 0; i < 100; i++ {
err = db1.Update(func(txn *Txn) error {
return txn.SetEntry(NewEntry(key(i), val))
})
require.NoError(t, err)
// Insert 100 entries. This will create about 50*3 vlog files and 6 SST files.
for i := 0; i < 3; i++ {
for j := 0; j < 100; j++ {
err = db1.Update(func(txn *Txn) error {
return txn.SetEntry(NewEntry(key(j), val))
})
require.NoError(t, err)
}
}
// Run value log GC multiple times. This would ensure at least
// one value log file is garbage collected.
success := 0
for i := 0; i < 10; i++ {
err := db1.RunValueLogGC(0.01)
if err == nil {
success++
}
if err != nil && err != ErrNoRewrite {
t.Fatalf(err.Error())
}
}

// Ensure alteast one GC call was successful.
require.NotZero(t, success)
// CheckKeys reads all the keys previously stored.
checkKeys := func(db *DB) {
for i := 0; i < 100; i++ {
Expand All @@ -665,7 +672,12 @@ func TestL0GCBug(t *testing.T) {
if db1.valueDirGuard != nil {
require.NoError(t, db1.valueDirGuard.release())
}
require.NoError(t, db1.vlog.Close())
for _, f := range db1.vlog.filesMap {
require.NoError(t, f.fd.Close())
}
require.NoError(t, db1.registry.Close())
require.NoError(t, db1.lc.close())
require.NoError(t, db1.manifest.close())

db2, err := Open(opts)
require.NoError(t, err)
Expand Down Expand Up @@ -723,7 +735,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 {
Expand All @@ -735,6 +746,12 @@ 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.registry.Close())
require.NoError(t, db.manifest.close())
require.NoError(t, db.lc.close())

fmt.Println()
fmt.Println("Third DB Open")
Expand Down
39 changes: 21 additions & 18 deletions badger/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,9 @@ func TestExpiryImproperDBClose(t *testing.T) {
// it would return Truncate Required Error.
require.NoError(t, db0.vlog.Close())

require.NoError(t, db0.registry.Close())
require.NoError(t, db0.manifest.close())

db1, err := Open(opt)
require.NoError(t, err)
err = db1.View(func(txn *Txn) error {
Expand Down Expand Up @@ -1200,7 +1203,7 @@ 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]
}
Expand Down Expand Up @@ -1670,12 +1673,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()

Expand All @@ -1687,17 +1690,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 {
Expand All @@ -1714,7 +1717,7 @@ func ExampleOpen() {
})

if err != nil {
log.Fatal(err)
panic(err)
}

// Output:
Expand All @@ -1725,13 +1728,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()

Expand All @@ -1749,13 +1752,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
Expand All @@ -1772,7 +1775,7 @@ func ExampleTxn_NewIterator() {
return nil
})
if err != nil {
log.Fatal(err)
panic(err)
}
fmt.Printf("Counted %d elements", count)
// Output:
Expand Down Expand Up @@ -1957,7 +1960,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())
Expand All @@ -1977,12 +1980,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()

Expand All @@ -2002,7 +2005,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")
}()
Expand All @@ -2012,11 +2015,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")
Expand Down
1 change: 1 addition & 0 deletions badger/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,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)
Expand Down
2 changes: 1 addition & 1 deletion badger/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (lf *logFile) encryptionEnabled() bool {
}

func (lf *logFile) munmap() (err error) {
if lf.loadingMode != options.MemoryMap {
if lf.loadingMode != options.MemoryMap || len(lf.fmap) == 0 {
// Nothing to do
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions badger/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,9 @@ 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())
require.NoError(t, db0.registry.Close())

opt.Truncate = true
db1, err := Open(opt)
Expand All @@ -799,7 +802,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++
Expand Down

0 comments on commit 711e093

Please sign in to comment.