Skip to content

Commit

Permalink
Replace t.Fatal with require.NoError in tests (#1213)
Browse files Browse the repository at this point in the history
We are using the following pattern in tests that can be
replaced with `require.NoError(t, err)`.
```go
if err != nil {
    t.Fatal(err)
}
```
  • Loading branch information
Ibrahim Jarif authored Feb 10, 2020
1 parent c51748e commit 78d405a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 85 deletions.
98 changes: 38 additions & 60 deletions backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,17 @@ func TestBackupRestore1(t *testing.T) {

func TestBackupRestore2(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "badger-test")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

defer removeDir(tmpdir)

s1Path := filepath.Join(tmpdir, "test1")
s2Path := filepath.Join(tmpdir, "test2")
s3Path := filepath.Join(tmpdir, "test3")

db1, err := Open(getTestOptions(s1Path))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

defer db1.Close()
key1 := []byte("key1")
key2 := []byte("key2")
Expand All @@ -140,36 +138,30 @@ func TestBackupRestore2(t *testing.T) {
}
return tx.SetEntry(NewEntry(key2, rawValue))
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

for i := byte(1); i < N; i++ {
err = db1.Update(func(tx *Txn) error {
if err := tx.SetEntry(NewEntry(append(key1, i), rawValue)); err != nil {
return err
}
return tx.SetEntry(NewEntry(append(key2, i), rawValue))
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

}
var backup bytes.Buffer
_, err = db1.Backup(&backup, 0)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

fmt.Println("backup1 length:", backup.Len())

db2, err := Open(getTestOptions(s2Path))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

defer db2.Close()
err = db2.Load(&backup, 16)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

for i := byte(1); i < N; i++ {
err = db2.View(func(tx *Txn) error {
Expand All @@ -190,9 +182,8 @@ func TestBackupRestore2(t *testing.T) {
}
return nil
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

}

for i := byte(1); i < N; i++ {
Expand All @@ -202,27 +193,22 @@ func TestBackupRestore2(t *testing.T) {
}
return tx.SetEntry(NewEntry(append(key2, i), rawValue))
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

}

backup.Reset()
_, err = db2.Backup(&backup, 0)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

fmt.Println("backup2 length:", backup.Len())
db3, err := Open(getTestOptions(s3Path))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

defer db3.Close()

err = db3.Load(&backup, 16)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

for i := byte(1); i < N; i++ {
err = db3.View(func(tx *Txn) error {
Expand All @@ -243,9 +229,8 @@ func TestBackupRestore2(t *testing.T) {
}
return nil
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

}

}
Expand Down Expand Up @@ -313,9 +298,8 @@ func TestBackup(t *testing.T) {
}
t.Run("disk mode", func(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "badger-test")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

defer removeDir(tmpdir)
opt := DefaultOptions(filepath.Join(tmpdir, "backup0"))
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
Expand All @@ -334,9 +318,8 @@ 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)
}
require.NoError(t, err)

defer removeDir(tmpdir)

N := 1000
Expand All @@ -345,9 +328,8 @@ func TestBackupRestore3(t *testing.T) {
// backup
{
db1, err := Open(DefaultOptions(filepath.Join(tmpdir, "backup1")))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

defer db1.Close()
require.NoError(t, populateEntries(db1, entries))

Expand All @@ -360,9 +342,8 @@ func TestBackupRestore3(t *testing.T) {

// restore
db2, err := Open(DefaultOptions(filepath.Join(tmpdir, "restore1")))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

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

Expand Down Expand Up @@ -393,9 +374,8 @@ func TestBackupRestore3(t *testing.T) {

func TestBackupLoadIncremental(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "badger-test")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

defer removeDir(tmpdir)

N := 100
Expand All @@ -406,9 +386,8 @@ func TestBackupLoadIncremental(t *testing.T) {
// backup
{
db1, err := Open(DefaultOptions(filepath.Join(tmpdir, "backup2")))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

defer db1.Close()

require.NoError(t, populateEntries(db1, entries))
Expand Down Expand Up @@ -467,9 +446,8 @@ func TestBackupLoadIncremental(t *testing.T) {

// restore
db2, err := Open(getTestOptions(filepath.Join(tmpdir, "restore2")))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

defer db2.Close()

require.NoError(t, db2.Load(&bb, 16))
Expand Down
7 changes: 1 addition & 6 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1567,9 +1567,6 @@ func TestLSMOnly(t *testing.T) {
opts.ValueLogMaxEntries = 100
db, err := Open(opts)
require.NoError(t, err)
if err != nil {
t.Fatal(err)
}

value := make([]byte, 128)
_, err = rand.Read(value)
Expand All @@ -1581,9 +1578,7 @@ func TestLSMOnly(t *testing.T) {

db, err = Open(opts)
require.NoError(t, err)
if err != nil {
t.Fatal(err)
}

defer db.Close()
require.NoError(t, db.RunValueLogGC(0.2))
}
Expand Down
27 changes: 8 additions & 19 deletions txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,9 +837,8 @@ func TestManagedDB(t *testing.T) {

func TestArmV7Issue311Fix(t *testing.T) {
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

defer removeDir(dir)

db, err := Open(DefaultOptions(dir).
Expand All @@ -848,31 +847,21 @@ func TestArmV7Issue311Fix(t *testing.T) {
WithLevelOneSize(8 << 20).
WithMaxTableSize(2 << 20).
WithSyncWrites(false))
if err != nil {
t.Fatalf("cannot open db at location %s: %v", dir, err)
}

require.NoError(t, err)

err = db.View(func(txn *Txn) error { return nil })
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

err = db.Update(func(txn *Txn) error {
return txn.SetEntry(NewEntry([]byte{0x11}, []byte{0x22}))
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

err = db.Update(func(txn *Txn) error {
return txn.SetEntry(NewEntry([]byte{0x11}, []byte{0x22}))
})

if err != nil {
t.Fatal(err)
}

if err = db.Close(); err != nil {
t.Fatal(err)
}
require.NoError(t, err)
require.NoError(t, db.Close())
}

0 comments on commit 78d405a

Please sign in to comment.