aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-03-01 10:39:40 +0100
committerRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-03-01 10:39:40 +0100
commit5498b4ee782d2878a69e7311bd5bf7a21b1e6f04 (patch)
tree1b519ba86e2c8b009e8643bae22a595d68885216
parentc05c22ddbc771e7713849cae40f9d91bfafa0503 (diff)
added deadline matcher
Test that gRPC calls have deadlines.
-rw-r--r--endpoint_test.go35
-rw-r--r--sth_test.go4
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)