Skip to content

Commit

Permalink
Simplify usage of the recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 5, 2024
1 parent 288c7e1 commit 75dea0b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 74 deletions.
25 changes: 10 additions & 15 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ func TestClient(t *testing.T) {
t.Fatal(err)
}

dispatchtest.AssertDispatchRequests(t, recorder.Requests, []dispatchtest.DispatchRequest{
{
ApiKey: "foobar",
Calls: []dispatch.Call{call},
},
recorder.Assert(t, dispatchtest.DispatchRequest{
ApiKey: "foobar",
Calls: []dispatch.Call{call},
})
}

Expand All @@ -58,11 +56,9 @@ func TestClientEnvConfig(t *testing.T) {
t.Fatal(err)
}

dispatchtest.AssertDispatchRequests(t, recorder.Requests, []dispatchtest.DispatchRequest{
{
ApiKey: "foobar",
Calls: []dispatch.Call{call},
},
recorder.Assert(t, dispatchtest.DispatchRequest{
ApiKey: "foobar",
Calls: []dispatch.Call{call},
})
}

Expand Down Expand Up @@ -107,16 +103,15 @@ func TestClientBatch(t *testing.T) {
t.Fatal(err)
}

dispatchtest.AssertDispatchRequests(t, recorder.Requests, []dispatchtest.DispatchRequest{
{
recorder.Assert(t,
dispatchtest.DispatchRequest{
ApiKey: "foobar",
Calls: []dispatch.Call{call1, call2},
},
{
dispatchtest.DispatchRequest{
ApiKey: "foobar",
Calls: []dispatch.Call{call3, call4},
},
})
})
}

func TestClientNoAPIKey(t *testing.T) {
Expand Down
24 changes: 9 additions & 15 deletions dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ func TestDispatchCall(t *testing.T) {
t.Fatal(err)
}

dispatchtest.AssertDispatchRequests(t, recorder.Requests, []dispatchtest.DispatchRequest{
{
ApiKey: "foobar",
Calls: []dispatch.Call{wantCall},
},
recorder.Assert(t, dispatchtest.DispatchRequest{
ApiKey: "foobar",
Calls: []dispatch.Call{wantCall},
})
}

Expand Down Expand Up @@ -151,11 +149,9 @@ func TestDispatchCallEnvConfig(t *testing.T) {
t.Fatal(err)
}

dispatchtest.AssertDispatchRequests(t, recorder.Requests, []dispatchtest.DispatchRequest{
{
ApiKey: "foobar",
Calls: []dispatch.Call{wantCall},
},
recorder.Assert(t, dispatchtest.DispatchRequest{
ApiKey: "foobar",
Calls: []dispatch.Call{wantCall},
})
}

Expand Down Expand Up @@ -201,11 +197,9 @@ func TestDispatchCallsBatch(t *testing.T) {
t.Fatal(err)
}

dispatchtest.AssertDispatchRequests(t, recorder.Requests, []dispatchtest.DispatchRequest{
{
ApiKey: "foobar",
Calls: []dispatch.Call{call1, call2},
},
recorder.Assert(t, dispatchtest.DispatchRequest{
ApiKey: "foobar",
Calls: []dispatch.Call{call1, call2},
})
}

Expand Down
42 changes: 0 additions & 42 deletions dispatchtest/assert.go

This file was deleted.

28 changes: 26 additions & 2 deletions dispatchtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"strconv"
"strings"
"testing"

"buf.build/gen/go/stealthrocket/dispatch-proto/connectrpc/go/dispatch/sdk/v1/sdkv1connect"
sdkv1 "buf.build/gen/go/stealthrocket/dispatch-proto/protocolbuffers/go/dispatch/sdk/v1"
Expand Down Expand Up @@ -82,7 +83,7 @@ func wrapCall(c *sdkv1.Call) (dispatch.Call, error) {

// CallRecorder is a DispatchServerHandler that captures requests to the Dispatch API.
type CallRecorder struct {
Requests []DispatchRequest
requests []DispatchRequest
calls int
}

Expand All @@ -96,7 +97,7 @@ func (r *CallRecorder) Handle(ctx context.Context, apiKey string, calls []dispat
base := r.calls
r.calls += len(calls)

r.Requests = append(r.Requests, DispatchRequest{
r.requests = append(r.requests, DispatchRequest{
ApiKey: apiKey,
Calls: calls,
})
Expand All @@ -107,3 +108,26 @@ func (r *CallRecorder) Handle(ctx context.Context, apiKey string, calls []dispat
}
return ids, nil
}

func (r *CallRecorder) Assert(t *testing.T, want ...DispatchRequest) {
t.Helper()

got := r.requests
if len(got) != len(want) {
t.Fatalf("unexpected number of requests: got %v, want %v", len(got), len(want))
}
for i, req := range got {
if req.ApiKey != want[i].ApiKey {
t.Errorf("unexpected API key on request %d: got %v, want %v", i, req.ApiKey, want[i].ApiKey)
}
if len(req.Calls) != len(want[i].Calls) {
t.Errorf("unexpected number of calls in request %d: got %v, want %v", i, len(req.Calls), len(want[i].Calls))
} else {
for j, call := range req.Calls {
if !call.Equal(want[i].Calls[j]) {
t.Errorf("unexpected request %d call %d: got %v, want %v", i, j, call, want[i].Calls[j])
}
}
}
}
}

0 comments on commit 75dea0b

Please sign in to comment.