Improved http error tests

This commit is contained in:
Derrick J. Wippler 2023-06-12 12:13:06 -05:00
parent 9f417fbc4f
commit 6296916fe6
2 changed files with 19 additions and 11 deletions

View File

@ -17,8 +17,9 @@ func (e *ErrNotFound) Is(target error) bool {
return ok return ok
} }
// ErrRemoteCall is returned from `group.Get()` when an error that is not a `ErrNotFound` // ErrRemoteCall is returned from `group.Get()` when a remote GetterFunc returns an
// is returned during a remote HTTP instance call // error. When this happens `group.Get()` does not attempt to retrieve the value
// via our local GetterFunc.
type ErrRemoteCall struct { type ErrRemoteCall struct {
Msg string Msg string
} }

View File

@ -187,21 +187,24 @@ func TestHTTPPool(t *testing.T) {
} }
// Get a key that does not exist // Get a key that does not exist
err := g.Get(ctx, "IDoNotExist", StringSink(&value)) err := g.Get(ctx, "IReturnErrNotFound", StringSink(&value))
errNotFound := &ErrNotFound{} errNotFound := &ErrNotFound{}
if !errors.As(err, &errNotFound) { if !errors.As(err, &errNotFound) {
t.Fatal(errors.New("expected error to be '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 // Get a key that is guaranteed to return a remote error.
err = g.Get(ctx, "IAlwaysReturnAnError", StringSink(&value)) err = g.Get(ctx, "IReturnErrRemoteCall", StringSink(&value))
errRemoteCall := &ErrRemoteCall{} errRemoteCall := &ErrRemoteCall{}
if !errors.As(err, &errRemoteCall) { if !errors.As(err, &errRemoteCall) {
t.Fatal(errors.New("expected error to be '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())
} }
@ -220,12 +223,16 @@ func beChildForTestHTTPPool(t *testing.T) {
p.Set(addrToURL(addrs)...) p.Set(addrToURL(addrs)...)
getter := GetterFunc(func(ctx context.Context, key string, dest Sink) error { getter := GetterFunc(func(ctx context.Context, key string, dest Sink) error {
if key == "IDoNotExist" { if key == "IReturnErrNotFound" {
return &ErrNotFound{Msg: "I do not exist error"} return &ErrNotFound{Msg: "I am a ErrNotFound error"}
} }
if key == "IAlwaysReturnAnError" { if key == "IReturnErrRemoteCall" {
return &ErrRemoteCall{Msg: "I am a GetterFunc error"} 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 { if _, err := http.Get(*serverAddr); err != nil {