4343import java .util .List ;
4444import java .util .Map ;
4545import java .util .concurrent .Semaphore ;
46+ import java .util .concurrent .TimeUnit ;
4647
4748import static org .mockito .AdditionalMatchers .not ;
4849import static org .mockito .Matchers .eq ;
@@ -74,23 +75,34 @@ public void setUp() {
7475 * Test that read writes happened sequentially when calling sendNow().
7576 */
7677 @ Test
77- public void shouldWriteRequestAndSendInSequence () {
78+ public void shouldWriteRequestAndSendInSequence () throws InterruptedException {
7879 // Given a request.
7980 Map <String , Object > params = new HashMap <>();
8081 params .put ("data1" , "value1" );
8182 params .put ("data2" , "value2" );
82- ThreadRequestSequence waiter = new ThreadRequestSequence ();
83- Request request = new Request (POST , Constants .Methods .START , params , waiter );
83+ final ThreadRequestSequence threadRequestSequence = new ThreadRequestSequence ();
84+ Request request = new Request (POST , Constants .Methods .START , params , threadRequestSequence );
8485 request .setAppId ("fskadfshdbfa" , "wee5w4waer422323" );
8586
86- //block the write
87- waiter .setBlockWrite (true );
87+ new Thread (new Runnable () {
88+ @ Override
89+ public void run () {
90+ try {
91+ Thread .sleep (100 );
92+ } catch (InterruptedException e ) {
93+ throw new RuntimeException (e );
94+ }
95+ threadRequestSequence .writeSemaphore .release (1 );
96+ }
97+ }).start ();
8898
8999 // When the request is sent.
90100 request .sendIfConnected ();
91101
102+ threadRequestSequence .testThreadSemaphore .tryAcquire (5000 , TimeUnit .MILLISECONDS );
103+
92104 // When the request is sent.
93- waiter .assertCallSequence ();
105+ threadRequestSequence .assertCallSequence ();
94106 }
95107 /**
96108 * Tests the testRemoveIrrelevantBackgroundStartRequests method.
@@ -350,36 +362,36 @@ private List<Map<String, Object>> mockRequests(int requestSize) {
350362
351363 private static class ThreadRequestSequence implements RequestSequence {
352364 Instant beforeReadTime , afterReadTime , beforeWriteTime , afterWriteTime ;
353- final Semaphore semaphore = new Semaphore (1 );
354- private boolean blockWrite = false ;
365+ final Semaphore writeSemaphore = new Semaphore (0 );
366+ final Semaphore readSemaphore = new Semaphore (1 );
367+ final Semaphore testThreadSemaphore = new Semaphore (0 );
355368
356369 @ Override
357370 public void beforeRead () {
358371 try {
359- semaphore .tryAcquire ();
372+ readSemaphore .tryAcquire ();
360373 beforeReadTime = Instant .now ();
361374 } finally {
362- semaphore .release ();
375+ readSemaphore .release ();
363376 }
364377 }
365378
366379 @ Override
367380 public void afterRead () {
368381 afterReadTime = Instant .now ();
382+ testThreadSemaphore .release (1 );
369383 }
370384
371385 @ Override
372386 public void beforeWrite () {
373387 // since we are blocking on main thread
374- if (blockWrite ) {
375- try {
376- semaphore .tryAcquire ();
377- Thread .sleep (2000 );
378- } catch (InterruptedException e ) {
379- e .printStackTrace ();
380- } finally {
381- semaphore .release ();
382- }
388+ try {
389+ writeSemaphore .tryAcquire ();
390+ Thread .sleep (2000 );
391+ } catch (InterruptedException e ) {
392+ throw new RuntimeException (e );
393+ } finally {
394+ writeSemaphore .release ();
383395 }
384396 beforeWriteTime = Instant .now ();
385397 }
@@ -389,12 +401,11 @@ public void afterWrite() {
389401 afterWriteTime = Instant .now ();
390402 }
391403
392- public void setBlockWrite (boolean value ) {
393- this .blockWrite = value ;
394- }
395-
396- public void assertCallSequence () {
397- assertTrue (afterWriteTime .isBefore (beforeReadTime ));
404+ void assertCallSequence () {
405+ assertTrue (
406+ beforeWriteTime .isBefore (afterWriteTime )
407+ && beforeReadTime .isBefore (afterReadTime )
408+ && beforeReadTime .isAfter (afterWriteTime ));
398409 }
399410 }
400411}
0 commit comments