1111import java .util .Set ;
1212import java .util .concurrent .Semaphore ;
1313import java .util .concurrent .TimeoutException ;
14+ import java .util .concurrent .atomic .AtomicInteger ;
1415import org .junit .After ;
1516import org .junit .Before ;
1617import org .junit .Test ;
@@ -42,8 +43,8 @@ public class EventSendBufferTest {
4243 private long sendDuration ;
4344 private Semaphore sendStartSemaphore ; // released when a send starts
4445 private Exception sendException ;
45- private volatile int sendOperationsStarted ;
46- private volatile int sendOperationsFinished ;
46+ private AtomicInteger sendOperationsStarted ;
47+ private AtomicInteger sendOperationsFinished ;
4748
4849 @ Captor
4950 private ArgumentCaptor <LoggableEvent []> savedEvents ;
@@ -64,18 +65,18 @@ public void setUp() throws IOException {
6465 sendDuration = 0 ;
6566 sendStartSemaphore = new Semaphore (0 );
6667 sendException = null ;
67- sendOperationsStarted = 0 ;
68- sendOperationsFinished = 0 ;
68+ sendOperationsStarted = new AtomicInteger ( 0 ) ;
69+ sendOperationsFinished = new AtomicInteger ( 0 ) ;
6970 when (serverAccess .getSendEventLogJob (spywareServerUrl .capture (), sentEvents .capture ())).thenReturn (new CancellableCallable <Object >() {
7071 @ Override
7172 public Object call () throws Exception {
72- sendOperationsStarted ++ ;
73+ sendOperationsStarted . incrementAndGet () ;
7374 sendStartSemaphore .release ();
7475 Thread .sleep (sendDuration );
7576 if (sendException != null ) {
7677 throw sendException ;
7778 }
78- sendOperationsFinished ++ ;
79+ sendOperationsFinished . incrementAndGet () ;
7980 return null ;
8081 }
8182
@@ -165,7 +166,7 @@ public void autosendsPeriodically() throws InterruptedException {
165166 sender .receiveEvent (ev2 );
166167 Thread .sleep (250 );
167168
168- assertTrue (sendOperationsFinished >= 2 );
169+ assertTrue (sendOperationsFinished . get () >= 2 );
169170 }
170171
171172 @ Test
@@ -192,12 +193,12 @@ public void autosendsWhenNumberOfEventsGoesOverThreshold() throws TimeoutExcepti
192193 sender .receiveEvent (ev1 );
193194 sender .receiveEvent (ev2 );
194195 Thread .sleep (50 );
195- assertEquals (0 , sendOperationsFinished );
196+ assertEquals (0 , sendOperationsFinished . get () );
196197
197198 sender .receiveEvent (ev3 );
198199 sender .waitUntilCurrentSendingFinished (1000 );
199200
200- assertEquals (1 , sendOperationsFinished );
201+ assertEquals (1 , sendOperationsFinished . get () );
201202 LoggableEvent [] expecteds = new LoggableEvent [] { ev1 , ev2 , ev3 };
202203 assertArrayEquals (expecteds , sentEvents .getValue ().toArray (new LoggableEvent [0 ]));
203204 }
@@ -214,12 +215,26 @@ public void autosendingWhenOverThresholdHasACooldown() throws TimeoutException,
214215 sender .receiveEvent (ev4 );
215216 sender .waitUntilCurrentSendingFinished (1000 );
216217
217- assertEquals (1 , sendOperationsStarted );
218+ assertEquals (1 , sendOperationsStarted . get () );
218219 LoggableEvent [] expecteds = new LoggableEvent [] { ev1 , ev2 , ev3 };
219220 assertArrayEquals (expecteds , sentEvents .getValue ().toArray (new LoggableEvent [0 ]));
220221 }
221222
222- @ Test
223+ @ Test // Issue #125
224+ public void retryLoopRespectAutosendIntervalOnFailureEvenIfThereIsMoreToSend () throws TimeoutException , InterruptedException {
225+ sendException = new RuntimeException ("Sending failed" );
226+ sender .setMaxEventsPerSend (2 );
227+ sender .receiveEvent (ev1 );
228+ sender .receiveEvent (ev2 );
229+ sender .receiveEvent (ev3 );
230+ sender .sendNow ();
231+ sender .waitUntilCurrentSendingFinished (1000 );
232+
233+ assertEquals (1 , sendOperationsStarted .get ());
234+ assertEquals (0 , sendOperationsFinished .get ());
235+ }
236+
237+ @ Test // FIXME: this test appears to be flaky
223238 public void discardsOldestEventsOnOverflow () throws TimeoutException , InterruptedException {
224239 sender .setMaxEvents (3 );
225240
@@ -251,7 +266,7 @@ public void sendsEventsReceivedDuringSendingInSubsequentSend() throws TimeoutExc
251266 sender .sendNow ();
252267 sender .waitUntilCurrentSendingFinished (1000 );
253268
254- assertEquals (2 , sendOperationsFinished );
269+ assertEquals (2 , sendOperationsFinished . get () );
255270 assertEquals (2 , sentEvents .getAllValues ().size ());
256271
257272 assertArrayEquals (new LoggableEvent [] { ev1 }, sentEvents .getAllValues ().get (0 ).toArray (new LoggableEvent [0 ]));
@@ -286,7 +301,7 @@ public void toleratesOverflowDuringSending() throws TimeoutException, Interrupte
286301 sender .sendNow ();
287302 sender .waitUntilCurrentSendingFinished (1000 );
288303
289- assertEquals (3 , sendOperationsFinished );
304+ assertEquals (3 , sendOperationsFinished . get () );
290305 assertEquals (3 , sentEvents .getAllValues ().size ());
291306
292307 assertArrayEquals (new LoggableEvent [] { ev1 }, sentEvents .getAllValues ().get (0 ).toArray (new LoggableEvent [0 ]));
@@ -320,7 +335,7 @@ public void retainsEventsForNextSendIfSendingFails() throws TimeoutException, In
320335 sender .waitUntilCurrentSendingFinished (1000 );
321336
322337 assertEquals (2 , sendStartSemaphore .availablePermits ());
323- assertEquals (1 , sendOperationsFinished );
338+ assertEquals (1 , sendOperationsFinished . get () );
324339 assertArrayEquals (new LoggableEvent [] { ev1 }, sentEvents .getValue ().toArray (new LoggableEvent [0 ]));
325340
326341 Thread .sleep (100 ); // Wait for save
0 commit comments