Skip to content

Commit

Permalink
Fix windows build (#1177)
Browse files Browse the repository at this point in the history
Fix windows build and some deepsource warnings

(cherry picked from commit 0f2e629)
  • Loading branch information
Ibrahim Jarif committed Mar 12, 2020
1 parent 22ba474 commit b0941d4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
10 changes: 8 additions & 2 deletions 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 @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion db2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
45 changes: 26 additions & 19 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
}
Expand Down Expand Up @@ -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()

Expand All @@ -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 {
Expand All @@ -1599,7 +1606,7 @@ func ExampleOpen() {
})

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

// Output:
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -1657,7 +1664,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 @@ -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())
Expand All @@ -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()

Expand All @@ -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")
}()
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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++
Expand Down

0 comments on commit b0941d4

Please sign in to comment.