6
6
*/
7
7
package org .gridsuite .study .notification .server ;
8
8
9
- import io .micrometer .core .instrument .Gauge ;
10
9
import io .micrometer .core .instrument .MeterRegistry ;
11
10
import org .junit .Test ;
12
11
import org .junit .runner .RunWith ;
22
21
import reactor .core .publisher .Mono ;
23
22
24
23
import java .net .URI ;
24
+ import java .util .Map ;
25
25
26
26
import static org .gridsuite .study .notification .server .NotificationWebSocketHandler .*;
27
27
import static org .junit .Assert .assertEquals ;
28
- import static org .junit .Assert .assertNotNull ;
29
28
30
29
/**
31
30
* @author Jon Harper <jon.harper at rte-france.com>
@@ -50,27 +49,47 @@ public void echo() {
50
49
client .execute (getUrl ("/notify" ), httpHeaders , ws -> Mono .empty ()).block ();
51
50
}
52
51
53
- @ Test
54
- public void metrics () {
55
- testMeters (0 );
56
- WebSocketClient client = new StandardWebSocketClient ();
57
- HttpHeaders httpHeaders = new HttpHeaders ();
58
- httpHeaders .add (HEADER_USER_ID , "test" );
59
- client .execute (getUrl ("/notify" ), httpHeaders , ws -> Mono .fromRunnable (() -> testMeters (1 ))).block ();
60
- }
61
-
62
52
protected URI getUrl (String path ) {
63
53
return URI .create ("ws://localhost:" + this .port + path );
64
54
}
65
55
66
- private void testMeters (int val ) {
67
- testMeter (USERS_METER_NAME , val );
68
- testMeter (CONNECTIONS_METER_NAME , val );
56
+ @ Test
57
+ public void metricsMapOneUserTwoConnections () {
58
+ WebSocketClient client1 = new StandardWebSocketClient ();
59
+ HttpHeaders httpHeaders1 = new HttpHeaders ();
60
+ String user = "test" ;
61
+ httpHeaders1 .add (HEADER_USER_ID , user );
62
+ Map <String , Double > exp = Map .of (user , 2d );
63
+ Mono <Void > connection1 = client1 .execute (getUrl ("/notify" ), httpHeaders1 , ws -> Mono .fromRunnable (() -> testMeterMap (exp )));
64
+ Mono <Void > connection2 = client1 .execute (getUrl ("/notify" ), httpHeaders1 , ws -> Mono .fromRunnable (() -> testMeterMap (exp )));
65
+
66
+ Mono .zip (connection1 , connection2 ).block ();
67
+ }
68
+
69
+ @ Test
70
+ public void metricsMapTwoUsers () {
71
+ // First WebSocketClient for connections related to 'test' user
72
+ WebSocketClient client1 = new StandardWebSocketClient ();
73
+ HttpHeaders httpHeaders1 = new HttpHeaders ();
74
+ String user1 = "test" ;
75
+ httpHeaders1 .add (HEADER_USER_ID , user1 );
76
+ String user2 = "test1" ;
77
+ Map <String , Double > exp = Map .of (user1 , 2d , user2 , 1d );
78
+ Mono <Void > connection1 = client1 .execute (getUrl ("/notify" ), httpHeaders1 , ws -> Mono .fromRunnable (() -> testMeterMap (exp )));
79
+ Mono <Void > connection2 = client1 .execute (getUrl ("/notify" ), httpHeaders1 , ws -> Mono .fromRunnable (() -> testMeterMap (exp )));
80
+
81
+ // Second WebSocketClient for connections related to 'test1' user
82
+ WebSocketClient client2 = new StandardWebSocketClient ();
83
+ HttpHeaders httpHeaders2 = new HttpHeaders ();
84
+ httpHeaders2 .add (HEADER_USER_ID , user2 );
85
+ Mono <Void > connection3 = client2 .execute (getUrl ("/notify" ), httpHeaders2 , ws -> Mono .fromRunnable (() -> testMeterMap (exp )));
86
+
87
+ Mono .zip (connection1 , connection2 , connection3 ).block ();
69
88
}
70
89
71
- private void testMeter ( String name , int val ) {
72
- Gauge meter = meterRegistry . get ( name ). gauge ();
73
- assertNotNull ( meter );
74
- assertEquals ( val , Double . valueOf ( meter . value ()). intValue ());
90
+ private void testMeterMap ( Map < String , Double > userMap ) {
91
+ for ( Map . Entry < String , Double > userEntry : userMap . entrySet ()) {
92
+ assertEquals ( userEntry . getValue (), meterRegistry . get ( USERS_METER_NAME ). tag ( USER_TAG , userEntry . getKey ()). gauge (). value (), 0 );
93
+ }
75
94
}
76
95
}
0 commit comments