Skip to content

Commit

Permalink
Fix : Fix test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
OBlackmon3 committed Oct 22, 2024
1 parent 21f9a1c commit b903700
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 179 deletions.
3 changes: 3 additions & 0 deletions erpc/evm_json_rpc_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestEvmJsonRpcCache_Set(t *testing.T) {

assert.NoError(t, err)
mockConnector.AssertNotCalled(t, "Set")
mockConnector.On("HasTTL", mock.AnythingOfType("string")).Return(false)
})

t.Run("CacheIfBlockNumberIsFinalizedWhenBlockIsIrrelevantForPrimaryKey", func(t *testing.T) {
Expand Down Expand Up @@ -107,6 +108,7 @@ func TestEvmJsonRpcCache_Set(t *testing.T) {

t.Run("SkipWhenNoRefAndNoBlockNumberFound", func(t *testing.T) {
mockConnector, _, cache := createCacheTestFixtures(10, 15, nil)
mockConnector.On("HasTTL", mock.AnythingOfType("string")).Return(false)

req := common.NewNormalizedRequest([]byte(`{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x123","latest"],"id":1}`))
resp := common.NewNormalizedResponse().WithBody(util.StringToReaderCloser(`{"result":"0x1234"}`))
Expand All @@ -119,6 +121,7 @@ func TestEvmJsonRpcCache_Set(t *testing.T) {

t.Run("CacheIfBlockRefFoundWhetherBlockNumberExistsOrNot", func(t *testing.T) {
mockConnector, mockNetwork, cache := createCacheTestFixtures(10, 15, nil)
mockConnector.On("HasTTL", mock.AnythingOfType("string")).Return(false)

testCases := []struct {
name string
Expand Down
179 changes: 0 additions & 179 deletions erpc/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,182 +842,3 @@ func createServerTestFixtures(cfg *common.Config, t *testing.T) (

return sendRequest, baseURL
}

func createServerTestFixtures(cfg *common.Config, t *testing.T) (
func(body string, headers map[string]string, queryParams map[string]string) (int, string),
string,
) {
gock.EnableNetworking()
gock.NetworkingFilter(func(req *http.Request) bool {
shouldMakeRealCall := strings.Split(req.URL.Host, ":")[0] == "localhost"
return shouldMakeRealCall
})
defer gock.Off()

logger := zerolog.New(zerolog.NewConsoleWriter())
ctx := context.Background()
// ctx, cancel := context.WithCancel(context.Background())
// defer cancel()

erpcInstance, err := NewERPC(ctx, &logger, nil, cfg)
require.NoError(t, err)

httpServer := NewHttpServer(ctx, &logger, cfg.Server, erpcInstance)

listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
port := listener.Addr().(*net.TCPAddr).Port

go func() {
err := httpServer.server.Serve(listener)
if err != nil && err != http.ErrServerClosed {
t.Errorf("Server error: %v", err)
}
}()
// defer httpServer.server.Shutdown()

time.Sleep(1000 * time.Millisecond)

baseURL := fmt.Sprintf("http://localhost:%d", port)

sendRequest := func(body string, headers map[string]string, queryParams map[string]string) (int, string) {
req, err := http.NewRequest("POST", baseURL+"/test_project/evm/1", strings.NewReader(body))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
for k, v := range headers {
req.Header.Set(k, v)
}
q := req.URL.Query()
for k, v := range queryParams {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()

client := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return 0, err.Error()
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
return resp.StatusCode, err.Error()
}
return resp.StatusCode, string(respBody)
}

return sendRequest, baseURL
}

func TestHttpServer_MultipleUpstreams(t *testing.T) {
cfg := &common.Config{
Server: &common.ServerConfig{
MaxTimeout: "5s",
},
Projects: []*common.ProjectConfig{
{
Id: "test_project",
Networks: []*common.NetworkConfig{
{
Architecture: common.ArchitectureEvm,
Evm: &common.EvmNetworkConfig{
ChainId: 1,
},
},
},
Upstreams: []*common.UpstreamConfig{
{
Id: "rpc1",
Type: common.UpstreamTypeEvm,
Endpoint: "http://rpc1.localhost",
Evm: &common.EvmUpstreamConfig{
ChainId: 1,
},
VendorName: "llama",
},
{
Id: "rpc2",
Type: common.UpstreamTypeEvm,
Endpoint: "http://rpc2.localhost",
Evm: &common.EvmUpstreamConfig{
ChainId: 1,
},
VendorName: "",
},
},
},
},
RateLimiters: &common.RateLimiterConfig{},
}

sendRequest, _ := createServerTestFixtures(cfg, t)

t.Run("UpstreamNotAllowedByDirectiveViaHeaders", func(t *testing.T) {
gock.New("http://rpc1.localhost").
Post("/").
Reply(200).
JSON(map[string]interface{}{
"jsonrpc": "2.0",
"id": 111,
"result": "0x1111111",
})
gock.New("http://rpc2.localhost").
Post("/").
Reply(200).
JSON(map[string]interface{}{
"jsonrpc": "2.0",
"id": 222,
"result": "0x2222222",
})

statusCode2, body2 := sendRequest(`{"jsonrpc":"2.0","method":"eth_getBlockNumber","params":[],"id":1}`, map[string]string{
"X-ERPC-Use-Upstream": "rpc2",
}, nil)

assert.Equal(t, http.StatusOK, statusCode2)
assert.Contains(t, body2, "0x2222222")

statusCode1, body1 := sendRequest(`{"jsonrpc":"2.0","method":"eth_getBlockNumber","params":[],"id":1}`, nil, nil)

assert.Equal(t, http.StatusOK, statusCode1)
assert.Contains(t, body1, "0x1111111")

assert.True(t, gock.IsDone(), "All mocks should have been called")
})

t.Run("UpstreamNotAllowedByDirectiveViaQueryParams", func(t *testing.T) {
gock.New("http://rpc1.localhost").
Post("/").
Reply(200).
JSON(map[string]interface{}{
"jsonrpc": "2.0",
"id": 111,
"result": "0x1111111",
})
gock.New("http://rpc2.localhost").
Post("/").
Reply(200).
JSON(map[string]interface{}{
"jsonrpc": "2.0",
"id": 222,
"result": "0x2222222",
})

statusCode2, body2 := sendRequest(`{"jsonrpc":"2.0","method":"eth_getBlockNumber","params":[],"id":1}`, nil, map[string]string{
"use-upstream": "rpc2",
})

assert.Equal(t, http.StatusOK, statusCode2)
assert.Contains(t, body2, "0x2222222")

statusCode1, body1 := sendRequest(`{"jsonrpc":"2.0","method":"eth_getBlockNumber","params":[],"id":1}`, nil, nil)

assert.Equal(t, http.StatusOK, statusCode1)
assert.Contains(t, body1, "0x1111111")

assert.True(t, gock.IsDone(), "All mocks should have been called")
})
}

0 comments on commit b903700

Please sign in to comment.