Skip to content

Prevent panic when batchFunc returns nil in his results #64

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

Open
wants to merge 3 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/
.idea
42 changes: 39 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions dataloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,13 @@ func (b *batcher) batch(originalContext context.Context) {
return
}

// When a batchFunc returns a nil in it's items, we replace those by a Result struct containing an error
for key, value := range items {
if value == nil {
items[key] = &Result{Error: fmt.Errorf("no value for key")}
}
}

for i, req := range reqs {
req.channel <- items[i]
close(req.channel)
Expand Down
50 changes: 50 additions & 0 deletions dataloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ func TestLoader(t *testing.T) {
// TODO: expect to get some kind of warning
})

t.Run("first result is a nil", func(t *testing.T) {
t.Parallel()
faultyLoader, _ := LoaderNilInsteadOfResult()
ctx := context.Background()

n := 10
reqs := []Thunk{}
keys := Keys{}
for i := 0; i < n; i++ {
key := StringKey(strconv.Itoa(i))
reqs = append(reqs, faultyLoader.Load(ctx, key))
keys = append(keys, key)
}

for i, future := range reqs {
_, err := future()
if i == 0 && err == nil {
t.Error("expected first result to contain an error")
}

if i != 0 && err != nil {
t.Error("expected rest of results not to contain an error")
}
}
})

t.Run("responds to max batch size", func(t *testing.T) {
t.Parallel()
identityLoader, loadCalls := IDLoader(2)
Expand Down Expand Up @@ -586,6 +612,30 @@ func FaultyLoader() (*Loader, *[][]string) {
return loader, &loadCalls
}

// LoaderNilInsteadOfResult gives a nil result back for the first key.
func LoaderNilInsteadOfResult() (*Loader, *[][]string) {
var mu sync.Mutex
var loadCalls [][]string

loader := NewBatchedLoader(func(_ context.Context, keys Keys) []*Result {
var results []*Result
mu.Lock()
loadCalls = append(loadCalls, keys.Keys())
mu.Unlock()

for i, key := range keys {
if i == 0 {
results = append(results, nil)
} else {
results = append(results, &Result{key, nil})
}
}
return results
})

return loader, &loadCalls
}

///////////////////////////////////////////////////
// Benchmarks
///////////////////////////////////////////////////
Expand Down