public void testTaskAggregateProfileCounter() throws Exception {
final String name = "counter";
ProfileCollector collector = getCollector(serverNode);
ProfileConsumer cons1 = collector.getConsumer("c1");
// Register a counter to be noted at all profiling levels
final AggregateProfileCounter counter =
(AggregateProfileCounter)
cons1.createCounter(name,
ProfileDataType.TASK_AND_AGGREGATE,
ProfileLevel.MIN);
// Because the listener is running in a different thread, JUnit
// is not able to report the assertions and failures.
// Use an exchanger to synchronize between the threads and communicate
// any problems.
final Exchanger<AssertionError> errorExchanger =
new Exchanger<AssertionError>();
// Set up a couple of test listeners, each listening for a different
// task owner
final Identity positiveOwner = new DummyIdentity("hello");
final Identity negativeOwner = new DummyIdentity("neg-hello");
SimpleTestListener test = new SimpleTestListener(
new CounterReportRunnable(counter.getName(),
negativeOwner, positiveOwner,
errorExchanger, 1));
profileCollector.addListener(test, true);
// We expect to see the counter updated in the task report
txnScheduler.runTask(
new TestAbstractKernelRunnable() {
public void run() {
counter.incrementCount();
}
}, positiveOwner);
AssertionError error =
errorExchanger.exchange(null, 100, TimeUnit.MILLISECONDS);
if (error != null) {
// Rethrow with the original error as the cause so we see
// both stack traces.
throw new AssertionError(error);
}
// We expect to see the counter updated in the task report,
// and it should be independent of the last report.
// Note that we assume the profile listener for the last task
// has already been called at this point. This is safe, because
// we're using the ErrorExchanger to ensure the profile report for
// the above task has been seen.
txnScheduler.runTask(
new TestAbstractKernelRunnable() {
public void run() {
counter.incrementCount();
}
}, positiveOwner);
error = errorExchanger.exchange(null, 100, TimeUnit.MILLISECONDS);
if (error != null) {
// Rethrow with the original error as the cause so we see
// both stack traces.
throw new AssertionError(error);
}
// And we expect to see the counter aggregated
assertEquals(2, counter.getCount());
}