/** * Handles an event of type {@link AgentRegisteredEvent}. * * @param event * the event instance */ private void handleAgentRegisteredEvent(AgentRegisteredEvent event) { clear(event.getPlatformId()); }
/** * {@inheritDoc} */ @Override public void updateInstrumentation(Collection<Long> updatePlatformIds) { if (CollectionUtils.isEmpty(updatePlatformIds)) { throw new IllegalArgumentException("The collection of platform idents may not be null or empty."); } if (log.isInfoEnabled()) { log.info("Instrumentation updated messages flushed for the agent(s) with ID(s): {}", Arrays.toString(updatePlatformIds.toArray())); } for (long platformId : updatePlatformIds) { messageGate.flush(platformId); } }
/** * {@inheritDoc} */ @Override public synchronized void onApplicationEvent(ApplicationEvent event) { if (event instanceof ClassInstrumentationChangedEvent) { handleClassInstrumentationChangedEvent((ClassInstrumentationChangedEvent) event); } else if (event instanceof AgentDeletedEvent) { handleAgentDeletedEvent((AgentDeletedEvent) event); } else if (event instanceof AgentRegisteredEvent) { handleAgentRegisteredEvent((AgentRegisteredEvent) event); } }
@Test public void unknownPlatformId() throws Exception { when(definition.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent event = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definition)); messageGate.onApplicationEvent(event); messageGate.clear(20L); verifyZeroInteractions(messageProvider); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(1)); } }
@Test @SuppressWarnings("unchecked") public void unknownPlatformId() throws Exception { when(definition.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent event = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definition)); messageGate.onApplicationEvent(event); when(agentStatusDataProvider.getAgentStatusDataMap()).thenReturn(Collections.EMPTY_MAP); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(1)); messageGate.flush(20L); verifyZeroInteractions(messageProvider); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(1)); } }
@Test public void nullEvent() throws Exception { messageGate.onApplicationEvent(null); verifyZeroInteractions(definitionOne, definitionTwo, definitionThree, agentStatusDataProvider, statusData, messageProvider); } }
@Test public void successful() throws Exception { when(definition.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent event = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definition)); messageGate.onApplicationEvent(event); messageGate.clear(10L); verifyZeroInteractions(messageProvider); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(0)); }
@Test @SuppressWarnings("rawtypes") public void successful() throws Exception { when(definition.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent event = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definition)); messageGate.onApplicationEvent(event); when(agentStatusDataProvider.getAgentStatusDataMap()).thenReturn(ImmutableMap.of(10L, statusData)); when(statusData.getInstrumentationStatus()).thenReturn(InstrumentationStatus.PENDING); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(1)); messageGate.flush(10L); ArgumentCaptor<IAgentMessage> messageCaptor = ArgumentCaptor.forClass(IAgentMessage.class); verify(messageProvider).provideMessage(eq(10L), messageCaptor.capture()); verify(statusData).setInstrumentationStatus(InstrumentationStatus.UP_TO_DATE); verifyNoMoreInteractions(statusData, messageProvider); assertThat(((UpdatedInstrumentationMessage) messageCaptor.getValue()).getMessageContent(), contains(definition)); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(0)); }
@Test public void unknownEvent() throws Exception { messageGate.onApplicationEvent(mock(ApplicationEvent.class)); verifyZeroInteractions(definitionOne, definitionTwo, definitionThree, agentStatusDataProvider, statusData, messageProvider); }
/** * Handles an event of type {@link AgentDeletedEvent}. * * @param event * the event instance */ private void handleAgentDeletedEvent(AgentDeletedEvent event) { clear(event.getPlatformId()); }
@Test public void agentRegisteredEvent() throws Exception { when(definitionOne.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent event = new ClassInstrumentationChangedEvent(this, 10L, Collections.singletonList(definitionOne)); messageGate.onApplicationEvent(event); AgentRegisteredEvent registeredEvent = new AgentRegisteredEvent(this, 10L); messageGate.onApplicationEvent(registeredEvent); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(0)); }
@Test public void successful() { instrumentationService.updateInstrumentation(Arrays.asList(10L, 20L)); ArgumentCaptor<Long> platformIdentCaptor = ArgumentCaptor.forClass(Long.class); verify(messageGate, times(2)).flush(platformIdentCaptor.capture()); verifyNoMoreInteractions(messageGate); assertThat(platformIdentCaptor.getAllValues(), contains(10L, 20L)); }
@Test public void agentDeletedEvent() throws Exception { when(definitionOne.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent event = new ClassInstrumentationChangedEvent(this, 10L, Collections.singletonList(definitionOne)); messageGate.onApplicationEvent(event); AgentDeletedEvent deletedEvent = new AgentDeletedEvent(this, 10L); messageGate.onApplicationEvent(deletedEvent); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(0)); }
@Test @SuppressWarnings("unchecked") public void changeEventNoAgentStatusData() throws Exception { when(definitionOne.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent eventOne = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definitionOne)); when(agentStatusDataProvider.getAgentStatusDataMap()).thenReturn(Collections.EMPTY_MAP); messageGate.onApplicationEvent(eventOne); verify(agentStatusDataProvider).getAgentStatusDataMap(); verifyNoMoreInteractions(agentStatusDataProvider); verifyZeroInteractions(statusData, messageProvider); }
@Test @SuppressWarnings("unchecked") public void changeEventReplaceInstrumentationDefinition() throws Exception { when(definitionOne.getClassName()).thenReturn("class.one"); when(definitionTwo.getClassName()).thenReturn("class.two"); when(definitionThree.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent eventOne = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definitionOne, definitionTwo)); ClassInstrumentationChangedEvent eventTwo = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definitionThree)); when(agentStatusDataProvider.getAgentStatusDataMap()).thenReturn(Collections.EMPTY_MAP); when(statusData.getInstrumentationStatus()).thenReturn(InstrumentationStatus.UP_TO_DATE, InstrumentationStatus.PENDING); messageGate.onApplicationEvent(eventOne); messageGate.onApplicationEvent(eventTwo); assertThat(getDefinitionBuffer().entrySet(), hasSize(1)); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(2)); assertThat(getDefinitionBuffer().get(10L), hasEntry("class.one", definitionThree)); assertThat(getDefinitionBuffer().get(10L), hasEntry("class.two", definitionTwo)); }
@Test @SuppressWarnings("unchecked") public void changeEventAddInstrumentationDefinitionForDiffrentAgents() throws Exception { when(definitionOne.getClassName()).thenReturn("class.one"); when(definitionTwo.getClassName()).thenReturn("class.two"); when(definitionThree.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent eventOne = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definitionOne, definitionTwo)); ClassInstrumentationChangedEvent eventTwo = new ClassInstrumentationChangedEvent(this, 20L, Arrays.asList(definitionThree)); when(agentStatusDataProvider.getAgentStatusDataMap()).thenReturn(Collections.EMPTY_MAP); when(statusData.getInstrumentationStatus()).thenReturn(InstrumentationStatus.UP_TO_DATE, InstrumentationStatus.PENDING); messageGate.onApplicationEvent(eventOne); messageGate.onApplicationEvent(eventTwo); assertThat(getDefinitionBuffer().entrySet(), hasSize(2)); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(2)); assertThat(getDefinitionBuffer().get(10L), hasEntry("class.one", definitionOne)); assertThat(getDefinitionBuffer().get(10L), hasEntry("class.two", definitionTwo)); assertThat(getDefinitionBuffer().get(20L).entrySet(), hasSize(1)); assertThat(getDefinitionBuffer().get(20L), hasEntry("class.one", definitionThree)); }
@Test public void changeEventAddInstrumentationDefinitions() throws Exception { when(definitionOne.getClassName()).thenReturn("class.one"); when(definitionTwo.getClassName()).thenReturn("class.two"); when(definitionThree.getClassName()).thenReturn("class.three"); ClassInstrumentationChangedEvent eventOne = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definitionOne, definitionTwo)); ClassInstrumentationChangedEvent eventTwo = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definitionThree)); when(agentStatusDataProvider.getAgentStatusDataMap()).thenReturn(ImmutableMap.of(10L, statusData)); when(statusData.getInstrumentationStatus()).thenReturn(InstrumentationStatus.UP_TO_DATE, InstrumentationStatus.PENDING); long currentTime = System.currentTimeMillis(); messageGate.onApplicationEvent(eventOne); messageGate.onApplicationEvent(eventTwo); verify(definitionOne).getClassName(); verify(definitionTwo).getClassName(); verify(definitionThree).getClassName(); verify(agentStatusDataProvider, times(2)).getAgentStatusDataMap(); verify(statusData, times(2)).getInstrumentationStatus(); verify(statusData).setInstrumentationStatus(InstrumentationStatus.PENDING); ArgumentCaptor<Long> timeCaptor = ArgumentCaptor.forClass(Long.class); verify(statusData).setPendingSinceTime(timeCaptor.capture()); verifyNoMoreInteractions(definitionOne, definitionTwo, definitionThree, agentStatusDataProvider, statusData); verifyZeroInteractions(messageProvider); assertThat(timeCaptor.getValue(), greaterThanOrEqualTo(currentTime)); assertThat(getDefinitionBuffer().entrySet(), hasSize(1)); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(3)); assertThat(getDefinitionBuffer().get(10L), hasEntry("class.one", definitionOne)); assertThat(getDefinitionBuffer().get(10L), hasEntry("class.two", definitionTwo)); assertThat(getDefinitionBuffer().get(10L), hasEntry("class.three", definitionThree)); }