Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
services:
redis:
image: redis:alpine
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down
17 changes: 8 additions & 9 deletions pkg/rpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func TestNodeScore(t *testing.T) {
n := &Node{
config: NodeConfig{Priority: 10},
}

// Initial score: 10 * 100 = 1000
assert.Equal(t, int64(1000), n.Score(0))

// Simulate latency (no errors recorded)
n.RecordMetric(time.Now().Add(-100*time.Millisecond), nil)
n.RecordMetric(time.Now().Add(-100*time.Millisecond), nil)
// Latency update: (old=0) -> set to 100.
// Score: 1000 - (100/10) = 990
assert.Equal(t, int64(990), n.Score(0))
Expand Down Expand Up @@ -58,8 +58,8 @@ func TestMultiClient_Failover(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, uint64(100), h)

// Check metrics: Node 1 should have errors
assert.Equal(t, uint64(1), node1.GetTotalErrors())
// Check metrics: Node 1 should have at least 1 error (from background sync or manual call)
assert.GreaterOrEqual(t, node1.GetTotalErrors(), uint64(1))
}

func TestNode_ScoreLag(t *testing.T) {
Expand All @@ -77,7 +77,7 @@ func TestExecute_RetryLimit(t *testing.T) {
mockEth := new(MockEthClient)
// Fail 3 times. Also allow background sync calls.
mockEth.On("BlockNumber", mock.Anything).Return(uint64(0), errors.New("fail")).Maybe()

node := NewNodeWithClient(NodeConfig{URL: "node1", Priority: 10}, mockEth)
mc, _ := NewClientWithNodes(ctx, []*Node{node}, 100)

Expand All @@ -89,7 +89,7 @@ func TestExecute_ContextCanceled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
mockEth := new(MockEthClient)
mockEth.On("BlockNumber", mock.Anything).Return(uint64(100), nil).Maybe()

node := NewNodeWithClient(NodeConfig{URL: "node1", Priority: 10}, mockEth)
mc, _ := NewClientWithNodes(ctx, []*Node{node}, 100)

Expand All @@ -102,7 +102,7 @@ func TestExecute_ContextCanceled(t *testing.T) {
func TestProxyMethods(t *testing.T) {
ctx := context.Background()
mockEth := new(MockEthClient)

// Expect background sync calls (immediate one)
mockEth.On("BlockNumber", mock.Anything).Return(uint64(100), nil).Maybe()

Expand Down Expand Up @@ -150,7 +150,7 @@ func TestProxyMethods(t *testing.T) {
func TestNewClient_Errors(t *testing.T) {
_, err := NewClient(context.Background(), []NodeConfig{}, 10)
assert.Error(t, err)

_, err = NewClientWithNodes(context.Background(), []*Node{}, 10)
assert.Error(t, err)
}
Expand All @@ -170,4 +170,3 @@ func TestNodeGetters(t *testing.T) {
assert.Equal(t, "http://test", n.URL())
assert.Equal(t, 5, n.Priority())
}

23 changes: 20 additions & 3 deletions pkg/sink/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestPostgresOutput_Send(t *testing.T) {

logs := []DecodedLog{
{
Log: types.Log{BlockNumber: 100, TxHash: common.HexToHash("0xabc"), Index: 1},
Log: types.Log{BlockNumber: 100, TxHash: common.HexToHash("0xabc"), Index: 1},
EventName: "Transfer",
},
}
Expand All @@ -207,12 +207,29 @@ func TestPostgresOutput_Send(t *testing.T) {
}

func TestWebhookOutput_Async(t *testing.T) {
wo := NewWebhookOutput("http://localhost", "secret", 1, "1s", "10s", true, 10, 1)
called := make(chan bool, 1)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called <- true
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()

wo := NewWebhookOutput(ts.URL, "secret", 1, "1s", "10s", true, 10, 1)
logs := []DecodedLog{{Log: types.Log{Index: 1}}}

start := time.Now()
err := wo.Send(context.Background(), logs)
assert.NoError(t, err)
// Must return immediately in async mode
assert.Less(t, time.Since(start), 100*time.Millisecond)

// Wait for background worker to deliver
select {
case <-called:
case <-time.After(1 * time.Second):
t.Fatal("Async webhook was never delivered")
}

err = wo.Close()
assert.NoError(t, err)
}
Expand All @@ -223,4 +240,4 @@ func TestConsoleOutput(t *testing.T) {
err := c.Send(context.Background(), []DecodedLog{{Log: types.Log{Index: 1, Topics: []common.Hash{}}}})
assert.NoError(t, err)
assert.NoError(t, c.Close())
}
}
Loading