Skip to content

Commit

Permalink
add fray example.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed Jul 15, 2024
1 parent c8cef43 commit 6a4eb24
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
25 changes: 25 additions & 0 deletions integration-tests/src/main/java/example/FrayExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package example;
import java.util.concurrent.atomic.AtomicInteger;

public class FrayExample extends Thread {
static Object o = new Object();
static AtomicInteger a = new AtomicInteger();
static volatile int b;
public void run() {
int x = a.getAndIncrement();
synchronized(o) { // monitorenter
if (x == 0) {
try { o.wait(); } catch (InterruptedException ignore) { }
} else {
o.notify();
}
} // monitorexit
b = x;
}
public static void main(String[] args) throws Exception {
FrayExample[] threads = {new FrayExample(), new FrayExample()};
for (var thread : threads) thread.start();
for (var thread : threads) thread.join();
assert(b == 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmu.pasta.fray.it.core;

import cmu.pasta.fray.core.scheduler.POSScheduler;
import cmu.pasta.fray.it.IntegrationTestRunner;
import example.FrayExample;
import org.junit.jupiter.api.Test;

import java.util.Random;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class FrayExampleTest extends IntegrationTestRunner {

@Test
void test() {
String result = runTest(() -> {
try {
FrayExample.main(new String[0]);
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}, new POSScheduler(new Random()), 10000);
assertTrue(result.contains("DeadlockException") || result.contains("AssertionError"));
}
}

0 comments on commit 6a4eb24

Please sign in to comment.