Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix flaky WithClearCacheOnBatch test #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion dataloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (l *Loader) reset() {
l.curBatcher = nil

if l.clearCacheOnBatch {
l.cache.Clear()
l.ClearAll()
}
}

Expand Down
18 changes: 16 additions & 2 deletions dataloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ func TestLoader(t *testing.T) {
t.Parallel()
batchOnlyLoader, loadCalls := BatchOnlyLoader(0)
ctx := context.Background()

// The first two calls should be part of the same batch.
// After the batch fires, we expect the cache to be cleared,
// so subsequent calls should be recorded.
future1 := batchOnlyLoader.Load(ctx, StringKey("1"))
future2 := batchOnlyLoader.Load(ctx, StringKey("1"))

Expand All @@ -352,8 +356,18 @@ func TestLoader(t *testing.T) {
t.Errorf("did not batch queries. Expected %#v, got %#v", expected, calls)
}

if _, found := batchOnlyLoader.cache.Get(ctx, StringKey("1")); found {
t.Errorf("did not clear cache after batch. Expected %#v, got %#v", false, found)
// This call should record another call
future3 := batchOnlyLoader.Load(ctx, StringKey("1"))

_, err = future3()
if err != nil {
t.Error(err.Error())
}

calls = *loadCalls
expected = [][]string{{"1"}, {"1"}}
if !reflect.DeepEqual(calls, expected) {
t.Errorf("expected a second batch, got %#v", calls)
}
})

Expand Down