use of com.newrelic.agent.MockServiceManager in project newrelic-java-agent by newrelic.
the class ClassTransformerConfigImplTest method testSpringPointcutDisabledDefaultEnabled.
@Test
public void testSpringPointcutDisabledDefaultEnabled() {
Map<String, Object> configuration = new HashMap<>();
configuration.put("class_transformer:instrumentation_default:enabled", true);
configuration.put("enable_spring_tracing", false);
configuration.put("app_name", "Unit Test");
configuration = buildConfigMap(configuration);
AgentConfig agentConfig = AgentConfigImpl.createAgentConfig(configuration);
ConfigService configService = ConfigServiceFactory.createConfigService(agentConfig, Collections.<String, Object>emptyMap());
ServiceManager serviceManager = new MockServiceManager(configService);
ServiceFactory.setServiceManager(serviceManager);
SpringPointCut springPointCut = new SpringPointCut(null, agentConfig);
assertFalse(springPointCut.isEnabled());
}
use of com.newrelic.agent.MockServiceManager in project newrelic-java-agent by newrelic.
the class ConfigServiceTest method fileChangeAndThenConnectDoesActuallyChangeConfig.
@Test
public void fileChangeAndThenConnectDoesActuallyChangeConfig() throws IOException {
ServiceManager mockServiceManager = mock(ServiceManager.class);
ClassTransformerService mockClassTransformerService = mock(ClassTransformerService.class);
when(mockServiceManager.getClassTransformerService()).thenReturn(mockClassTransformerService);
ServiceFactory.setServiceManager(mockServiceManager);
String appName = "Unit Test";
Map<String, Object> originalMap = ImmutableMap.<String, Object>of("app_name", appName);
File mockConfigFile = File.createTempFile("ConfigServiceTest", null);
try (OutputStream os = new FileOutputStream(mockConfigFile)) {
os.write(JSONObject.toJSONString(Collections.singletonMap("common", originalMap)).getBytes());
}
assertTrue(mockConfigFile.setLastModified(15L));
AgentConfig originalConfig = AgentConfigImpl.createAgentConfig(originalMap);
final Boolean[] circuitBreakerSetting = new Boolean[] { null };
assertTrue("Default circuitbreaker was expected to be true; it was apparently not.", originalConfig.getCircuitBreakerConfig().isEnabled());
ConfigServiceImpl target = new ConfigServiceImpl(originalConfig, mockConfigFile, originalMap, false);
target.addIAgentConfigListener(new AgentConfigListener() {
@Override
public void configChanged(String appName, AgentConfig agentConfig) {
circuitBreakerSetting[0] = agentConfig.getCircuitBreakerConfig().isEnabled();
}
});
// step 1: modify the file.
try (OutputStream os = new FileOutputStream(mockConfigFile)) {
os.write(JSONObject.toJSONString(Collections.singletonMap("common", ImmutableMap.of("app_name", appName, "circuitbreaker", Collections.singletonMap("enabled", false)))).getBytes());
}
assertTrue("unable to set the last modified time on the mock config file.", mockConfigFile.setLastModified(System.currentTimeMillis()));
target.afterHarvest(appName);
assertNotNull("circuitbreaker setting should have been set; it was not", circuitBreakerSetting[0]);
assertFalse("circuitbreaker setting has not changed from true to false; it should have!", circuitBreakerSetting[0]);
circuitBreakerSetting[0] = null;
// step 2: trigger connect.
IRPMService mockRPMService = mock(IRPMService.class);
when(mockRPMService.getApplicationName()).thenReturn(appName);
target.connected(mockRPMService, Collections.<String, Object>emptyMap());
// this should not have reverted to the original contents.
assertNotNull("circuitbreaker setting should have been set; it was not", circuitBreakerSetting[0]);
assertFalse("circuitbreaker setting has changed from false; it should not have!", circuitBreakerSetting[0]);
}
use of com.newrelic.agent.MockServiceManager in project newrelic-java-agent by newrelic.
the class JfrServiceTest method jfrLoopDoesStart.
@Test
public void jfrLoopDoesStart() {
JfrService jfrService = new JfrService(jfrConfig, agentConfig);
JfrService spyJfr = spy(jfrService);
when(spyJfr.coreApisExist()).thenReturn(true);
when(spyJfr.isEnabled()).thenReturn(true);
MockServiceManager manager = new MockServiceManager();
ServiceFactory.setServiceManager(manager);
RPMServiceManager mockRPMServiceManager = manager.getRPMServiceManager();
RPMService mockRPMService = mock(RPMService.class);
when(mockRPMServiceManager.getRPMService()).thenReturn(mockRPMService);
when(mockRPMService.getEntityGuid()).thenReturn("test_guid");
spyJfr.doStart();
try {
// The timeout wait is necessary because jfr loop is being executed on async thread.
verify(spyJfr, timeout(100)).startJfrLoop();
spyJfr.doStop();
} catch (JfrRecorderException e) {
fail("Should not have thrown any exception");
}
}
use of com.newrelic.agent.MockServiceManager in project newrelic-java-agent by newrelic.
the class TransactionEventTest method setup.
@Before
public void setup() {
MockServiceManager manager = new MockServiceManager();
ServiceFactory.setServiceManager(manager);
Map<String, Object> map = createConfigMap();
ConfigService configService = ConfigServiceFactory.createConfigService(AgentConfigImpl.createAgentConfig(map), map);
manager.setConfigService(configService);
manager.setStatsService(Mockito.mock(StatsService.class));
AttributesService attService = new AttributesService();
manager.setAttributesService(attService);
startTime = System.currentTimeMillis();
}
use of com.newrelic.agent.MockServiceManager in project newrelic-java-agent by newrelic.
the class TransactionEventsServiceTest method testTransactionEventFasterHarvest.
@Test
public void testTransactionEventFasterHarvest() throws Exception {
Map<String, Object> config = new HashMap<>();
config.put(AgentConfigImpl.APP_NAME, APP_NAME);
EventTestHelper.setAppName(APP_NAME);
EventTestHelper.createServiceManager(config);
service = new TransactionEventsService(mock(TransactionDataToDistributedTraceIntrinsics.class));
((MockServiceManager) ServiceFactory.getServiceManager()).setTransactionEventsService(service);
environmentService = new EnvironmentServiceImpl();
((MockServiceManager) ServiceFactory.getServiceManager()).setEnvironmentService(environmentService);
ServiceManager serviceManager = spy(ServiceFactory.getServiceManager());
ServiceFactory.setServiceManager(serviceManager);
HarvestServiceImpl harvestService = spy(new HarvestServiceImpl());
doReturn(harvestService).when(serviceManager).getHarvestService();
doReturn(0L).when(harvestService).getInitialDelay();
service.addHarvestableToService(APP_NAME);
service.configureHarvestables(60, 3);
assertEquals(3, service.getMaxSamplesStored());
service.doStart();
Map<String, Object> connectionInfo = new HashMap<>();
Map<String, Object> eventHarvest = new HashMap<>();
Map<String, Object> harvestLimits = new HashMap<>();
// 5 is the lowest allowable value
eventHarvest.put("report_period_ms", 5000L);
eventHarvest.put("harvest_limits", harvestLimits);
harvestLimits.put("analytic_event_data", 100L);
connectionInfo.put("event_harvest_config", eventHarvest);
harvestService.startHarvestables(ServiceFactory.getRPMService(), AgentConfigImpl.createAgentConfig(connectionInfo));
getEventData(APP_NAME);
Thread.sleep(500);
DistributedSamplingPriorityQueue<TransactionEvent> currentEventData = createAndSendTransaction();
assertEquals(1, currentEventData.size());
Thread.sleep(6000);
checkForEvent();
assertEquals(1, currentEventData.size());
createAndSendTransaction();
Thread.sleep(6000);
checkForEvent();
}
Aggregations