Skip to content

Commit 38a2389

Browse files
committed
add tests
1 parent 47bbfeb commit 38a2389

File tree

17 files changed

+139
-5
lines changed

17 files changed

+139
-5
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/concurrent/DrivesTaskManager.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,15 @@ public Object getResource(File file) {
5757

5858
/** Return the associated resource for the given file path. */
5959
public Object getResource(String path) {
60+
Map.Entry<String, Object> bestMatch = null;
6061
synchronized (rootManagers) {
6162
for (Map.Entry<String, Object> e : rootResources.entrySet())
62-
if (path.startsWith(e.getKey()))
63-
return e.getValue();
63+
if (path.startsWith(e.getKey())) {
64+
if (bestMatch == null || bestMatch.getKey().length() < e.getKey().length())
65+
bestMatch = e;
66+
}
67+
if (bestMatch != null)
68+
return bestMatch.getValue();
6469
}
6570
return null;
6671
}
@@ -79,10 +84,15 @@ public TaskManager getTaskManager(File file) {
7984

8085
/** Return the TaskManager for the given file path. */
8186
public TaskManager getTaskManager(String path) {
87+
Map.Entry<String, MonoThreadTaskManager> bestMatch = null;
8288
synchronized (rootManagers) {
8389
for (Map.Entry<String, MonoThreadTaskManager> e : rootManagers.entrySet())
84-
if (path.startsWith(e.getKey()))
85-
return e.getValue();
90+
if (path.startsWith(e.getKey())) {
91+
if (bestMatch == null || bestMatch.getKey().length() < e.getKey().length())
92+
bestMatch = e;
93+
}
94+
if (bestMatch != null)
95+
return bestMatch.getValue();
8696
}
8797
return null;
8898
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.lecousin.framework.core.tests.concurrent;
2+
3+
import net.lecousin.framework.concurrent.CPUTaskManager;
4+
import net.lecousin.framework.concurrent.Threading;
5+
import net.lecousin.framework.core.test.LCCoreAbstractTest;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
public class TestCPUTaskManager extends LCCoreAbstractTest {
11+
12+
@Test(timeout=120000)
13+
public void test() {
14+
CPUTaskManager cpu = (CPUTaskManager)Threading.getCPUTaskManager();
15+
16+
Assert.assertEquals(Threading.CPU, Threading.getCPUTaskManager().getResource());
17+
18+
String name = cpu.getName();
19+
cpu.setName("test");
20+
Assert.assertEquals("test", cpu.getName());
21+
cpu.setName(name);
22+
}
23+
24+
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/concurrent/TestDrivesTaskManager.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
package net.lecousin.framework.core.tests.concurrent;
22

33
import java.io.File;
4+
import java.util.Collections;
5+
import java.util.List;
46

7+
import net.lecousin.framework.concurrent.CancelException;
8+
import net.lecousin.framework.concurrent.DrivesTaskManager;
9+
import net.lecousin.framework.concurrent.DrivesTaskManager.DrivesProvider;
10+
import net.lecousin.framework.concurrent.Task;
511
import net.lecousin.framework.concurrent.Threading;
12+
import net.lecousin.framework.concurrent.synch.SynchronizationPoint;
13+
import net.lecousin.framework.concurrent.tasks.drives.CreateDirectoryTask;
614
import net.lecousin.framework.core.test.LCCoreAbstractTest;
15+
import net.lecousin.framework.event.Listener;
16+
import net.lecousin.framework.io.TemporaryFiles;
17+
import net.lecousin.framework.util.Pair;
718

819
import org.junit.Assert;
920
import org.junit.Test;
@@ -19,4 +30,50 @@ public void simpleTests() {
1930
Threading.getDrivesTaskManager().getResources();
2031
}
2132

33+
@Test(timeout=120000)
34+
public void testDrivesProvider() throws Exception {
35+
DrivesTaskManager tm = Threading.getDrivesTaskManager();
36+
File tmpDir = TemporaryFiles.get().createDirectorySync("testDrivesTM");
37+
File dir = new File(tmpDir, "test");
38+
Object fakeDrive1 = new Object();
39+
Object fakeDrive2 = new Object();
40+
SynchronizationPoint<Exception> spDrive2Appear = new SynchronizationPoint<>();
41+
SynchronizationPoint<Exception> spDrive2AppearDone = new SynchronizationPoint<>();
42+
SynchronizationPoint<Exception> spDrive1Disappear = new SynchronizationPoint<>();
43+
SynchronizationPoint<Exception> spDrive1DisappearDone = new SynchronizationPoint<>();
44+
tm.setDrivesProvider(new DrivesProvider() {
45+
@Override
46+
public void provide(
47+
Listener<Pair<Object, List<File>>> onNewDrive,
48+
Listener<Pair<Object, List<File>>> onDriveRemoved,
49+
Listener<Pair<Object, File>> onNewPartition,
50+
Listener<Pair<Object, File>> onPartitionRemoved
51+
) {
52+
onNewDrive.fire(new Pair<>(fakeDrive1, Collections.singletonList(tmpDir)));
53+
spDrive2Appear.listenInline(() -> {
54+
onNewDrive.fire(new Pair<>(fakeDrive2, Collections.singletonList(tmpDir)));
55+
spDrive2AppearDone.unblock();
56+
});
57+
spDrive1Disappear.listenInline(() -> {
58+
onDriveRemoved.fire(new Pair<>(fakeDrive1, Collections.singletonList(tmpDir)));
59+
spDrive1DisappearDone.unblock();
60+
});
61+
}
62+
});
63+
64+
CreateDirectoryTask task = new CreateDirectoryTask(dir, false, true, Task.PRIORITY_NORMAL);
65+
task.executeIn(60000).start();
66+
Assert.assertEquals(fakeDrive1, task.getTaskManager().getResource());
67+
spDrive2Appear.unblock();
68+
spDrive2AppearDone.blockThrow(0);
69+
spDrive1Disappear.unblock();
70+
spDrive1DisappearDone.blockThrow(0);
71+
task.changeNextExecutionTime(System.currentTimeMillis());
72+
task.getOutput().block(0);
73+
Assert.assertEquals(fakeDrive2, task.getTaskManager().getResource());
74+
task.cancel(new CancelException("Test is ok"));
75+
task = new CreateDirectoryTask(dir, false, true, Task.PRIORITY_NORMAL);
76+
Assert.assertEquals(fakeDrive2, task.getTaskManager().getResource());
77+
}
78+
2279
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,18 @@ public void run() {
270270
Assert.assertTrue(sp.isUnblocked());
271271
Assert.assertFalse(sp.hasError());
272272
Assert.assertTrue(sp.isCancelled());
273+
274+
aw = new AsyncWork<>();
275+
result.set(new AssertionError("Listener not called"));
276+
aw.listenInline((res) -> {
277+
if (res != null && res.intValue() == 51)
278+
result.set(null);
279+
else
280+
result.set(new AssertionError("Listener received wrong result: " + res));
281+
});
282+
aw.unblockSuccess(Integer.valueOf(51));
283+
if (result.get() != null)
284+
throw result.get();
273285
}
274286

275287
@Test(timeout=30000)

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/xml/TestXMLStreamEventsAsync.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ public void testReadInnerElementsText() throws Exception {
9595
@Test
9696
public void test2() throws Exception {
9797
XMLStreamEventsAsync xml;
98+
xml = parse("xml-test-suite/mine/002.xml");
99+
xml.start().blockThrow(0);
100+
xml.searchElement("translation").blockThrow(0);
101+
xml.nextStartElement().blockThrow(0); // hello
102+
xml.nextStartElement().blockThrow(0); // french
103+
xml.nextStartElement().blockThrow(0); // spanish
104+
xml.nextStartElement().blockThrow(0); // english
105+
try {
106+
xml.nextStartElement().blockThrow(0); // no more
107+
throw new AssertionError("No more start element was expected");
108+
} catch (EOFException e) {
109+
// expected
110+
}
111+
98112
xml = parse("xml-test-suite/mine/002.xml");
99113
xml.start().blockThrow(0);
100114
xml.searchElement("translation").blockThrow(0);

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/xml/TestXMLStreamEventsSync.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void testInnerText() throws Exception {
134134
}
135135

136136
public static final int ERROR_START = 1;
137-
public static final int ERROR_END = 68;
137+
public static final int ERROR_END = 79;
138138

139139
@Test(timeout=120000)
140140
public void testErrors() {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<hello>
2+
<!- wrong comment -->
3+
</hello>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<hello>
2+
<![ wrong cdata -->
3+
</hello>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<hello>
2+
<!DOC wrong doc type -->
3+
</hello>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- a comment that is not closed properly -

0 commit comments

Comments
 (0)