Skip to content

Commit 6c85fba

Browse files
authored
Merge branch 'main' into julien/fi
2 parents a076cb9 + a75b4bc commit 6c85fba

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

pkg/rpc/server/http.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ func RegisterCustomHTTPEndpoints(mux *http.ServeMux, s store.Store, pm p2p.P2PRP
9090
}
9191

9292
if cfg.Node.Aggregator {
93+
var maxAllowedDelay time.Duration
94+
if cfg.Node.LazyMode {
95+
maxAllowedDelay = 2 * cfg.Node.LazyBlockInterval.Duration
96+
} else {
97+
maxAllowedDelay = 5 * cfg.Node.BlockTime.Duration
98+
}
9399
timeSinceLastBlock := time.Since(state.LastBlockTime)
94-
maxAllowedDelay := 5 * cfg.Node.BlockTime.Duration
95-
96100
if timeSinceLastBlock > maxAllowedDelay {
97101
http.Error(w, "UNREADY: aggregator not producing blocks at expected rate", http.StatusServiceUnavailable)
98102
return

pkg/rpc/server/http_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55
"net/http"
66
"net/http/httptest"
77
"testing"
8+
"time"
89

910
"github.com/evstack/ev-node/pkg/config"
1011
"github.com/evstack/ev-node/test/mocks"
12+
"github.com/evstack/ev-node/types"
1113
"github.com/rs/zerolog"
1214
"github.com/stretchr/testify/assert"
1315
"github.com/stretchr/testify/mock"
16+
"github.com/stretchr/testify/require"
1417
)
1518

1619
func TestRegisterCustomHTTPEndpoints(t *testing.T) {
@@ -38,3 +41,94 @@ func TestRegisterCustomHTTPEndpoints(t *testing.T) {
3841

3942
mockStore.AssertExpectations(t)
4043
}
44+
45+
func TestHealthReady_aggregatorBlockDelay(t *testing.T) {
46+
ctx := t.Context()
47+
logger := zerolog.Nop()
48+
49+
type spec struct {
50+
lazy bool
51+
blockTime time.Duration
52+
lazyInterval time.Duration
53+
delay time.Duration
54+
expStatusCode int
55+
expBody string
56+
}
57+
58+
specs := map[string]spec{
59+
"aggregator within non-lazy threshold": {
60+
lazy: false,
61+
blockTime: 200 * time.Millisecond,
62+
lazyInterval: 0,
63+
delay: 800 * time.Millisecond, // 5x blockTime = 1s, so 0.8s is OK
64+
expStatusCode: http.StatusOK,
65+
expBody: "READY\n",
66+
},
67+
"aggregator exceeds non-lazy threshold": {
68+
lazy: false,
69+
blockTime: 200 * time.Millisecond,
70+
lazyInterval: 0,
71+
delay: 1500 * time.Millisecond, // > 1s threshold
72+
expStatusCode: http.StatusServiceUnavailable,
73+
expBody: "UNREADY: aggregator not producing blocks at expected rate\n",
74+
},
75+
"aggregator within lazy threshold": {
76+
lazy: true,
77+
blockTime: 0,
78+
lazyInterval: 300 * time.Millisecond,
79+
delay: 500 * time.Millisecond, // 2x lazyInterval = 600ms, so 0.5s is OK
80+
expStatusCode: http.StatusOK,
81+
expBody: "READY\n",
82+
},
83+
"aggregator exceeds lazy threshold": {
84+
lazy: true,
85+
blockTime: 0,
86+
lazyInterval: 300 * time.Millisecond,
87+
delay: 800 * time.Millisecond, // > 600ms threshold
88+
expStatusCode: http.StatusServiceUnavailable,
89+
expBody: "UNREADY: aggregator not producing blocks at expected rate\n",
90+
},
91+
}
92+
93+
for name, tc := range specs {
94+
t.Run(name, func(t *testing.T) {
95+
mux := http.NewServeMux()
96+
97+
cfg := config.DefaultConfig()
98+
cfg.Node.Aggregator = true
99+
if tc.blockTime > 0 {
100+
cfg.Node.BlockTime = config.DurationWrapper{Duration: tc.blockTime}
101+
}
102+
cfg.Node.LazyMode = tc.lazy
103+
if tc.lazy {
104+
cfg.Node.LazyBlockInterval = config.DurationWrapper{Duration: tc.lazyInterval}
105+
}
106+
107+
mockStore := mocks.NewMockStore(t)
108+
state := types.State{
109+
LastBlockHeight: 10,
110+
LastBlockTime: time.Now().Add(-tc.delay),
111+
}
112+
mockStore.On("GetState", mock.Anything).Return(state, nil)
113+
114+
bestKnownHeightProvider := func() uint64 { return state.LastBlockHeight }
115+
116+
RegisterCustomHTTPEndpoints(mux, mockStore, nil, cfg, bestKnownHeightProvider, logger)
117+
118+
ts := httptest.NewServer(mux)
119+
t.Cleanup(ts.Close)
120+
121+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/health/ready", nil)
122+
require.NoError(t, err)
123+
resp, err := http.DefaultClient.Do(req)
124+
require.NoError(t, err)
125+
t.Cleanup(func() { _ = resp.Body.Close() })
126+
127+
body, err := io.ReadAll(resp.Body)
128+
require.NoError(t, err)
129+
130+
assert.Equal(t, tc.expStatusCode, resp.StatusCode)
131+
assert.Equal(t, tc.expBody, string(body))
132+
})
133+
}
134+
}

0 commit comments

Comments
 (0)