Skip to content

Add multi-thread dumpLocks test #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.openhft.affinity;

import net.openhft.affinity.impl.VanillaCpuLayout;
import org.junit.Assume;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertTrue;

public class AffinityLockDumpLocksTest extends BaseAffinityTest {

@Test
public void dumpLocksListsThreadsHoldingLocks() throws Exception {
Assume.assumeTrue(new File("/proc/cpuinfo").exists());

AffinityLock.cpuLayout(VanillaCpuLayout.fromCpuInfo());

int nThreads = Math.min(3, Math.max(1, AffinityLock.PROCESSORS - 1));
CountDownLatch acquired = new CountDownLatch(nThreads);
CountDownLatch release = new CountDownLatch(1);
List<Thread> threads = new ArrayList<>();

for (int i = 0; i < nThreads; i++) {
String name = "worker-" + i;
Thread t = new Thread(() -> {
try (AffinityLock lock = AffinityLock.acquireLock()) {
acquired.countDown();
release.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}, name);
threads.add(t);
t.start();
}

assertTrue("threads failed to acquire locks", acquired.await(5, TimeUnit.SECONDS));

String dump = AffinityLock.dumpLocks();
for (Thread t : threads) {
assertTrue("Missing entry for " + t.getName(), dump.contains("Thread[" + t.getName()));
}

release.countDown();
for (Thread t : threads) {
t.join();
}
}
}