Skip to content

Commit

Permalink
Refactoring Test_Client_LatestReport to parameterized test (#11670)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhuie19 authored Jan 3, 2024
1 parent 48002f2 commit adfa4bd
Showing 1 changed file with 51 additions and 89 deletions.
140 changes: 51 additions & 89 deletions core/services/relay/evm/mercury/wsrpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,97 +120,59 @@ func Test_Client_Transmit(t *testing.T) {
func Test_Client_LatestReport(t *testing.T) {
lggr := logger.TestLogger(t)
ctx := testutils.Context(t)
cacheReads := 5

tests := []struct {
name string
ttl time.Duration
expectedCalls int
}{
{
name: "with cache disabled",
ttl: 0,
expectedCalls: 5,
},
{
name: "with cache enabled",
ttl: 1000 * time.Hour, //some large value that will never expire during a test
expectedCalls: 1,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := &pb.LatestReportRequest{}

cacheSet := cache.NewCacheSet(lggr, cache.Config{LatestReportTTL: tt.ttl})

resp := &pb.LatestReportResponse{}

var calls int
wsrpcClient := &mocks.MockWSRPCClient{
LatestReportF: func(ctx context.Context, in *pb.LatestReportRequest) (*pb.LatestReportResponse, error) {
calls++
assert.Equal(t, req, in)
return resp, nil
},
}

t.Run("with nil cache", func(t *testing.T) {
req := &pb.LatestReportRequest{}
noopCacheSet := newNoopCacheSet()
resp := &pb.LatestReportResponse{}

wsrpcClient := &mocks.MockWSRPCClient{
LatestReportF: func(ctx context.Context, in *pb.LatestReportRequest) (*pb.LatestReportResponse, error) {
assert.Equal(t, req, in)
return resp, nil
},
}

conn := &mocks.MockConn{
Ready: true,
}
c := newClient(lggr, csakey.KeyV2{}, nil, "", noopCacheSet)
c.conn = conn
c.rawClient = wsrpcClient
require.NoError(t, c.StartOnce("Mock WSRPC Client", func() error { return nil }))

r, err := c.LatestReport(ctx, req)

require.NoError(t, err)
assert.Equal(t, resp, r)
})

t.Run("with cache disabled", func(t *testing.T) {
req := &pb.LatestReportRequest{}
cacheSet := cache.NewCacheSet(lggr, cache.Config{LatestReportTTL: 0})
resp := &pb.LatestReportResponse{}

var calls int
wsrpcClient := &mocks.MockWSRPCClient{
LatestReportF: func(ctx context.Context, in *pb.LatestReportRequest) (*pb.LatestReportResponse, error) {
calls++
assert.Equal(t, req, in)
return resp, nil
},
}

conn := &mocks.MockConn{
Ready: true,
}
c := newClient(lggr, csakey.KeyV2{}, nil, "", cacheSet)
c.conn = conn
c.rawClient = wsrpcClient

servicetest.Run(t, cacheSet)
simulateStart(ctx, t, c)

for i := 0; i < 5; i++ {
r, err := c.LatestReport(ctx, req)

require.NoError(t, err)
assert.Equal(t, resp, r)
}
assert.Equal(t, 5, calls, "expected 5 calls to LatestReport but it was called %d times", calls)
})

t.Run("with caching", func(t *testing.T) {
req := &pb.LatestReportRequest{}
const neverExpireTTL = 1000 * time.Hour // some massive value that will never expire during a test
cacheSet := cache.NewCacheSet(lggr, cache.Config{LatestReportTTL: neverExpireTTL})
resp := &pb.LatestReportResponse{}

var calls int
wsrpcClient := &mocks.MockWSRPCClient{
LatestReportF: func(ctx context.Context, in *pb.LatestReportRequest) (*pb.LatestReportResponse, error) {
calls++
assert.Equal(t, req, in)
return resp, nil
},
}

conn := &mocks.MockConn{
Ready: true,
}
c := newClient(lggr, csakey.KeyV2{}, nil, "", cacheSet)
c.conn = conn
c.rawClient = wsrpcClient
conn := &mocks.MockConn{
Ready: true,
}
c := newClient(lggr, csakey.KeyV2{}, nil, "", cacheSet)
c.conn = conn
c.rawClient = wsrpcClient

servicetest.Run(t, cacheSet)
simulateStart(ctx, t, c)
servicetest.Run(t, cacheSet)
simulateStart(ctx, t, c)

for i := 0; i < 5; i++ {
r, err := c.LatestReport(ctx, req)
for i := 0; i < cacheReads; i++ {
r, err := c.LatestReport(ctx, req)

require.NoError(t, err)
assert.Equal(t, resp, r)
}
assert.Equal(t, 1, calls, "expected only 1 call to LatestReport but it was called %d times", calls)
})
require.NoError(t, err)
assert.Equal(t, resp, r)
}
assert.Equal(t, tt.expectedCalls, calls, "expected %d calls to LatestReport but it was called %d times", tt.expectedCalls, calls)
})
}
}

0 comments on commit adfa4bd

Please sign in to comment.