Skip to content

Commit f03a9fa

Browse files
committed
chore: add unit tests
1 parent 6423b58 commit f03a9fa

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

internal/datastore/crdb/watch.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ type changeDetails struct {
7070

7171
func (cds *crdbDatastore) DefaultsWatchOptions() datastore.WatchOptions {
7272
return datastore.WatchOptions{
73-
CheckpointInterval: 1 * time.Second,
74-
WatchBufferLength: defaultWatchBufferLength,
75-
WatchBufferWriteTimeout: defaultWatchBufferWriteTimeout,
76-
WatchConnectTimeout: defaultWatchConnectTimeout,
77-
MaximumBufferedChangesByteSize: 0, // 0 means no limit
73+
CheckpointInterval: 1 * time.Second,
74+
WatchBufferLength: defaultWatchBufferLength,
75+
WatchBufferWriteTimeout: defaultWatchBufferWriteTimeout,
76+
WatchConnectTimeout: defaultWatchConnectTimeout,
7877
}
7978
}
8079

8180
func (cds *crdbDatastore) Watch(ctx context.Context, afterRevision datastore.Revision, options datastore.WatchOptions) (<-chan datastore.RevisionChanges, <-chan error) {
81+
fmt.Printf("%+v\n", options)
8282
updates := make(chan datastore.RevisionChanges, options.WatchBufferLength)
8383
errs := make(chan error, 1)
8484

pkg/datastore/datastore.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -753,23 +753,22 @@ func BuildAndValidateWatchOptions(
753753
MaximumBufferedChangesByteSize: watchChangeBufferMaximumSize,
754754
}
755755

756-
// Apply datastore defaults for any zero values in server config
757-
if options.CheckpointInterval == 0 {
756+
if datastoreDefaults.CheckpointInterval > 0 {
758757
options.CheckpointInterval = datastoreDefaults.CheckpointInterval
759758
}
760759
if options.CheckpointInterval < 0 {
761760
return WatchOptions{}, errors.New("invalid checkpoint interval given")
762761
}
763-
if options.WatchBufferLength == 0 {
762+
if datastoreDefaults.WatchBufferLength > 0 {
764763
options.WatchBufferLength = datastoreDefaults.WatchBufferLength
765764
}
766-
if options.WatchBufferWriteTimeout <= 0 {
765+
if datastoreDefaults.WatchBufferWriteTimeout > 0 {
767766
options.WatchBufferWriteTimeout = datastoreDefaults.WatchBufferWriteTimeout
768767
}
769-
if options.WatchConnectTimeout <= 0 {
768+
if datastoreDefaults.WatchConnectTimeout > 0 {
770769
options.WatchConnectTimeout = datastoreDefaults.WatchConnectTimeout
771770
}
772-
if options.MaximumBufferedChangesByteSize == 0 {
771+
if datastoreDefaults.MaximumBufferedChangesByteSize > 0 {
773772
options.MaximumBufferedChangesByteSize = datastoreDefaults.MaximumBufferedChangesByteSize
774773
}
775774

pkg/datastore/datastore_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package datastore
33
import (
44
"context"
55
"testing"
6+
"time"
67

78
"github.com/stretchr/testify/require"
89

@@ -12,6 +13,37 @@ import (
1213
"github.com/authzed/spicedb/pkg/tuple"
1314
)
1415

16+
func TestBuildAndValidateWatchOptions(t *testing.T) {
17+
t.Run("WatchBufferWriteTimeout", func(t *testing.T) {
18+
c, err := BuildAndValidateWatchOptions(ServerWatchConfig{WatchBufferWriteTimeout: 1 * time.Second}, ClientWatchOptions{}, WatchOptions{WatchBufferWriteTimeout: 2 * time.Second})
19+
require.NoError(t, err)
20+
require.NotNil(t, c)
21+
require.Equal(t, 2*time.Second, c.WatchBufferWriteTimeout)
22+
})
23+
t.Run("WatchConnectTimeout", func(t *testing.T) {
24+
c, err := BuildAndValidateWatchOptions(ServerWatchConfig{WatchConnectTimeout: 1 * time.Second}, ClientWatchOptions{}, WatchOptions{WatchConnectTimeout: 2 * time.Second})
25+
require.NoError(t, err)
26+
require.NotNil(t, c)
27+
require.Equal(t, 2*time.Second, c.WatchConnectTimeout)
28+
})
29+
t.Run("WatchBufferLength", func(t *testing.T) {
30+
c, err := BuildAndValidateWatchOptions(ServerWatchConfig{WatchBufferLength: 100}, ClientWatchOptions{}, WatchOptions{WatchBufferLength: 200})
31+
require.NoError(t, err)
32+
require.NotNil(t, c)
33+
require.Equal(t, uint16(200), c.WatchBufferLength)
34+
})
35+
t.Run("CheckpointInterval", func(t *testing.T) {
36+
c, err := BuildAndValidateWatchOptions(ServerWatchConfig{CheckpointInterval: 1}, ClientWatchOptions{}, WatchOptions{CheckpointInterval: 2})
37+
require.NoError(t, err)
38+
require.NotNil(t, c)
39+
require.Equal(t, time.Duration(2), c.CheckpointInterval)
40+
})
41+
t.Run("Invalid CheckpointInterval", func(t *testing.T) {
42+
_, err := BuildAndValidateWatchOptions(ServerWatchConfig{CheckpointInterval: -1}, ClientWatchOptions{}, WatchOptions{CheckpointInterval: -1})
43+
require.Error(t, err)
44+
})
45+
}
46+
1547
func TestRelationshipsFilterFromPublicFilter(t *testing.T) {
1648
tests := []struct {
1749
name string

0 commit comments

Comments
 (0)