From 5498b4ee782d2878a69e7311bd5bf7a21b1e6f04 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Mon, 1 Mar 2021 10:39:40 +0100 Subject: added deadline matcher Test that gRPC calls have deadlines. --- endpoint_test.go | 35 ++++++++++++++++++++++++++++++----- sth_test.go | 4 ++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/endpoint_test.go b/endpoint_test.go index 7772837..801145c 100644 --- a/endpoint_test.go +++ b/endpoint_test.go @@ -2,6 +2,7 @@ package stfe import ( "bytes" + "context" "fmt" "reflect" "testing" @@ -53,7 +54,7 @@ func TestEndpointAddEntry(t *testing.T) { } req.Header.Set("Content-Type", "application/octet-stream") if table.trsp != nil || table.terr != nil { - ti.client.EXPECT().QueueLeaf(gomock.Any(), gomock.Any()).Return(table.trsp, table.terr) // TODO: deadline matcher? + ti.client.EXPECT().QueueLeaf(newDeadlineMatcher(), gomock.Any()).Return(table.trsp, table.terr) // TODO: deadline matcher? } w := httptest.NewRecorder() @@ -138,7 +139,7 @@ func TestEndpointGetLatestSth(t *testing.T) { t.Fatalf("must create http request: %v", err) } if table.trsp != nil || table.terr != nil { - ti.client.EXPECT().GetLatestSignedLogRoot(gomock.Any(), gomock.Any()).Return(table.trsp, table.terr) // TODO: deadline matcher? + ti.client.EXPECT().GetLatestSignedLogRoot(newDeadlineMatcher(), gomock.Any()).Return(table.trsp, table.terr) // TODO: deadline matcher? } w := httptest.NewRecorder() @@ -304,7 +305,7 @@ func TestEndpointGetProofByHash(t *testing.T) { } req.Header.Set("Content-Type", "application/octet-stream") if table.trsp != nil || table.terr != nil { - ti.client.EXPECT().GetInclusionProofByHash(gomock.Any(), gomock.Any()).Return(table.trsp, table.terr) // TODO: deadline matcher? + ti.client.EXPECT().GetInclusionProofByHash(newDeadlineMatcher(), gomock.Any()).Return(table.trsp, table.terr) // TODO: deadline matcher? } w := httptest.NewRecorder() @@ -366,7 +367,7 @@ func TestEndpointGetConsistencyProof(t *testing.T) { } req.Header.Set("Content-Type", "application/octet-stream") if table.trsp != nil || table.terr != nil { - ti.client.EXPECT().GetConsistencyProof(gomock.Any(), gomock.Any()).Return(table.trsp, table.terr) // TODO: deadline matcher? + ti.client.EXPECT().GetConsistencyProof(newDeadlineMatcher(), gomock.Any()).Return(table.trsp, table.terr) // TODO: deadline matcher? } w := httptest.NewRecorder() @@ -428,7 +429,7 @@ func TestEndpointGetEntriesV1(t *testing.T) { } req.Header.Set("Content-Type", "application/octet-stream") if table.trsp != nil || table.terr != nil { - ti.client.EXPECT().GetLeavesByRange(gomock.Any(), gomock.Any()).Return(table.trsp, table.terr) // TODO: deadline matcher? + ti.client.EXPECT().GetLeavesByRange(newDeadlineMatcher(), gomock.Any()).Return(table.trsp, table.terr) // TODO: deadline matcher? } w := httptest.NewRecorder() @@ -502,3 +503,27 @@ func TestEndpointPath(t *testing.T) { // TODO: TestWriteOctetResponse func TestWriteOctetResponse(t *testing.T) { } + +// deadlineMatcher implements gomock.Matcher, such that an error is raised if +// there is no context.Context deadline set +type deadlineMatcher struct{} + +// newDeadlineMatcher returns a new DeadlineMatcher +func newDeadlineMatcher() gomock.Matcher { + return &deadlineMatcher{} +} + +// Matches returns true if the passed interface is a context with a deadline +func (dm *deadlineMatcher) Matches(i interface{}) bool { + ctx, ok := i.(context.Context) + if !ok { + return false + } + _, ok = ctx.Deadline() + return ok +} + +// String is needed to implement gomock.Matcher +func (dm *deadlineMatcher) String() string { + return fmt.Sprintf("deadlineMatcher{}") +} diff --git a/sth_test.go b/sth_test.go index e5914c6..a75ca5f 100644 --- a/sth_test.go +++ b/sth_test.go @@ -41,7 +41,7 @@ func TestNewActiveSthSource(t *testing.T) { func() { // run deferred functions at the end of each iteration ti := newTestInstance(t, table.signer) defer ti.ctrl.Finish() - ti.client.EXPECT().GetLatestSignedLogRoot(gomock.Any(), gomock.Any()).Return(table.trsp, table.terr) + ti.client.EXPECT().GetLatestSignedLogRoot(newDeadlineMatcher(), gomock.Any()).Return(table.trsp, table.terr) source, err := NewActiveSthSource(ti.client, ti.instance.LogParameters) if got, want := err != nil, table.wantErr; got != want { t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err) @@ -102,7 +102,7 @@ func TestLatest(t *testing.T) { func() { // run deferred functions at the end of each iteration ti := newTestInstance(t, table.signer) defer ti.ctrl.Finish() - ti.client.EXPECT().GetLatestSignedLogRoot(gomock.Any(), gomock.Any()).Return(table.trsp, table.terr) + ti.client.EXPECT().GetLatestSignedLogRoot(gomock.Any(), gomock.Any()).Return(table.trsp, table.terr) // no deadline matcher because context is set by the caller of Latest(), i.e., this test on the line below sth, err := ti.instance.SthSource.Latest(context.Background()) if got, want := err != nil, table.wantErr; got != want { t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err) -- cgit v1.2.3