@@ -13,7 +13,10 @@ import (
1313 "github.com/labstack/echo/v5"
1414)
1515
16- func setupEcho (cfg ConcurrencyConfig , gate chan struct {}) (* echo.Echo , echo.MiddlewareFunc ) {
16+ func setupEcho (cfg ConcurrencyConfig , gate chan struct {}) (* echo.Echo , * ConcurrencyMetrics ) {
17+ if cfg .Metrics == nil {
18+ cfg .Metrics = & ConcurrencyMetrics {}
19+ }
1720 e := echo .New ()
1821 mw := ConcurrencyLimiter (cfg )
1922
@@ -25,7 +28,7 @@ func setupEcho(cfg ConcurrencyConfig, gate chan struct{}) (*echo.Echo, echo.Midd
2528 }
2629
2730 e .POST ("/v1/run" , h , mw )
28- return e , mw
31+ return e , cfg . Metrics
2932}
3033
3134func doRequest (e * echo.Echo ) * httptest.ResponseRecorder {
@@ -42,8 +45,18 @@ func doRequestWithContext(e *echo.Echo, ctx context.Context) *httptest.ResponseR
4245 return rec
4346}
4447
48+ func assertMetricsZero (t * testing.T , metrics * ConcurrencyMetrics ) {
49+ t .Helper ()
50+ if v := metrics .Active .Load (); v != 0 {
51+ t .Errorf ("expected Active=0 after completion, got %d" , v )
52+ }
53+ if v := metrics .Queued .Load (); v != 0 {
54+ t .Errorf ("expected Queued=0 after completion, got %d" , v )
55+ }
56+ }
57+
4558func TestConcurrencyLimiter_UnderLimit (t * testing.T ) {
46- e , _ := setupEcho (ConcurrencyConfig {
59+ e , metrics := setupEcho (ConcurrencyConfig {
4760 MaxConcurrency : 2 ,
4861 MaxQueueSize : 5 ,
4962 QueueTimeout : 5 * time .Second ,
@@ -53,11 +66,12 @@ func TestConcurrencyLimiter_UnderLimit(t *testing.T) {
5366 if rec .Code != http .StatusOK {
5467 t .Fatalf ("expected 200, got %d" , rec .Code )
5568 }
69+ assertMetricsZero (t , metrics )
5670}
5771
5872func TestConcurrencyLimiter_QueueAndSucceed (t * testing.T ) {
5973 gate := make (chan struct {})
60- e , _ := setupEcho (ConcurrencyConfig {
74+ e , metrics := setupEcho (ConcurrencyConfig {
6175 MaxConcurrency : 1 ,
6276 MaxQueueSize : 5 ,
6377 QueueTimeout : 5 * time .Second ,
@@ -98,11 +112,12 @@ func TestConcurrencyLimiter_QueueAndSucceed(t *testing.T) {
98112 if queuedRec .Code != http .StatusOK {
99113 t .Fatalf ("queued request: expected 200, got %d" , queuedRec .Code )
100114 }
115+ assertMetricsZero (t , metrics )
101116}
102117
103118func TestConcurrencyLimiter_QueueFull (t * testing.T ) {
104119 gate := make (chan struct {})
105- e , _ := setupEcho (ConcurrencyConfig {
120+ e , metrics := setupEcho (ConcurrencyConfig {
106121 MaxConcurrency : 1 ,
107122 MaxQueueSize : 1 ,
108123 QueueTimeout : 5 * time .Second ,
@@ -142,11 +157,12 @@ func TestConcurrencyLimiter_QueueFull(t *testing.T) {
142157 // Cleanup: release gate and wait.
143158 close (gate )
144159 wg .Wait ()
160+ assertMetricsZero (t , metrics )
145161}
146162
147163func TestConcurrencyLimiter_QueueTimeout (t * testing.T ) {
148164 gate := make (chan struct {})
149- e , _ := setupEcho (ConcurrencyConfig {
165+ e , metrics := setupEcho (ConcurrencyConfig {
150166 MaxConcurrency : 1 ,
151167 MaxQueueSize : 5 ,
152168 QueueTimeout : 100 * time .Millisecond ,
@@ -178,11 +194,12 @@ func TestConcurrencyLimiter_QueueTimeout(t *testing.T) {
178194 // Cleanup.
179195 close (gate )
180196 wg .Wait ()
197+ assertMetricsZero (t , metrics )
181198}
182199
183200func TestConcurrencyLimiter_ClientCancel (t * testing.T ) {
184201 gate := make (chan struct {})
185- e , _ := setupEcho (ConcurrencyConfig {
202+ e , metrics := setupEcho (ConcurrencyConfig {
186203 MaxConcurrency : 1 ,
187204 MaxQueueSize : 5 ,
188205 QueueTimeout : 10 * time .Second ,
@@ -227,4 +244,5 @@ func TestConcurrencyLimiter_ClientCancel(t *testing.T) {
227244 // Cleanup.
228245 close (gate )
229246 wg .Wait ()
247+ assertMetricsZero (t , metrics )
230248}
0 commit comments