Skip to content

Commit

Permalink
update spec to handle context cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajatprabha committed Aug 23, 2023
1 parent e1a7788 commit ca5670b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions xload/async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package xload
import (
"context"
"errors"
"github.com/stretchr/testify/assert"
"reflect"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestLoad_Async(t *testing.T) {
Expand Down Expand Up @@ -56,13 +55,18 @@ func TestLoad_Async_Error(t *testing.T) {

err := Load(ctx, &dest, Concurrency(2),
LoaderFunc(func(ctx context.Context, key string) (string, error) {
<-time.After(100 * time.Millisecond)
return "", nil
// simulate a slow loader
select {
case <-ctx.Done():
return "", ctx.Err()
case <-time.After(100 * time.Millisecond):
return "name", nil
}
}),
)

assert.Error(t, err)
assert.True(t, errors.Is(err, context.Canceled))
assert.NotEqual(t, "name", dest.Name)
assert.EqualError(t, err, context.Canceled.Error())
})

errMap := map[string]error{
Expand Down

0 comments on commit ca5670b

Please sign in to comment.