1616import java .util .Iterator ;
1717import java .util .List ;
1818import java .util .Set ;
19+ import java .util .concurrent .ExecutorService ;
20+ import java .util .concurrent .Executors ;
1921import java .util .concurrent .TimeUnit ;
22+ import java .util .concurrent .atomic .AtomicInteger ;
2023import org .awaitility .Awaitility ;
2124import org .junit .After ;
2225import org .junit .Before ;
@@ -64,23 +67,30 @@ public void test() throws Throwable {
6467 .willReturn (
6568 WireMock .aResponse ().withStatus (503 ).withBody ("fail" ).withUniformRandomDelay (100 , 1_000 )));
6669
70+ int requestsPerSecond = 10 ;
71+ int numClients = 10 ;
72+ int timeToRun = 90_000 ;
73+ int timeToRestore = 30_000 ;
74+
6775 long start = System .currentTimeMillis ();
6876 boolean upAgain = false ;
69- int id = 0 ;
77+ final AtomicInteger id = new AtomicInteger ( 0 ) ;
7078 List <String > ids = new ArrayList <>();
71- RateLimiter rate = RateLimiter .create (5 );
72- while (System .currentTimeMillis () - start < 60_000 ) {
79+
80+ RateLimiter rate = RateLimiter .create (requestsPerSecond );
81+ ExecutorService exec = Executors .newWorkStealingPool (numClients );
82+
83+ while (System .currentTimeMillis () - start < timeToRun ) {
7384 if (rate .tryAcquire ()) {
74- String msgid = "m" + id ++;
75- ids .add (msgid );
76- analytics .enqueue (
77- TrackMessage .builder ("my-track" ).messageId (msgid ).userId ("userId" ));
78- System .err .println ("enqued " + msgid );
85+ exec .submit (() -> {
86+ String msgid = "m" + id .getAndIncrement ();
87+ ids .add (msgid );
88+ analytics .enqueue (
89+ TrackMessage .builder ("my-track" ).messageId (msgid ).userId ("userId" ));
90+ });
7991 }
80-
81- Thread .sleep (50 );
82-
83- if (!upAgain && System .currentTimeMillis () - start > 20_000 ) {
92+ Thread .sleep (1 );
93+ if (!upAgain && System .currentTimeMillis () - start > timeToRestore ) {
8494 upAgain = true ;
8595 stubFor (post (urlEqualTo ("/v1/import/" ))
8696 .willReturn (okJson ("{\" success\" : \" true\" }" ).withUniformRandomDelay (100 , 1_000 )));
@@ -92,6 +102,9 @@ public void test() throws Throwable {
92102 .atMost (10 , TimeUnit .MINUTES )
93103 .pollInterval (1 , TimeUnit .SECONDS )
94104 .until (() -> sentMessagesEqualsTo (ids .toArray (new String [ids .size ()])));
105+
106+ exec .shutdownNow ();
107+ exec .awaitTermination (10 , TimeUnit .SECONDS );
95108 }
96109
97110 private static final ObjectMapper OM = new ObjectMapper ();
0 commit comments