env.init();
env.getViewDefinition().setPersistent(true);
final ViewProcessorImpl vp = env.getViewProcessor();
vp.start();
final ViewClient client = vp.createViewClient(ViewProcessorTestEnvironment.TEST_USER);
final TestViewResultListener resultListener = new TestViewResultListener();
client.setResultListener(resultListener);
client.attachToViewProcess(env.getViewDefinition().getUniqueId(), ExecutionOptions.infinite(MarketData.live(), ExecutionFlags.none().get()));
final ViewProcessImpl viewProcess = env.getViewProcess(vp, client.getUniqueId());
assertEquals(ViewProcessState.RUNNING, viewProcess.getState());
resultListener.expectNextCall(ViewDefinitionCompiledCall.class, Timeout.standardTimeoutMillis());
resultListener.expectNextCall(CycleCompletedCall.class, Timeout.standardTimeoutMillis());
resultListener.assertNoCalls(Timeout.standardTimeoutMillis());
client.detachFromViewProcess();
assertEquals(ViewProcessState.RUNNING, viewProcess.getState());
resultListener.assertNoCalls(Timeout.standardTimeoutMillis());
client.attachToViewProcess(env.getViewDefinition().getUniqueId(), ExecutionOptions.infinite(MarketData.live(), ExecutionFlags.none().get()));
final ViewProcessImpl viewProcess2 = env.getViewProcess(vp, client.getUniqueId());
assertEquals(viewProcess, viewProcess2);
resultListener.expectNextCall(ViewDefinitionCompiledCall.class, Timeout.standardTimeoutMillis());
resultListener.expectNextCall(CycleCompletedCall.class, Timeout.standardTimeoutMillis());
client.detachFromViewProcess();
// Still want to be able to shut down manually, e.g. through JMX
assertEquals(1, vp.getViewProcesses().size());
viewProcess.shutdown();
assertEquals(ViewProcessState.TERMINATED, viewProcess.getState());