@@ -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
1619func 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