Skip to content

Commit 4f4f761

Browse files
committed
test(badgerd): expand options, dns adapters, and item coverage
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
1 parent d74e502 commit 4f4f761

5 files changed

Lines changed: 170 additions & 0 deletions

File tree

internal/badgerd/db_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package badgerd
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestOpenInMemoryAndCloseIdempotent(t *testing.T) {
12+
dbi, err := Open(context.Background(), WithInMemory(true))
13+
require.NoError(t, err)
14+
db := dbi.(*db)
15+
16+
assert.True(t, db.InMemory())
17+
assert.Equal(t, "", db.Path())
18+
assert.False(t, db.Replicated())
19+
20+
require.NoError(t, db.Close())
21+
require.NoError(t, db.Close())
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package godns
2+
3+
import (
4+
"net"
5+
"testing"
6+
7+
"github.com/pkg/errors"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestResolverIsNotFoundTable(t *testing.T) {
12+
r := &Resolver{}
13+
14+
assert.False(t, r.IsNotFound(nil))
15+
assert.True(t, r.IsNotFound(&net.DNSError{IsNotFound: true}))
16+
assert.False(t, r.IsNotFound(&net.DNSError{IsNotFound: false}))
17+
assert.True(t, r.IsNotFound(errors.Wrap(&net.DNSError{IsNotFound: true}, "wrapped")))
18+
assert.False(t, r.IsNotFound(errors.New("other")))
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package miekgdns
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pkg/errors"
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestResolverLookupRecursionLimitTable(t *testing.T) {
12+
r := &Resolver{}
13+
14+
_, _, err := r.lookupSRV("", "", "example.org", 9, 8)
15+
require.Error(t, err)
16+
assert.Contains(t, err.Error(), "maximum number of recursive iterations reached")
17+
18+
_, err = r.lookupIPAddr("example.org", 9, 8)
19+
require.Error(t, err)
20+
assert.Contains(t, err.Error(), "maximum number of recursive iterations reached")
21+
}
22+
23+
func TestResolverIsNotFoundTable(t *testing.T) {
24+
r := &Resolver{}
25+
26+
assert.False(t, r.IsNotFound(nil))
27+
assert.True(t, r.IsNotFound(ErrNoSuchHost))
28+
assert.True(t, r.IsNotFound(errors.Wrap(ErrNoSuchHost, "wrapped")))
29+
assert.False(t, r.IsNotFound(errors.New("other")))
30+
}
31+
32+
func TestFmtErrsJoinsAllErrors(t *testing.T) {
33+
got := fmtErrs([]error{errors.New("a"), errors.New("b")})
34+
assert.Equal(t, ";a;b", got)
35+
}

internal/badgerd/options_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package badgerd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/dgraph-io/badger/v3"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type testLogger struct{}
11+
12+
func (testLogger) Errorf(string, ...any) {}
13+
func (testLogger) Warningf(string, ...any) {}
14+
func (testLogger) Infof(string, ...any) {}
15+
func (testLogger) Debugf(string, ...any) {}
16+
func (testLogger) Tracef(string, ...any) {}
17+
18+
func TestWithPathTable(t *testing.T) {
19+
o := options{}
20+
WithPath(":memory:")(&o)
21+
assert.True(t, o.inMemory)
22+
assert.Equal(t, "", o.path)
23+
24+
o = options{}
25+
WithPath("/tmp/pdb")(&o)
26+
assert.False(t, o.inMemory)
27+
assert.Equal(t, "/tmp/pdb", o.path)
28+
}
29+
30+
func TestWithInMemoryTable(t *testing.T) {
31+
o := options{path: "/tmp/pdb"}
32+
WithInMemory(true)(&o)
33+
assert.True(t, o.inMemory)
34+
assert.Equal(t, "", o.path)
35+
36+
WithInMemory(false)(&o)
37+
assert.False(t, o.inMemory)
38+
assert.Equal(t, "", o.path)
39+
}
40+
41+
func TestOptionsBuildAppliesBadgerOptionsFunc(t *testing.T) {
42+
o := options{inMemory: true, logger: testLogger{}}
43+
WithBadgerOptionsFunc(func(opts badger.Options) badger.Options {
44+
opts.SyncWrites = false
45+
opts.NumMemtables = 7
46+
return opts
47+
})(&o)
48+
49+
b := o.build()
50+
assert.True(t, b.InMemory)
51+
assert.False(t, b.SyncWrites)
52+
assert.Equal(t, 7, b.NumMemtables)
53+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package pending
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/dgraph-io/badger/v3"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestItemIsDeletedOrExpiredTable(t *testing.T) {
13+
now := uint64(time.Now().Unix())
14+
15+
tests := []struct {
16+
name string
17+
e *badger.Entry
18+
want bool
19+
}{
20+
{name: "deleted", e: &badger.Entry{UserMeta: BitDelete}, want: true},
21+
{name: "no_expiry", e: &badger.Entry{ExpiresAt: 0}, want: false},
22+
{name: "future_expiry", e: &badger.Entry{ExpiresAt: now + 2}, want: false},
23+
{name: "past_expiry", e: &badger.Entry{ExpiresAt: now - 2}, want: true},
24+
}
25+
26+
for _, tt := range tests {
27+
t.Run(tt.name, func(t *testing.T) {
28+
i := &item{e: tt.e}
29+
assert.Equal(t, tt.want, i.IsDeletedOrExpired())
30+
})
31+
}
32+
}
33+
34+
func TestItemValueCopyIsSafeCopy(t *testing.T) {
35+
i := &item{e: &badger.Entry{Value: []byte("abc")}}
36+
b, err := i.ValueCopy(nil)
37+
require.NoError(t, err)
38+
require.Equal(t, []byte("abc"), b)
39+
b[0] = 'z'
40+
assert.Equal(t, []byte("abc"), i.e.Value)
41+
}

0 commit comments

Comments
 (0)