Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Test_Client_LatestReport to parameterized test #11670

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
}
Loading