22
22
#include " util/MockPrometheus.hpp"
23
23
#include " util/TestHttpSyncClient.hpp"
24
24
#include " util/config/Config.hpp"
25
+ #include " util/prometheus/Gauge.hpp"
25
26
#include " util/prometheus/Label.hpp"
26
27
#include " util/prometheus/Prometheus.hpp"
27
28
#include " web/DOSGuard.hpp"
42
43
#include < boost/json/parse.hpp>
43
44
#include < boost/system/system_error.hpp>
44
45
#include < fmt/core.h>
46
+ #include < gmock/gmock.h>
45
47
#include < gtest/gtest.h>
46
48
47
49
#include < chrono>
@@ -202,6 +204,8 @@ class WebServerTest : public NoLoggerFixture {
202
204
std::optional<std::thread> runner;
203
205
};
204
206
207
+ struct WebServerTestsWithMockPrometheus : WebServerTest, prometheus::WithMockPrometheus {};
208
+
205
209
class EchoExecutor {
206
210
public:
207
211
void
@@ -263,16 +267,21 @@ makeServerSync(
263
267
264
268
} // namespace
265
269
266
- TEST_F (WebServerTest , Http)
270
+ TEST_F (WebServerTestsWithMockPrometheus , Http)
267
271
{
268
272
auto e = std::make_shared<EchoExecutor>();
269
273
auto const server = makeServerSync (cfg, ctx, std::nullopt, dosGuard, e);
270
274
auto const res = HttpSyncClient::syncPost (" localhost" , port, R"( {"Hello":1})" );
271
275
EXPECT_EQ (res, R"( {"Hello":1})" );
272
276
}
273
277
274
- TEST_F (WebServerTest , Ws)
278
+ TEST_F (WebServerTestsWithMockPrometheus , Ws)
275
279
{
280
+ ::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
281
+ makeMock<util::prometheus::GaugeInt>(" ws_messages_length" , " " );
282
+ EXPECT_CALL (wsMessagesCounterMock, add (1 ));
283
+ EXPECT_CALL (wsMessagesCounterMock, add (-1 ));
284
+
276
285
auto e = std::make_shared<EchoExecutor>();
277
286
auto const server = makeServerSync (cfg, ctx, std::nullopt, dosGuard, e);
278
287
WebSocketSyncClient wsClient;
@@ -282,7 +291,7 @@ TEST_F(WebServerTest, Ws)
282
291
wsClient.disconnect ();
283
292
}
284
293
285
- TEST_F (WebServerTest , HttpInternalError)
294
+ TEST_F (WebServerTestsWithMockPrometheus , HttpInternalError)
286
295
{
287
296
auto e = std::make_shared<ExceptionExecutor>();
288
297
auto const server = makeServerSync (cfg, ctx, std::nullopt, dosGuard, e);
@@ -293,8 +302,13 @@ TEST_F(WebServerTest, HttpInternalError)
293
302
);
294
303
}
295
304
296
- TEST_F (WebServerTest , WsInternalError)
305
+ TEST_F (WebServerTestsWithMockPrometheus , WsInternalError)
297
306
{
307
+ ::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
308
+ makeMock<util::prometheus::GaugeInt>(" ws_messages_length" , " " );
309
+ EXPECT_CALL (wsMessagesCounterMock, add (1 ));
310
+ EXPECT_CALL (wsMessagesCounterMock, add (-1 ));
311
+
298
312
auto e = std::make_shared<ExceptionExecutor>();
299
313
auto const server = makeServerSync (cfg, ctx, std::nullopt, dosGuard, e);
300
314
WebSocketSyncClient wsClient;
@@ -307,8 +321,13 @@ TEST_F(WebServerTest, WsInternalError)
307
321
);
308
322
}
309
323
310
- TEST_F (WebServerTest , WsInternalErrorNotJson)
324
+ TEST_F (WebServerTestsWithMockPrometheus , WsInternalErrorNotJson)
311
325
{
326
+ ::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
327
+ makeMock<util::prometheus::GaugeInt>(" ws_messages_length" , " " );
328
+ EXPECT_CALL (wsMessagesCounterMock, add (1 ));
329
+ EXPECT_CALL (wsMessagesCounterMock, add (-1 ));
330
+
312
331
auto e = std::make_shared<ExceptionExecutor>();
313
332
auto const server = makeServerSync (cfg, ctx, std::nullopt, dosGuard, e);
314
333
WebSocketSyncClient wsClient;
@@ -321,7 +340,7 @@ TEST_F(WebServerTest, WsInternalErrorNotJson)
321
340
);
322
341
}
323
342
324
- TEST_F (WebServerTest , Https)
343
+ TEST_F (WebServerTestsWithMockPrometheus , Https)
325
344
{
326
345
auto e = std::make_shared<EchoExecutor>();
327
346
auto sslCtx = parseCertsForTest ();
@@ -331,8 +350,13 @@ TEST_F(WebServerTest, Https)
331
350
EXPECT_EQ (res, R"( {"Hello":1})" );
332
351
}
333
352
334
- TEST_F (WebServerTest , Wss)
353
+ TEST_F (WebServerTestsWithMockPrometheus , Wss)
335
354
{
355
+ ::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
356
+ makeMock<util::prometheus::GaugeInt>(" ws_messages_length" , " " );
357
+ EXPECT_CALL (wsMessagesCounterMock, add (1 ));
358
+ EXPECT_CALL (wsMessagesCounterMock, add (-1 ));
359
+
336
360
auto e = std::make_shared<EchoExecutor>();
337
361
auto sslCtx = parseCertsForTest ();
338
362
auto const ctxSslRef = sslCtx ? std::optional<std::reference_wrapper<ssl::context>>{sslCtx.value ()} : std::nullopt;
@@ -345,7 +369,7 @@ TEST_F(WebServerTest, Wss)
345
369
wsClient.disconnect ();
346
370
}
347
371
348
- TEST_F (WebServerTest , HttpRequestOverload)
372
+ TEST_F (WebServerTestsWithMockPrometheus , HttpRequestOverload)
349
373
{
350
374
auto e = std::make_shared<EchoExecutor>();
351
375
auto const server = makeServerSync (cfg, ctx, std::nullopt, dosGuardOverload, e);
@@ -358,8 +382,13 @@ TEST_F(WebServerTest, HttpRequestOverload)
358
382
);
359
383
}
360
384
361
- TEST_F (WebServerTest , WsRequestOverload)
385
+ TEST_F (WebServerTestsWithMockPrometheus , WsRequestOverload)
362
386
{
387
+ ::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
388
+ makeMock<util::prometheus::GaugeInt>(" ws_messages_length" , " " );
389
+ EXPECT_CALL (wsMessagesCounterMock, add (1 )).Times (2 );
390
+ EXPECT_CALL (wsMessagesCounterMock, add (-1 )).Times (2 );
391
+
363
392
auto e = std::make_shared<EchoExecutor>();
364
393
auto const server = makeServerSync (cfg, ctx, std::nullopt, dosGuardOverload, e);
365
394
WebSocketSyncClient wsClient;
@@ -377,7 +406,7 @@ TEST_F(WebServerTest, WsRequestOverload)
377
406
);
378
407
}
379
408
380
- TEST_F (WebServerTest , HttpPayloadOverload)
409
+ TEST_F (WebServerTestsWithMockPrometheus , HttpPayloadOverload)
381
410
{
382
411
std::string const s100 (100 , ' a' );
383
412
auto e = std::make_shared<EchoExecutor>();
@@ -389,8 +418,13 @@ TEST_F(WebServerTest, HttpPayloadOverload)
389
418
);
390
419
}
391
420
392
- TEST_F (WebServerTest , WsPayloadOverload)
421
+ TEST_F (WebServerTestsWithMockPrometheus , WsPayloadOverload)
393
422
{
423
+ ::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
424
+ makeMock<util::prometheus::GaugeInt>(" ws_messages_length" , " " );
425
+ EXPECT_CALL (wsMessagesCounterMock, add (1 ));
426
+ EXPECT_CALL (wsMessagesCounterMock, add (-1 ));
427
+
394
428
std::string const s100 (100 , ' a' );
395
429
auto e = std::make_shared<EchoExecutor>();
396
430
auto server = makeServerSync (cfg, ctx, std::nullopt, dosGuardOverload, e);
@@ -404,7 +438,7 @@ TEST_F(WebServerTest, WsPayloadOverload)
404
438
);
405
439
}
406
440
407
- TEST_F (WebServerTest , WsTooManyConnection)
441
+ TEST_F (WebServerTestsWithMockPrometheus , WsTooManyConnection)
408
442
{
409
443
auto e = std::make_shared<EchoExecutor>();
410
444
auto server = makeServerSync (cfg, ctx, std::nullopt, dosGuardOverload, e);
@@ -510,10 +544,17 @@ struct WebServerAdminTestParams {
510
544
std::string expectedResponse;
511
545
};
512
546
513
- class WebServerAdminTest : public WebServerTest , public ::testing::WithParamInterface<WebServerAdminTestParams> {};
547
+ class WebServerAdminTest : public WebServerTest ,
548
+ public ::testing::WithParamInterface<WebServerAdminTestParams>,
549
+ public prometheus::WithMockPrometheus {};
514
550
515
551
TEST_P (WebServerAdminTest, WsAdminCheck)
516
552
{
553
+ ::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
554
+ makeMock<util::prometheus::GaugeInt>(" ws_messages_length" , " " );
555
+ EXPECT_CALL (wsMessagesCounterMock, add (1 ));
556
+ EXPECT_CALL (wsMessagesCounterMock, add (-1 ));
557
+
517
558
auto e = std::make_shared<AdminCheckExecutor>();
518
559
Config const serverConfig{parse (GetParam ().config )};
519
560
auto server = makeServerSync (serverConfig, ctx, std::nullopt, dosGuardOverload, e);
0 commit comments