mirror of https://github.com/apache/cloudstack.git
Add test to check that each thread has it's own execution counter
This commit is contained in:
parent
24277e1d8e
commit
21d2423709
|
|
@ -22,6 +22,10 @@ package com.cloud.network.nicira;
|
|||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ExecutionCounterTest {
|
||||
|
|
@ -52,4 +56,45 @@ public class ExecutionCounterTest {
|
|||
|
||||
assertThat(executionCounter.hasReachedExecutionLimit(), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcurrentUpdatesToCounter() throws Exception {
|
||||
final ExecutionCounter executionCounter = new ExecutionCounter(0);
|
||||
final ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
final AtomicInteger counterTask1 = new AtomicInteger(-1);
|
||||
final AtomicInteger counterTask2 = new AtomicInteger(-1);
|
||||
final AtomicInteger counterTask3 = new AtomicInteger(-1);
|
||||
|
||||
final Runnable task1 = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
executionCounter.incrementExecutionCounter().incrementExecutionCounter();
|
||||
executionCounter.incrementExecutionCounter().incrementExecutionCounter();
|
||||
counterTask1.set(executionCounter.getValue());
|
||||
}
|
||||
};
|
||||
final Runnable task2 = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
executionCounter.incrementExecutionCounter().incrementExecutionCounter();
|
||||
counterTask2.set(executionCounter.getValue());
|
||||
}
|
||||
};
|
||||
final Runnable task3 = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
counterTask3.set(executionCounter.getValue());
|
||||
}
|
||||
};
|
||||
|
||||
executorService.execute(task1);
|
||||
executorService.execute(task2);
|
||||
executorService.execute(task3);
|
||||
|
||||
executorService.shutdown();
|
||||
|
||||
assertThat(counterTask1.get(), equalTo(4));
|
||||
assertThat(counterTask2.get(), equalTo(2));
|
||||
assertThat(counterTask3.get(), equalTo(0));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue