final ViewProcessorImpl vp = env.getViewProcessor();
vp.start();
final ViewClient client1 = vp.createViewClient(ViewProcessorTestEnvironment.TEST_USER);
client1.setFragmentResultMode(ViewResultMode.FULL_ONLY);
final TestViewResultListener client1ResultListener = new TestViewResultListener();
client1.setResultListener(client1ResultListener);
assertEquals(0, client1ResultListener.getQueueSize());
client1.attachToViewProcess(env.getViewDefinition().getUniqueId(), ExecutionOptions.infinite(MarketData.live(), ExecutionFlags.none().get()));
// Wait for first computation cycle
client1ResultListener.assertViewDefinitionCompiled(TIMEOUT);
client1ResultListener.expectNextCall(CycleStartedCall.class, TIMEOUT);
client1ResultListener.assertCycleFragmentCompleted(TIMEOUT);
client1ResultListener.assertCycleCompleted(TIMEOUT);
final ViewClient client2 = vp.createViewClient(ViewProcessorTestEnvironment.TEST_USER);
client2.setFragmentResultMode(ViewResultMode.FULL_ONLY);
final TestViewResultListener client2ResultListener = new TestViewResultListener();
client2.setResultListener(client2ResultListener);
assertEquals(0, client2ResultListener.getQueueSize());
client2.attachToViewProcess(env.getViewDefinition().getUniqueId(), ExecutionOptions.infinite(MarketData.live(), ExecutionFlags.none().get()));
// Initial result should be pushed through
client2ResultListener.assertViewDefinitionCompiled(TIMEOUT);
client2ResultListener.assertCycleCompleted(TIMEOUT);
final ViewProcessImpl viewProcess1 = env.getViewProcess(vp, client1.getUniqueId());
final ViewProcessImpl viewProcess2 = env.getViewProcess(vp, client2.getUniqueId());
assertEquals(viewProcess1, viewProcess2);
client1.pause();
client1ResultListener.assertNoCalls(TIMEOUT);
// Now client 1 is paused, so any changes should be batched.
marketDataProvider.addValue(ViewProcessorTestEnvironment.getPrimitive1(), (byte) 1);
env.getCurrentWorker(viewProcess1).requestCycle();
client2ResultListener.assertCycleStarted(TIMEOUT);
client2ResultListener.assertCycleFragmentCompleted(TIMEOUT);
client2ResultListener.assertCycleCompleted(TIMEOUT);
assertEquals(0, client2ResultListener.getQueueSize());
client1ResultListener.assertNoCalls(TIMEOUT);
marketDataProvider.addValue(ViewProcessorTestEnvironment.getPrimitive1(), (byte) 2);
env.getCurrentWorker(viewProcess1).requestCycle();
client2ResultListener.assertCycleStarted(TIMEOUT);
client2ResultListener.assertCycleFragmentCompleted(TIMEOUT);
client2ResultListener.assertCycleCompleted(TIMEOUT);
assertEquals(0, client2ResultListener.getQueueSize());
client1ResultListener.assertNoCalls(TIMEOUT);
// Resuming should release the most recent result to the client
client1.resume();
client1ResultListener.getCycleStarted(TIMEOUT).getCycleMetadata();
final ViewComputationResultModel result2Fragment = client1ResultListener.getCycleFragmentCompleted(TIMEOUT).getFullFragment();
final ViewComputationResultModel result2 = client1ResultListener.getCycleCompleted(TIMEOUT).getFullResult();
Map<ValueRequirement, Object> expected = new HashMap<ValueRequirement, Object>();
expected.put(ViewProcessorTestEnvironment.getPrimitive1(), (byte) 2);
expected.put(ViewProcessorTestEnvironment.getPrimitive2(), (byte) 0);
assertComputationResult(expected, env.getCalculationResult(result2Fragment));
assertComputationResult(expected, env.getCalculationResult(result2));
// Changes should now propagate straight away to both listeners
marketDataProvider.addValue(ViewProcessorTestEnvironment.getPrimitive1(), (byte) 3);
env.getCurrentWorker(viewProcess1).requestCycle();
client1ResultListener.assertCycleStarted(TIMEOUT);
client2ResultListener.assertCycleStarted(TIMEOUT);
client2ResultListener.assertCycleFragmentCompleted(TIMEOUT);
client2ResultListener.assertCycleCompleted(TIMEOUT);
final ViewComputationResultModel result3Fragment = client1ResultListener.getCycleFragmentCompleted(TIMEOUT).getFullFragment();
final ViewComputationResultModel result3 = client1ResultListener.getCycleCompleted(TIMEOUT).getFullResult();
expected = new HashMap<ValueRequirement, Object>();
expected.put(ViewProcessorTestEnvironment.getPrimitive1(), (byte) 3);
expected.put(ViewProcessorTestEnvironment.getPrimitive2(), (byte) 0);
assertComputationResult(expected, env.getCalculationResult(result3Fragment));
assertComputationResult(expected, env.getCalculationResult(result3));
// Pause results again and we should be back to merging both whole cycle results and fragments
client1.pause();
client2ResultListener.assertNoCalls(TIMEOUT);
client1ResultListener.assertNoCalls(TIMEOUT);
marketDataProvider.addValue(ViewProcessorTestEnvironment.getPrimitive2(), (byte) 1);
env.getCurrentWorker(viewProcess1).requestCycle();
client2ResultListener.assertCycleStarted(TIMEOUT);
client2ResultListener.assertCycleFragmentCompleted(TIMEOUT);
client2ResultListener.assertCycleCompleted(TIMEOUT);
assertEquals(0, client2ResultListener.getQueueSize());
client1ResultListener.assertNoCalls(TIMEOUT);
marketDataProvider.addValue(ViewProcessorTestEnvironment.getPrimitive2(), (byte) 2);
env.getCurrentWorker(viewProcess1).requestCycle();
client2ResultListener.assertCycleStarted(TIMEOUT);
client2ResultListener.assertCycleFragmentCompleted(TIMEOUT);
client2ResultListener.assertCycleCompleted(TIMEOUT);
assertEquals(0, client2ResultListener.getQueueSize());
client1ResultListener.assertNoCalls(TIMEOUT);
// Start results again
client1.resume();
client1ResultListener.assertCycleStarted(TIMEOUT);
final ViewComputationResultModel result4Fragment = client1ResultListener.getCycleFragmentCompleted(TIMEOUT).getFullFragment();
final ViewComputationResultModel result4 = client1ResultListener.getCycleCompleted(TIMEOUT).getFullResult();
assertEquals(0, client1ResultListener.getQueueSize());
client2ResultListener.assertNoCalls(TIMEOUT);
expected = new HashMap<ValueRequirement, Object>();
expected.put(ViewProcessorTestEnvironment.getPrimitive1(), (byte) 3);
expected.put(ViewProcessorTestEnvironment.getPrimitive2(), (byte) 2);
assertComputationResult(expected, env.getCalculationResult(result4Fragment));
assertComputationResult(expected, env.getCalculationResult(result4));
client1.detachFromViewProcess();
client2ResultListener.assertNoCalls(TIMEOUT);
client1ResultListener.assertNoCalls(TIMEOUT);
client1.shutdown();
client1ResultListener.assertClientShutdown(TIMEOUT);
client2.setResultListener(null);
client2.shutdown();
client2ResultListener.assertNoCalls(TIMEOUT);
}