|
| 1 | +package querier |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync/atomic" |
| 7 | + "testing" |
| 8 | + |
| 9 | + sdk "github.com/cosmos/cosmos-sdk/types/grpc" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "google.golang.org/grpc" |
| 12 | + "google.golang.org/grpc/metadata" |
| 13 | +) |
| 14 | + |
| 15 | +// Mock Request and Response types |
| 16 | +type MockRequest struct{} |
| 17 | + |
| 18 | +type MockResponse struct { |
| 19 | + Value string |
| 20 | +} |
| 21 | + |
| 22 | +// Mock gRPC function generator |
| 23 | +func generateMockFuncWithBlockHeight(blockHeight string) QueryFunction[MockRequest, MockResponse] { |
| 24 | + return func(ctx context.Context, in *MockRequest, opts ...grpc.CallOption) (*MockResponse, error) { |
| 25 | + var header *metadata.MD |
| 26 | + for _, opt := range opts { |
| 27 | + if hOpt, ok := opt.(grpc.HeaderCallOption); ok { |
| 28 | + header = hOpt.HeaderAddr |
| 29 | + *header = metadata.Pairs(sdk.GRPCBlockHeightHeader, blockHeight) |
| 30 | + } |
| 31 | + } |
| 32 | + return &MockResponse{Value: "mock response"}, nil |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func mockFuncMissingHeader(ctx context.Context, in *MockRequest, opts ...grpc.CallOption) (*MockResponse, error) { |
| 37 | + return &MockResponse{Value: "mock response"}, nil |
| 38 | +} |
| 39 | + |
| 40 | +func TestGetResponseWithBlockHeight(t *testing.T) { |
| 41 | + in := &MockRequest{} |
| 42 | + opts := []grpc.CallOption{} |
| 43 | + |
| 44 | + resp, err := getResponseWithBlockHeight(generateMockFuncWithBlockHeight("15"), in, opts...) |
| 45 | + require.NoError(t, err) |
| 46 | + require.NotNil(t, resp) |
| 47 | + require.Equal(t, int64(15), resp.blockHeight) |
| 48 | + require.Equal(t, "mock response", resp.response.Value) |
| 49 | +} |
| 50 | + |
| 51 | +func TestGetResponseWithBlockHeight_MissingHeader(t *testing.T) { |
| 52 | + in := &MockRequest{} |
| 53 | + opts := []grpc.CallOption{} |
| 54 | + |
| 55 | + resp, err := getResponseWithBlockHeight(mockFuncMissingHeader, in, opts...) |
| 56 | + require.Error(t, err) |
| 57 | + require.Contains(t, err.Error(), "block height not found in header") |
| 58 | + require.Nil(t, resp) |
| 59 | +} |
| 60 | + |
| 61 | +func TestGetResponseWithBlockHeight_InvalidHeader(t *testing.T) { |
| 62 | + in := &MockRequest{} |
| 63 | + opts := []grpc.CallOption{} |
| 64 | + |
| 65 | + resp, err := getResponseWithBlockHeight(generateMockFuncWithBlockHeight("invalid"), in, opts...) |
| 66 | + require.Error(t, err) |
| 67 | + require.Contains(t, err.Error(), "failed to parse block height") |
| 68 | + require.Nil(t, resp) |
| 69 | +} |
| 70 | + |
| 71 | +func TestGetMaxBlockHeightResponse(t *testing.T) { |
| 72 | + fs := []QueryFunction[MockRequest, MockResponse]{ |
| 73 | + generateMockFuncWithBlockHeight("1"), |
| 74 | + generateMockFuncWithBlockHeight("12"), |
| 75 | + generateMockFuncWithBlockHeight("15"), |
| 76 | + } |
| 77 | + |
| 78 | + in := &MockRequest{} |
| 79 | + maxBlockHeight := new(atomic.Int64) |
| 80 | + maxBlockHeight.Store(10) |
| 81 | + opts := []grpc.CallOption{} |
| 82 | + |
| 83 | + resp, err := getMaxBlockHeightResponse(fs, in, maxBlockHeight, opts...) |
| 84 | + require.NoError(t, err) |
| 85 | + require.NotNil(t, resp) |
| 86 | + require.Equal(t, int64(15), maxBlockHeight.Load()) |
| 87 | + require.Equal(t, "mock response", resp.Value) |
| 88 | +} |
| 89 | + |
| 90 | +func TestGetMaxBlockHeightResponse_LowerThanCurrentMax(t *testing.T) { |
| 91 | + fs := []QueryFunction[MockRequest, MockResponse]{ |
| 92 | + generateMockFuncWithBlockHeight("19"), |
| 93 | + } |
| 94 | + |
| 95 | + in := &MockRequest{} |
| 96 | + maxBlockHeight := new(atomic.Int64) |
| 97 | + maxBlockHeight.Store(20) |
| 98 | + opts := []grpc.CallOption{} |
| 99 | + |
| 100 | + resp, err := getMaxBlockHeightResponse(fs, in, maxBlockHeight, opts...) |
| 101 | + require.Error(t, err) |
| 102 | + require.Contains(t, err.Error(), "block height is lower than latest max block height") |
| 103 | + require.Nil(t, resp) |
| 104 | +} |
| 105 | + |
| 106 | +func TestGetMaxBlockHeightResponse_AllFail(t *testing.T) { |
| 107 | + fs := []QueryFunction[MockRequest, MockResponse]{ |
| 108 | + func(ctx context.Context, in *MockRequest, opts ...grpc.CallOption) (*MockResponse, error) { |
| 109 | + return nil, fmt.Errorf("failed") |
| 110 | + }, |
| 111 | + func(ctx context.Context, in *MockRequest, opts ...grpc.CallOption) (*MockResponse, error) { |
| 112 | + return nil, fmt.Errorf("failed") |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + in := &MockRequest{} |
| 117 | + maxBlockHeight := new(atomic.Int64) |
| 118 | + maxBlockHeight.Store(10) |
| 119 | + opts := []grpc.CallOption{} |
| 120 | + |
| 121 | + _, err := getMaxBlockHeightResponse(fs, in, maxBlockHeight, opts...) |
| 122 | + require.Error(t, err) |
| 123 | +} |
0 commit comments