Skip to content
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

Dean Schulze and Diana Y's SimpleAtomicLongMultithreadedTest #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -7,6 +7,7 @@
@RunWith(Suite.class)
@SuiteClasses({PalantirManagerUnitTest.class,
SimpleAtomicLongUnitTest.class,
SimpleSemaphoreUnitTest.class})
SimpleSemaphoreUnitTest.class,
SimpleAtomicLongMultithreadedTest.class})
public class AllTests {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package edu.vuum.mocca;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

import org.junit.Test;

public class SimpleAtomicLongMultithreadedTest {

final static long INITIAL_VALUE = 0;
static CyclicBarrier mStartBarrier;
static CountDownLatch mStopLatch;
static SimpleAtomicLong mCounter;

final static int numThreads = 200;
static List<Long> syncList = Collections.synchronizedList(new ArrayList<Long>());

static class RunTest implements Runnable {
private Runnable mCommand;
RunTest(Runnable command) {
mCommand = command;
}
public void run() {
mCommand.run();
}
}

@Test
public void multiGetAndIncrementTest() throws InterruptedException {
Runnable getAndIncrementRunnable = new Runnable() {
public void run() {
try {
mStartBarrier.await();
syncList.add(mCounter.getAndIncrement());
mStopLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
};

runThreads(getAndIncrementRunnable);
}

@Test
public void multiIncrementAndGetTest() throws InterruptedException {
Runnable incrementAndGetRunnable = new Runnable() {
public void run() {
try {
mStartBarrier.await();
syncList.add(mCounter.incrementAndGet());
mStopLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
};

runThreads(incrementAndGetRunnable);
}

@Test
public void multitGetAndDecremenTest() throws InterruptedException {
Runnable getAndDecrementRunnable = new Runnable() {
public void run() {
try {
mStartBarrier.await();
syncList.add(mCounter.getAndDecrement());
mStopLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
};

runThreads(getAndDecrementRunnable);
}

@Test
public void multiDecrementAndGetTest() throws InterruptedException {
Runnable decrementAndGetRunnable = new Runnable() {
public void run() {
try {
mStartBarrier.await();
syncList.add(mCounter.decrementAndGet());
mStopLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
};

runThreads(decrementAndGetRunnable);
}



private void runThreads(Runnable command) throws InterruptedException {
mStopLatch = new CountDownLatch(numThreads);
mCounter = new SimpleAtomicLong(INITIAL_VALUE);
mStartBarrier = new CyclicBarrier(numThreads);
RunTest[] runTests;

syncList.clear();
runTests = new RunTest[numThreads];
for (int i = 0; i < runTests.length; i++)
runTests[i] = new RunTest(command);
for (int i = 0; i < runTests.length; i++)
new Thread(runTests[i]).start();

mStopLatch.await();

Set<Long> resultSet = new TreeSet<Long>(syncList);

assertEquals(syncList.size(), resultSet.size());
}


}