Skip to content

Commit 7b4afa1

Browse files
committed
add tests for Future implementation of AsyncWork and SynchronizationPoint
1 parent 313861c commit 7b4afa1

File tree

4 files changed

+214
-29
lines changed

4 files changed

+214
-29
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/concurrent/synch/AsyncWork.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -309,19 +309,19 @@ public void unblockSuccess(T result) {
309309
listeners = listenersInline;
310310
listenersInline = new ArrayList<>(2);
311311
}
312+
Application app = LCCore.getApplication();
313+
Logger log = app.isReleaseMode() ? null : app.getLoggerFactory().getLogger(SynchronizationPoint.class);
312314
while (true) {
313-
Application app = LCCore.getApplication();
314-
Logger log = app.isReleaseMode() ? null : app.getLoggerFactory().getLogger(SynchronizationPoint.class);
315-
if (log == null || !log.debug())
315+
if (!log.debug())
316316
for (int i = 0; i < listeners.size(); ++i)
317317
try { listeners.get(i).ready(result); }
318-
catch (Throwable t) { app.getDefaultLogger().error(
318+
catch (Throwable t) { log.error(
319319
"Exception thrown by an inline listener of AsyncWork: " + listeners.get(i), t); }
320320
else
321321
for (int i = 0; i < listeners.size(); ++i) {
322322
long start = System.nanoTime();
323323
try { listeners.get(i).ready(result); }
324-
catch (Throwable t) { app.getDefaultLogger().error(
324+
catch (Throwable t) { log.error(
325325
"Exception thrown by an inline listener of AsyncWork: " + listeners.get(i), t); }
326326
long time = System.nanoTime() - start;
327327
if (time > 1000000) // more than 1ms
@@ -356,29 +356,28 @@ public void unblockError(TError error) {
356356
listeners = listenersInline;
357357
listenersInline = new ArrayList<>(2);
358358
}
359+
Application app = LCCore.getApplication();
360+
Logger log = app.isReleaseMode() ? null : app.getLoggerFactory().getLogger(SynchronizationPoint.class);
359361
while (true) {
360-
Application app = LCCore.getApplication();
361-
Logger log = app.isReleaseMode() ? null : app.getLoggerFactory().getLogger(SynchronizationPoint.class);
362-
if (log == null || !log.debug())
362+
if (!log.debug())
363363
for (int i = 0; i < listeners.size(); ++i)
364364
try { listeners.get(i).error(error); }
365-
catch (Throwable t) { app.getDefaultLogger().error(
365+
catch (Throwable t) { log.error(
366366
"Exception thrown by an inline listener of AsyncWork, cancel it: " + listeners.get(i), t);
367367
try { listeners.get(i).cancelled(new CancelException("Error in listener", t)); }
368-
catch (Throwable t2) { app.getDefaultLogger().error(
368+
catch (Throwable t2) { log.error(
369369
"Exception thrown while cancelling inline listener of AsyncWork after error: "
370370
+ listeners.get(i), t2); }
371371
}
372372
else
373373
for (int i = 0; i < listeners.size(); ++i) {
374374
long start = System.nanoTime();
375375
try { listeners.get(i).error(error); }
376-
catch (Throwable t) {
377-
app.getDefaultLogger().error(
376+
catch (Throwable t) { log.error(
378377
"Exception thrown by an inline listener of AsyncWork, cancel it: "
379378
+ listeners.get(i), t);
380379
try { listeners.get(i).cancelled(new CancelException("Error in listener", t)); }
381-
catch (Throwable t2) { app.getDefaultLogger().error(
380+
catch (Throwable t2) { log.error(
382381
"Exception thrown while cancelling inline listener of AsyncWork after error: "
383382
+ listeners.get(i), t2); }
384383
}
@@ -420,19 +419,19 @@ public void unblockCancel(CancelException event) {
420419
listeners = listenersInline;
421420
listenersInline = new ArrayList<>(2);
422421
}
422+
Application app = LCCore.getApplication();
423+
Logger log = app.isReleaseMode() ? null : app.getLoggerFactory().getLogger(SynchronizationPoint.class);
423424
while (true) {
424-
Application app = LCCore.getApplication();
425-
Logger log = app.isReleaseMode() ? null : app.getLoggerFactory().getLogger(SynchronizationPoint.class);
426-
if (log == null || !log.debug())
425+
if (!log.debug())
427426
for (int i = 0; i < listeners.size(); ++i)
428427
try { listeners.get(i).cancelled(event); }
429-
catch (Throwable t) { app.getDefaultLogger().error(
428+
catch (Throwable t) { log.error(
430429
"Exception thrown by an inline listener of AsyncWork: " + listeners.get(i), t); }
431430
else
432431
for (int i = 0; i < listeners.size(); ++i) {
433432
long start = System.nanoTime();
434433
try { listeners.get(i).cancelled(event); }
435-
catch (Throwable t) { app.getDefaultLogger().error(
434+
catch (Throwable t) { log.error(
436435
"Exception thrown by an inline listener of AsyncWork: " + listeners.get(i), t); }
437436
long time = System.nanoTime() - start;
438437
if (time > 1000000) // more than 1ms

net.lecousin.core/src/main/java/net/lecousin/framework/concurrent/synch/SynchronizationPoint.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ public void unblock() {
7878
listeners = listenersInline;
7979
listenersInline = new ArrayList<>(2);
8080
}
81-
while (listeners != null) {
82-
Application app = LCCore.getApplication();
83-
Logger log = app.isReleaseMode() ? null : app.getLoggerFactory().getLogger(SynchronizationPoint.class);
81+
Application app = LCCore.getApplication();
82+
Logger log = app.isReleaseMode() ? null : app.getLoggerFactory().getLogger(SynchronizationPoint.class);
83+
while (true) {
8484
if (log == null || !log.debug())
8585
for (int i = 0; i < listeners.size(); ++i)
8686
try { listeners.get(i).run(); }
87-
catch (Throwable t) { app.getDefaultLogger().error(
87+
catch (Throwable t) { log.error(
8888
"Exception thrown by an inline listener of SynchronizationPoint", t); }
8989
else
9090
for (int i = 0; i < listeners.size(); ++i) {
9191
long start = System.nanoTime();
9292
try { listeners.get(i).run(); }
93-
catch (Throwable t) { app.getDefaultLogger().error(
93+
catch (Throwable t) { log.error(
9494
"Exception thrown by an inline listener of SynchronizationPoint", t); }
9595
long time = System.nanoTime() - start;
9696
if (time > 1000000) // more than 1ms

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/concurrent/synch/TestAsyncWork.java

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package net.lecousin.framework.core.tests.concurrent.synch;
22

3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.TimeUnit;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import net.lecousin.framework.application.LCCore;
310
import net.lecousin.framework.concurrent.CancelException;
411
import net.lecousin.framework.concurrent.synch.AsyncWork;
512
import net.lecousin.framework.concurrent.synch.AsyncWork.AsyncWorkListener;
@@ -8,14 +15,13 @@
815
import net.lecousin.framework.concurrent.synch.SynchronizationPoint;
916
import net.lecousin.framework.core.test.LCCoreAbstractTest;
1017
import net.lecousin.framework.event.Listener;
18+
import net.lecousin.framework.log.Logger;
19+
import net.lecousin.framework.log.Logger.Level;
1120
import net.lecousin.framework.mutable.Mutable;
1221
import net.lecousin.framework.mutable.MutableInteger;
1322
import net.lecousin.framework.util.Pair;
1423
import net.lecousin.framework.util.RunnableWithParameter;
1524

16-
import org.junit.Assert;
17-
import org.junit.Test;
18-
1925
public class TestAsyncWork extends LCCoreAbstractTest {
2026

2127
@Test(timeout=30000)
@@ -282,6 +288,120 @@ public void run() {
282288
aw.unblockSuccess(Integer.valueOf(51));
283289
if (result.get() != null)
284290
throw result.get();
291+
292+
aw = new AsyncWork<>();
293+
result.set(null);
294+
aw.listenInline((res) -> { result.set(new AssertionError("Listener onSuccess called")); });
295+
aw.error(new Exception());
296+
if (result.get() != null)
297+
throw result.get();
298+
299+
aw = new AsyncWork<>();
300+
result.set(null);
301+
aw.listenInline((res) -> { result.set(new AssertionError("Listener onSuccess called")); });
302+
aw.cancel(new CancelException("test"));
303+
if (result.get() != null)
304+
throw result.get();
305+
306+
try {
307+
AsyncWork<Integer, Exception> aw1 = new AsyncWork<>();
308+
aw1.listenInline(() -> { aw1.listenInline(() -> {}); });
309+
aw1.error(new Exception());
310+
311+
AsyncWork<Integer, Exception> aw2 = new AsyncWork<>();
312+
aw2.listenInline(() -> { aw2.listenInline(() -> {}); });
313+
aw2.cancel(new CancelException("test"));
314+
} finally {
315+
}
316+
317+
Logger log = LCCore.get().getApplication().getLoggerFactory().getLogger(SynchronizationPoint.class);
318+
log.setLevel(Level.INFO);
319+
320+
try {
321+
322+
AsyncWork<Integer, Exception> aw1 = new AsyncWork<>();
323+
aw1.listenInline(() -> { aw1.listenInline(() -> {}); });
324+
aw1.error(new Exception());
325+
326+
AsyncWork<Integer, Exception> aw2 = new AsyncWork<>();
327+
aw2.listenInline(() -> { aw2.listenInline(() -> {}); });
328+
aw2.cancel(new CancelException("test"));
329+
330+
try {
331+
aw2.blockResult(0);
332+
throw new AssertionError("Should be cancelled");
333+
} catch (CancelException e) {
334+
// ok
335+
} catch (Throwable t) {
336+
throw new AssertionError("Should be cancelled", t);
337+
}
338+
339+
} finally {
340+
log.setLevel(Level.DEBUG);
341+
}
342+
}
343+
344+
@Test(timeout=30000)
345+
public void testFuture() {
346+
AsyncWork<Integer, Exception> aw = new AsyncWork<>();
347+
Assert.assertFalse(aw.isDone());
348+
aw.unblockSuccess(Integer.valueOf(51));
349+
try {
350+
Assert.assertEquals(51, aw.get().intValue());
351+
Assert.assertEquals(51, aw.get(1, TimeUnit.SECONDS).intValue());
352+
Assert.assertTrue(aw.isDone());
353+
Assert.assertFalse(aw.cancel(true));
354+
} catch (Exception e) {
355+
throw new AssertionError(e);
356+
}
357+
358+
aw = new AsyncWork<>();
359+
Assert.assertFalse(aw.isDone());
360+
aw.cancel(new CancelException("test"));
361+
try {
362+
aw.get();
363+
throw new AssertionError();
364+
} catch (ExecutionException e) {
365+
Assert.assertTrue(e.getCause() instanceof CancelException);
366+
} catch (Throwable t) {
367+
throw new AssertionError(t);
368+
}
369+
try {
370+
aw.get(1, TimeUnit.SECONDS);
371+
throw new AssertionError();
372+
} catch (ExecutionException e) {
373+
Assert.assertTrue(e.getCause() instanceof CancelException);
374+
} catch (Throwable t) {
375+
throw new AssertionError(t);
376+
}
377+
Assert.assertTrue(aw.isDone());
378+
Assert.assertFalse(aw.cancel(true));
379+
380+
aw = new AsyncWork<>();
381+
Assert.assertTrue(aw.cancel(true));
382+
Assert.assertTrue(aw.isDone());
383+
384+
aw = new AsyncWork<>();
385+
Assert.assertFalse(aw.isDone());
386+
aw.error(new Exception("test"));
387+
try {
388+
aw.get();
389+
throw new AssertionError();
390+
} catch (ExecutionException e) {
391+
Assert.assertTrue(e.getCause() instanceof Exception);
392+
} catch (Throwable t) {
393+
throw new AssertionError(t);
394+
}
395+
try {
396+
aw.get(1, TimeUnit.SECONDS);
397+
throw new AssertionError();
398+
} catch (ExecutionException e) {
399+
Assert.assertTrue(e.getCause() instanceof Exception);
400+
} catch (Throwable t) {
401+
throw new AssertionError(t);
402+
}
403+
Assert.assertTrue(aw.isDone());
404+
Assert.assertFalse(aw.cancel(true));
285405
}
286406

287407
@Test(timeout=30000)

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/concurrent/synch/TestSynchronizationPoint.java

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package net.lecousin.framework.core.tests.concurrent.synch;
22

3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.TimeUnit;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
39
import net.lecousin.framework.concurrent.CancelException;
410
import net.lecousin.framework.concurrent.Task;
511
import net.lecousin.framework.concurrent.synch.AsyncWork;
@@ -8,9 +14,6 @@
814
import net.lecousin.framework.event.Listener;
915
import net.lecousin.framework.mutable.MutableInteger;
1016

11-
import org.junit.Assert;
12-
import org.junit.Test;
13-
1417
public class TestSynchronizationPoint extends LCCoreAbstractTest {
1518

1619
@Test(timeout=30000)
@@ -281,4 +284,67 @@ public Void run() throws CancelException {
281284
spbp2.blockPause(1);
282285
}
283286

287+
@Test(timeout=30000)
288+
public void testFuture() {
289+
SynchronizationPoint<Exception> sp = new SynchronizationPoint<>();
290+
Assert.assertFalse(sp.isDone());
291+
sp.unblock();
292+
try {
293+
Assert.assertNull(sp.get());
294+
Assert.assertNull(sp.get(1, TimeUnit.SECONDS));
295+
Assert.assertTrue(sp.isDone());
296+
Assert.assertFalse(sp.cancel(true));
297+
} catch (Exception e) {
298+
throw new AssertionError(e);
299+
}
300+
301+
sp = new SynchronizationPoint<>();
302+
Assert.assertFalse(sp.isDone());
303+
sp.cancel(new CancelException("test"));
304+
try {
305+
sp.get();
306+
throw new AssertionError();
307+
} catch (ExecutionException e) {
308+
Assert.assertTrue(e.getCause() instanceof CancelException);
309+
} catch (Throwable t) {
310+
throw new AssertionError(t);
311+
}
312+
try {
313+
sp.get(1, TimeUnit.SECONDS);
314+
throw new AssertionError();
315+
} catch (ExecutionException e) {
316+
Assert.assertTrue(e.getCause() instanceof CancelException);
317+
} catch (Throwable t) {
318+
throw new AssertionError(t);
319+
}
320+
Assert.assertTrue(sp.isDone());
321+
Assert.assertFalse(sp.cancel(true));
322+
323+
sp = new SynchronizationPoint<>();
324+
Assert.assertTrue(sp.cancel(true));
325+
Assert.assertTrue(sp.isDone());
326+
327+
sp = new SynchronizationPoint<>();
328+
Assert.assertFalse(sp.isDone());
329+
sp.error(new Exception("test"));
330+
try {
331+
sp.get();
332+
throw new AssertionError();
333+
} catch (ExecutionException e) {
334+
Assert.assertTrue(e.getCause() instanceof Exception);
335+
} catch (Throwable t) {
336+
throw new AssertionError(t);
337+
}
338+
try {
339+
sp.get(1, TimeUnit.SECONDS);
340+
throw new AssertionError();
341+
} catch (ExecutionException e) {
342+
Assert.assertTrue(e.getCause() instanceof Exception);
343+
} catch (Throwable t) {
344+
throw new AssertionError(t);
345+
}
346+
Assert.assertTrue(sp.isDone());
347+
Assert.assertFalse(sp.cancel(true));
348+
}
349+
284350
}

0 commit comments

Comments
 (0)