Skip to content

Commit

Permalink
Improved http error tests (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 committed Jun 13, 2023
1 parent 9f417fb commit cc3d361
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
5 changes: 3 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func (e *ErrNotFound) Is(target error) bool {
return ok
}

// ErrRemoteCall is returned from `group.Get()` when an error that is not a `ErrNotFound`
// is returned during a remote HTTP instance call
// ErrRemoteCall is returned from `group.Get()` when a remote GetterFunc returns an
// error. When this happens `group.Get()` does not attempt to retrieve the value
// via our local GetterFunc.
type ErrRemoteCall struct {
Msg string
}
Expand Down
25 changes: 16 additions & 9 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,24 @@ func TestHTTPPool(t *testing.T) {
}

// Get a key that does not exist
err := g.Get(ctx, "IDoNotExist", StringSink(&value))
err := g.Get(ctx, "IReturnErrNotFound", StringSink(&value))
errNotFound := &ErrNotFound{}
if !errors.As(err, &errNotFound) {
t.Fatal(errors.New("expected error to be 'ErrNotFound'"))
}
assert.Equal(t, "I do not exist error", errNotFound.Error())
assert.Equal(t, "I am a ErrNotFound error", errNotFound.Error())

// Get a key that is guaranteed to return an error
err = g.Get(ctx, "IAlwaysReturnAnError", StringSink(&value))
// Get a key that is guaranteed to return a remote error.
err = g.Get(ctx, "IReturnErrRemoteCall", StringSink(&value))
errRemoteCall := &ErrRemoteCall{}
if !errors.As(err, &errRemoteCall) {
t.Fatal(errors.New("expected error to be 'ErrRemoteCall'"))
}
assert.Equal(t, "I am a ErrRemoteCall error", errRemoteCall.Error())

assert.Equal(t, "I am a GetterFunc error", errRemoteCall.Error())
// Get a key that is guaranteed to return an internal (500) error
err = g.Get(ctx, "IReturnInternalError", StringSink(&value))
assert.Equal(t, "I am a errors.New() error", err.Error())

}

Expand All @@ -220,12 +223,16 @@ func beChildForTestHTTPPool(t *testing.T) {
p.Set(addrToURL(addrs)...)

getter := GetterFunc(func(ctx context.Context, key string, dest Sink) error {
if key == "IDoNotExist" {
return &ErrNotFound{Msg: "I do not exist error"}
if key == "IReturnErrNotFound" {
return &ErrNotFound{Msg: "I am a ErrNotFound error"}
}

if key == "IAlwaysReturnAnError" {
return &ErrRemoteCall{Msg: "I am a GetterFunc error"}
if key == "IReturnErrRemoteCall" {
return &ErrRemoteCall{Msg: "I am a ErrRemoteCall error"}
}

if key == "IReturnInternalError" {
return errors.New("I am a errors.New() error")
}

if _, err := http.Get(*serverAddr); err != nil {
Expand Down

0 comments on commit cc3d361

Please sign in to comment.