Skip to content

Commit afa5fac

Browse files
committed
use virtual thread as well as scope
1 parent fdac0ff commit afa5fac

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
package net.codetojoy;
3+
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.concurrent.atomic.AtomicBoolean;
9+
import jdk.incubator.concurrent.StructuredTaskScope;
10+
11+
// Loom: we want to ensure this compiles, runs
12+
class CustomStructuredTaskScope<T> extends StructuredTaskScope<T> {}
13+
14+
class Worker {
15+
static AtomicBoolean isDone = new AtomicBoolean(false);
16+
17+
static void doSleep() {
18+
System.out.println("TRACER working t: " + Thread.currentThread());
19+
try { Thread.sleep(500); } catch (Exception ex) {}
20+
isDone.set(true);
21+
}
22+
23+
static void doWork() {
24+
isDone.set(false);
25+
try {
26+
// Loom: we want to ensure this compiles, runs
27+
var thread = Thread.startVirtualThread(Worker::doSleep);
28+
} catch(Exception ex) {
29+
System.err.println("TRACER caught ex !? msg: " + ex.getMessage());
30+
}
31+
}
32+
}
33+
34+
// This isn't really a unit test, but it exercises Loom features to ensure
35+
// that JUnit is happy and configured properly.
36+
37+
public class TestLoom {
38+
@Test
39+
void canaryTest() {
40+
// test
41+
var x = 4 + 4;
42+
43+
assertEquals(8, x);
44+
}
45+
46+
@Test
47+
void workerTest() {
48+
var worker = new Worker();
49+
50+
// test
51+
worker.doWork();
52+
53+
while (! worker.isDone.get()) {
54+
System.out.println("TRACER waiting on worker");
55+
try { Thread.sleep(250); } catch (Exception ex) {}
56+
}
57+
}
58+
}

egg__12_sc_junit/src/test/java/net/codetojoy/TestScope.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)