@@ -79,6 +79,25 @@ void cleanUp() {
79
79
}
80
80
}
81
81
82
+ @ ParameterizedTest
83
+ @ EnumSource (StatsdProtocol .class )
84
+ void receiveAllBufferedMetricsAfterCloseSuccessfully (StatsdProtocol protocol ) throws InterruptedException {
85
+ skipUdsTestOnWindows (protocol );
86
+ serverLatch = new CountDownLatch (3 );
87
+ server = startServer (protocol , 0 );
88
+
89
+ final int port = getPort (protocol );
90
+ meterRegistry = new StatsdMeterRegistry (getBufferedConfig (protocol , port ), Clock .SYSTEM );
91
+ startRegistryAndWaitForClient ();
92
+ Thread .sleep (1000 );
93
+ Counter counter = Counter .builder ("my.counter" ).register (meterRegistry );
94
+ counter .increment ();
95
+ counter .increment ();
96
+ counter .increment ();
97
+ meterRegistry .close ();
98
+ assertThat (serverLatch .await (5 , TimeUnit .SECONDS )).isTrue ();
99
+ }
100
+
82
101
@ ParameterizedTest
83
102
@ EnumSource (StatsdProtocol .class )
84
103
void receiveMetricsSuccessfully (StatsdProtocol protocol ) throws InterruptedException {
@@ -336,11 +355,14 @@ private DisposableChannel startServer(StatsdProtocol protocol, int port) {
336
355
return UdpServer .create ()
337
356
.bindAddress (() -> protocol == StatsdProtocol .UDP
338
357
? InetSocketAddress .createUnresolved ("localhost" , port ) : newDomainSocketAddress ())
339
- .handle ((in , out ) -> in .receive ().asString ().flatMap (packet -> {
340
- serverLatch .countDown ();
341
- serverMetricReadCount .getAndIncrement ();
342
- return Flux .never ();
343
- }))
358
+ .handle ((in , out ) -> in .receive ()
359
+ .asString ()
360
+ .flatMap (packet -> Flux .just (packet .split ("\n " )))
361
+ .flatMap (packetLine -> {
362
+ serverLatch .countDown ();
363
+ serverMetricReadCount .getAndIncrement ();
364
+ return Flux .never ();
365
+ }))
344
366
.doOnBound ((server ) -> bound = true )
345
367
.doOnUnbound ((server ) -> bound = false )
346
368
.wiretap ("udpserver" , LogLevel .INFO )
@@ -351,14 +373,17 @@ else if (protocol == StatsdProtocol.TCP) {
351
373
return TcpServer .create ()
352
374
.host ("localhost" )
353
375
.port (port )
354
- .handle ((in , out ) -> in .receive ().asString ().flatMap (packet -> {
355
- IntStream .range (0 , packet .split ("my.counter" ).length - 1 ).forEach (i -> {
356
- serverLatch .countDown ();
357
- serverMetricReadCount .getAndIncrement ();
358
- });
359
- in .withConnection (channel ::set );
360
- return Flux .never ();
361
- }))
376
+ .handle ((in , out ) -> in .receive ()
377
+ .asString ()
378
+ .flatMap (packet -> Flux .just (packet .split ("\n " )))
379
+ .flatMap (packetLine -> {
380
+ IntStream .range (0 , packetLine .split ("my.counter" ).length - 1 ).forEach (i -> {
381
+ serverLatch .countDown ();
382
+ serverMetricReadCount .getAndIncrement ();
383
+ });
384
+ in .withConnection (channel ::set );
385
+ return Flux .never ();
386
+ }))
362
387
.doOnBound ((server ) -> bound = true )
363
388
.doOnUnbound ((server ) -> {
364
389
bound = false ;
@@ -388,6 +413,14 @@ private static DomainSocketAddress newDomainSocketAddress() {
388
413
}
389
414
390
415
private StatsdConfig getUnbufferedConfig (StatsdProtocol protocol , int port ) {
416
+ return getConfig (protocol , port , false );
417
+ }
418
+
419
+ private StatsdConfig getBufferedConfig (StatsdProtocol protocol , int port ) {
420
+ return getConfig (protocol , port , true );
421
+ }
422
+
423
+ private StatsdConfig getConfig (StatsdProtocol protocol , int port , boolean buffered ) {
391
424
return new StatsdConfig () {
392
425
@ Override
393
426
public String get (String key ) {
@@ -411,7 +444,7 @@ public StatsdProtocol protocol() {
411
444
412
445
@ Override
413
446
public boolean buffered () {
414
- return false ;
447
+ return buffered ;
415
448
}
416
449
};
417
450
}
0 commit comments