Skip to content

Commit 100992a

Browse files
committed
add tests
1 parent 54b8fec commit 100992a

File tree

8 files changed

+239
-1
lines changed

8 files changed

+239
-1
lines changed

codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
coverage:
2-
range: "60...80"
2+
range: "75...90"

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/concurrent/async/TestAsync.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,4 +740,13 @@ public void testBlockPauseWithWarning() {
740740
a.blockPause(1);
741741
}
742742

743+
@Test
744+
public void testConvertError() {
745+
Async<IOException> a = new Async<>();
746+
Async<RuntimeException> b = new Async<>(a, e -> new RuntimeException("ok", e));
747+
a.error(new IOException());
748+
Assert.assertNotNull(b.getError());
749+
Assert.assertEquals("ok", b.getError().getMessage());
750+
}
751+
743752
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/concurrent/threads/TestTaskManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void testPutAsideAndKill() {
3535

3636
@Test
3737
public void testBlockInUnmanaged() throws Exception {
38+
Threading.traceBlockingTasks = true;
3839
Async<NoException> a = new Async<>();
3940
Async<NoException> b = new Async<>();
4041
Task<?, ?> task = Task.unmanaged("Test blocking task", () -> {
@@ -46,6 +47,7 @@ public void testBlockInUnmanaged() throws Exception {
4647
b.blockThrow(0);
4748
Thread.sleep(500);
4849
a.unblock();
50+
Threading.traceBlockingTasks = false;
4951
}
5052

5153
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package net.lecousin.framework.core.tests.concurrent.util;
2+
3+
import net.lecousin.framework.concurrent.async.Async;
4+
import net.lecousin.framework.concurrent.async.IAsync;
5+
import net.lecousin.framework.concurrent.util.AsyncConsumer;
6+
import net.lecousin.framework.core.test.LCCoreAbstractTest;
7+
import net.lecousin.framework.mutable.MutableInteger;
8+
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
12+
public class TestAsyncConsumer extends LCCoreAbstractTest {
13+
14+
@Test
15+
public void testSimple() throws Exception {
16+
AsyncConsumer.Simple<Integer, Exception> c = new AsyncConsumer.Simple<Integer, Exception>() {
17+
@Override
18+
public IAsync<Exception> consume(Integer data) {
19+
return new Async<>(true);
20+
}
21+
};
22+
c.consume(Integer.valueOf(0));
23+
c.end();
24+
c.error(new Exception());
25+
}
26+
27+
@Test
28+
public void testError() throws Exception {
29+
AsyncConsumer.Error<Integer, Exception> c = new AsyncConsumer.Error<>(new Exception("ok"));
30+
IAsync<Exception> a;
31+
a = c.consume(Integer.valueOf(0));
32+
Assert.assertTrue(a.hasError());
33+
Assert.assertEquals("ok", a.getError().getMessage());
34+
a = c.end();
35+
Assert.assertTrue(a.hasError());
36+
Assert.assertEquals("ok", a.getError().getMessage());
37+
c.error(new Exception());
38+
}
39+
40+
@Test
41+
public void testConverter() throws Exception {
42+
MutableInteger mi = new MutableInteger(0);
43+
AsyncConsumer.Simple<Integer, Exception> ci = new AsyncConsumer.Simple<Integer, Exception>() {
44+
@Override
45+
public IAsync<Exception> consume(Integer data) {
46+
mi.set(data.intValue());
47+
return new Async<>(true);
48+
}
49+
};
50+
AsyncConsumer<Long, Exception> cl = ci.convert(l -> Integer.valueOf(l.intValue()));
51+
Assert.assertEquals(0, mi.get());
52+
cl.consume(Long.valueOf(51)).blockThrow(0);
53+
Assert.assertEquals(51, mi.get());
54+
cl.end().blockThrow(0);
55+
cl.error(new Exception());
56+
}
57+
58+
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/concurrent/util/TestAsyncTimeout.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,20 @@ public void testNoTimeout() {
1818
Async<Exception> a = new Async<>();
1919
MutableBoolean timedOut = new MutableBoolean(false);
2020
AsyncTimeoutManager.timeout(a, 1000, () -> timedOut.set(true));
21+
Async<Exception> b = new Async<>();
22+
MutableBoolean timedOut2 = new MutableBoolean(false);
23+
AsyncTimeoutManager.timeout(b, 1000, () -> timedOut2.set(true));
24+
Async<Exception> c = new Async<>();
25+
MutableBoolean timedOut3 = new MutableBoolean(false);
26+
AsyncTimeoutManager.timeout(c, 900, () -> timedOut3.set(true));
2127
a.unblock();
28+
b.unblock();
29+
c.unblock();
2230
try { Thread.sleep(2000); }
2331
catch (InterruptedException e) {}
2432
Assert.assertFalse(timedOut.get());
33+
Assert.assertFalse(timedOut2.get());
34+
Assert.assertFalse(timedOut3.get());
2535
}
2636

2737
@Test

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/concurrent/util/TestBufferedAsyncConsumer.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.lecousin.framework.core.test.LCCoreAbstractTest;
1313
import net.lecousin.framework.mutable.MutableInteger;
1414

15+
import org.junit.Assert;
1516
import org.junit.Test;
1617

1718
public class TestBufferedAsyncConsumer extends LCCoreAbstractTest {
@@ -110,6 +111,13 @@ public void error(Exception error) {
110111
} catch (IllegalArgumentException e) {
111112
// ok
112113
}
114+
try {
115+
buffered.end().blockThrow(0);
116+
} catch (IllegalArgumentException e) {
117+
// ok
118+
}
119+
buffered.error(new Exception());
120+
Assert.assertTrue(buffered.end().getError() instanceof IllegalArgumentException);
113121
}
114122

115123
@Test
@@ -144,6 +152,43 @@ public void error(Exception error) {
144152
}
145153
}
146154

155+
@Test
156+
public void testErrorWhilePendingOperations() {
157+
AsyncConsumer<Integer, Exception> finalConsumer = new AsyncConsumer<Integer, Exception>() {
158+
@Override
159+
public IAsync<Exception> consume(Integer data) {
160+
try {
161+
Thread.sleep(50);
162+
} catch (InterruptedException e) {
163+
}
164+
return new Async<>(true);
165+
}
166+
167+
@Override
168+
public IAsync<Exception> end() {
169+
return new Async<>(new IllegalArgumentException());
170+
}
171+
172+
@Override
173+
public void error(Exception error) {
174+
}
175+
};
176+
BufferedAsyncConsumer<Integer, Exception> buffered = new BufferedAsyncConsumer<>(1, finalConsumer);
177+
buffered.consume(Integer.valueOf(0));
178+
buffered.consume(Integer.valueOf(0));
179+
buffered.consume(Integer.valueOf(0));
180+
buffered.consume(Integer.valueOf(0));
181+
buffered.consume(Integer.valueOf(0));
182+
buffered.consume(Integer.valueOf(0));
183+
buffered.consume(Integer.valueOf(0));
184+
buffered.consume(Integer.valueOf(0));
185+
buffered.consume(Integer.valueOf(0));
186+
buffered.consume(Integer.valueOf(0));
187+
IAsync<Exception> end = buffered.end();
188+
buffered.error(new Exception("ok"));
189+
Assert.assertEquals("ok", end.getError().getMessage());
190+
}
191+
147192
@Test
148193
public void testPush() throws Exception {
149194
AsyncConsumer<Integer, Exception> finalConsumer = new AsyncConsumer<Integer, Exception>() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package net.lecousin.framework.core.tests.concurrent.util;
2+
3+
import java.io.IOException;
4+
import java.nio.ByteBuffer;
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
import net.lecousin.framework.concurrent.async.AsyncSupplier;
9+
import net.lecousin.framework.concurrent.util.PartialAsyncConsumer;
10+
import net.lecousin.framework.core.test.LCCoreAbstractTest;
11+
import net.lecousin.framework.io.data.ByteArray;
12+
13+
import org.junit.Assert;
14+
import org.junit.Test;
15+
16+
public class TestPartialAsyncConsumer extends LCCoreAbstractTest {
17+
18+
private static class Consumer1 implements PartialAsyncConsumer<ByteBuffer, Exception> {
19+
private boolean done = false;
20+
21+
@Override
22+
public AsyncSupplier<Boolean, Exception> consume(ByteBuffer data) {
23+
done = true;
24+
if (data.get() == 1)
25+
return new AsyncSupplier<>(Boolean.TRUE, null);
26+
return new AsyncSupplier<>(null, new Exception());
27+
}
28+
29+
@Override
30+
public boolean isExpectingData() {
31+
return !done;
32+
}
33+
}
34+
35+
private static class Consumer2 implements PartialAsyncConsumer<ByteArray, Exception> {
36+
37+
private boolean done = false;
38+
39+
@Override
40+
public AsyncSupplier<Boolean, Exception> consume(ByteArray data) {
41+
done = true;
42+
if (data.get() == 2)
43+
return new AsyncSupplier<>(Boolean.TRUE, null);
44+
return new AsyncSupplier<>(null, new Exception());
45+
}
46+
47+
@Override
48+
public boolean isExpectingData() {
49+
return !done;
50+
}
51+
52+
}
53+
54+
private static class Consumer3 implements PartialAsyncConsumer<ByteBuffer, IOException> {
55+
56+
private int pos = 0;
57+
58+
@Override
59+
public AsyncSupplier<Boolean, IOException> consume(ByteBuffer data) {
60+
if (pos == 0) {
61+
pos++;
62+
if (data.get() == 3)
63+
return new AsyncSupplier<>(Boolean.FALSE, null);
64+
return new AsyncSupplier<>(null, new IOException());
65+
}
66+
pos++;
67+
if (data.get() == 4)
68+
return new AsyncSupplier<>(Boolean.TRUE, null);
69+
return new AsyncSupplier<>(null, new IOException());
70+
}
71+
72+
@Override
73+
public boolean isExpectingData() {
74+
return pos < 2;
75+
}
76+
77+
}
78+
79+
@Test
80+
public void testQueue() throws Exception {
81+
Consumer1 c1 = new Consumer1();
82+
Consumer2 c2 = new Consumer2();
83+
PartialAsyncConsumer.ConsumerQueue<ByteBuffer, Exception> queue = new PartialAsyncConsumer.ConsumerQueue<>(
84+
c1,
85+
c2.convert(ByteArray::fromByteBuffer, ByteArray::setPosition, e -> e)
86+
);
87+
ByteBuffer b = ByteBuffer.wrap(new byte[] { 1, 2, 3 });
88+
queue.consume(b).blockThrow(0);
89+
Assert.assertEquals(2, b.position());
90+
91+
c1 = new Consumer1();
92+
c2 = new Consumer2();
93+
Consumer3 c3 = new Consumer3();
94+
95+
Queue<PartialAsyncConsumer<ByteBuffer, Exception>> consumers = new LinkedList<>();
96+
consumers.add(c1);
97+
consumers.add(c2.convert(ByteArray::fromByteBuffer, ByteArray::setPosition, e -> e));
98+
consumers.add(c3.convertError(e -> e));
99+
queue = new PartialAsyncConsumer.ConsumerQueue<>(consumers);
100+
b = ByteBuffer.wrap(new byte[] { 1, 2, 3 });
101+
queue.consume(b).blockThrow(0);
102+
Assert.assertEquals(3, b.position());
103+
b = ByteBuffer.wrap(new byte[] { 4, 5, 6 });
104+
queue.consume(b).blockThrow(0);
105+
Assert.assertEquals(1, b.position());
106+
}
107+
108+
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/plugins/TestPlugins.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import java.util.ArrayList;
44
import java.util.Collection;
55

6+
import net.lecousin.framework.application.LCCore;
67
import net.lecousin.framework.core.test.LCCoreAbstractTest;
78
import net.lecousin.framework.core.tests.plugins.AnExtensionPoint.MyPlugin;
9+
import net.lecousin.framework.log.Logger.Level;
810
import net.lecousin.framework.plugins.CustomExtensionPoint;
911
import net.lecousin.framework.plugins.ExtensionPoint;
1012
import net.lecousin.framework.plugins.ExtensionPoints;
@@ -33,7 +35,11 @@ public void testPlugins() {
3335
for (ExtensionPoint<?> e : ExtensionPoints.getExtensionPoints()) {
3436
e.getPluginClass();
3537
}
38+
LCCore.getApplication().getDefaultLogger().setLevel(Level.DEBUG);
3639
ExtensionPoints.logRemainingPlugins();
40+
LCCore.getApplication().getDefaultLogger().setLevel(Level.OFF);
41+
ExtensionPoints.logRemainingPlugins();
42+
LCCore.getApplication().getDefaultLogger().setLevel(Level.DEBUG);
3743

3844
ACustomExtensionPointWithFile c = ExtensionPoints.getCustomExtensionPoint(ACustomExtensionPointWithFile.class);
3945
Assert.assertNotNull(c);

0 commit comments

Comments
 (0)