Skip to content

Commit

Permalink
added rpc test
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelsc committed Feb 12, 2025
1 parent e5fa0d3 commit 9afa93b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
71 changes: 69 additions & 2 deletions services/services_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package services

import (
_ "embed"
"encoding/json"
"math"
"math/big"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/gobitfly/eth2-beaconchain-explorer/types"

"github.com/gobitfly/eth2-beaconchain-explorer/utils"
)

//go:embed services_test_block.json
var pendingBlock string

// --- Dummy types for testing suggestGasPrices --- //

// checkSuggestion compares a suggestion value with an expected value.
Expand All @@ -16,6 +27,41 @@ func checkSuggestion(t *testing.T, name string, got, expected int64) {
}
}

func newFakeRPCServer(pendingBlock, latestBlock string) *httptest.Server {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {
ID interface{} `json:"id"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}

var block json.RawMessage // Use RawMessage to handle JSON correctly
// Choose the block based on the first parameter.
if req.Method == "eth_getBlockByNumber" && len(req.Params) > 0 {
if param, ok := req.Params[0].(string); ok {
if param == "pending" {
block = json.RawMessage(pendingBlock)
} else if param == "latest" {
block = json.RawMessage(latestBlock)
}
}
}

resp := map[string]interface{}{
"jsonrpc": "2.0",
"id": req.ID,
"result": block,
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
})
return httptest.NewServer(handler)
}

// --- Test Cases --- //

// TestSuggestGasPrices_SufficientTxs creates 50 transactions with tip values 1..50.
Expand Down Expand Up @@ -144,11 +190,32 @@ func TestSuggestGasPrices_Timestamp(t *testing.T) {
}
}

func TestViaFakeRPC(t *testing.T) {
cfg := &types.Config{}
utils.Config = cfg

server := newFakeRPCServer(pendingBlock, "")
defer server.Close()
utils.Config.Eth1GethEndpoint = server.URL

result, err := getGasNowData()
if err != nil {
t.Errorf("Error: %v", err)
}
if result == nil {

Check failure on line 205 in services/services_test.go

View workflow job for this annotation

GitHub Actions / Run CI (ubuntu-latest, 1.21.x)

this check suggests that the pointer can be nil

Check failure on line 205 in services/services_test.go

View workflow job for this annotation

GitHub Actions / Run CI (ubuntu-latest, 1.21.x)

this check suggests that the pointer can be nil

Check failure on line 205 in services/services_test.go

View workflow job for this annotation

GitHub Actions / Run CI (ubuntu-latest, 1.21.x)

this check suggests that the pointer can be nil

Check failure on line 205 in services/services_test.go

View workflow job for this annotation

GitHub Actions / Run CI (ubuntu-latest, 1.21.x)

this check suggests that the pointer can be nil
t.Errorf("Error: data is nil")
}

checkSuggestion(t, "Rapid", result.Data.Rapid.Int64(), 3939435494)

Check failure on line 209 in services/services_test.go

View workflow job for this annotation

GitHub Actions / Run CI (ubuntu-latest, 1.21.x)

possible nil pointer dereference (SA5011)
checkSuggestion(t, "Fast", result.Data.Fast.Int64(), 3511919489)

Check failure on line 210 in services/services_test.go

View workflow job for this annotation

GitHub Actions / Run CI (ubuntu-latest, 1.21.x)

possible nil pointer dereference (SA5011)
checkSuggestion(t, "Standard", result.Data.Standard.Int64(), 3123069597)

Check failure on line 211 in services/services_test.go

View workflow job for this annotation

GitHub Actions / Run CI (ubuntu-latest, 1.21.x)

possible nil pointer dereference (SA5011)
checkSuggestion(t, "Slow", result.Data.Slow.Int64(), 3073069597)

Check failure on line 212 in services/services_test.go

View workflow job for this annotation

GitHub Actions / Run CI (ubuntu-latest, 1.21.x)

possible nil pointer dereference (SA5011)
}

// func TestGasNow2(t *testing.T) {
// cfg := &types.Config{}
// utils.Config = cfg
// utils.Config.Eth1GethEndpoint = "http://localhost:18545"

// // utils.Config.Eth1GethEndpoint = "http://localhost:18545"
// data, err := getGasNowData()
// if err != nil {
// t.Errorf("Error: %v", err)
Expand Down
1 change: 1 addition & 0 deletions services/services_test_block.json

Large diffs are not rendered by default.

0 comments on commit 9afa93b

Please sign in to comment.