@Test
public void testEventStream() {

    PublishSubject<AgentEvent> eventSubject = PublishSubject.create();
    when(agentManagementService.events(false)).thenReturn(eventSubject);
    ExtTestSubscriber<AgentStatus> testSubscriber = new ExtTestSubscriber<>();
    monitor.monitor().subscribe(testSubscriber);
    // First started
    eventSubject.onNext(new AgentInstanceUpdateEvent(STARTED_INSTANCE));
    assertThat(testSubscriber.takeNext().getStatusCode()).isEqualTo(AgentStatusCode.Healthy);
    // Now terminated
    eventSubject.onNext(new AgentInstanceRemovedEvent(STARTED_INSTANCE.getId()));
    AgentStatus terminatedStatus = testSubscriber.takeNext();
    assertThat(terminatedStatus.getStatusCode()).isEqualTo(AgentStatusCode.Terminated);
    assertThat(terminatedStatus.getAgentInstance().getId()).isEqualTo(STARTED_INSTANCE.getId());
    assertThat(terminatedStatus.getAgentInstance().getLifecycleStatus().getState()).isEqualTo(InstanceLifecycleState.Stopped);
}