Search in sources :

Example 81 with MockServiceManager

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());
}
Also used : SpringPointCut(com.newrelic.agent.instrumentation.pointcuts.frameworks.spring.SpringPointCut) HashMap(java.util.HashMap) MockServiceManager(com.newrelic.agent.MockServiceManager) ServiceManager(com.newrelic.agent.service.ServiceManager) MockServiceManager(com.newrelic.agent.MockServiceManager) Test(org.junit.Test)

Example 82 with MockServiceManager

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]);
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) MockRPMServiceManager(com.newrelic.agent.MockRPMServiceManager) MockServiceManager(com.newrelic.agent.MockServiceManager) ServiceManager(com.newrelic.agent.service.ServiceManager) ClassTransformerService(com.newrelic.agent.instrumentation.ClassTransformerService) FileOutputStream(java.io.FileOutputStream) IRPMService(com.newrelic.agent.IRPMService) JSONObject(org.json.simple.JSONObject) File(java.io.File) Test(org.junit.Test)

Example 83 with MockServiceManager

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");
    }
}
Also used : JfrRecorderException(com.newrelic.jfr.daemon.JfrRecorderException) MockServiceManager(com.newrelic.agent.MockServiceManager) RPMService(com.newrelic.agent.RPMService) RPMServiceManager(com.newrelic.agent.RPMServiceManager) Java10IncompatibleTest(com.newrelic.test.marker.Java10IncompatibleTest) Test(org.junit.Test) Java7IncompatibleTest(com.newrelic.test.marker.Java7IncompatibleTest) Java9IncompatibleTest(com.newrelic.test.marker.Java9IncompatibleTest)

Example 84 with MockServiceManager

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();
}
Also used : ConfigService(com.newrelic.agent.config.ConfigService) StatsService(com.newrelic.agent.stats.StatsService) MockServiceManager(com.newrelic.agent.MockServiceManager) AttributesService(com.newrelic.agent.attributes.AttributesService) JSONObject(org.json.simple.JSONObject) Before(org.junit.Before)

Example 85 with MockServiceManager

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();
}
Also used : HashMap(java.util.HashMap) EnvironmentServiceImpl(com.newrelic.agent.environment.EnvironmentServiceImpl) MockServiceManager(com.newrelic.agent.MockServiceManager) MockRPMServiceManager(com.newrelic.agent.MockRPMServiceManager) ServiceManager(com.newrelic.agent.service.ServiceManager) MockServiceManager(com.newrelic.agent.MockServiceManager) HarvestServiceImpl(com.newrelic.agent.HarvestServiceImpl) Test(org.junit.Test)

Aggregations

MockServiceManager (com.newrelic.agent.MockServiceManager)127 ConfigService (com.newrelic.agent.config.ConfigService)61 Test (org.junit.Test)49 MockRPMServiceManager (com.newrelic.agent.MockRPMServiceManager)48 HashMap (java.util.HashMap)44 TransactionService (com.newrelic.agent.TransactionService)42 MockRPMService (com.newrelic.agent.MockRPMService)37 ThreadService (com.newrelic.agent.ThreadService)34 AgentConfig (com.newrelic.agent.config.AgentConfig)34 AttributesService (com.newrelic.agent.attributes.AttributesService)28 HarvestService (com.newrelic.agent.HarvestService)27 TransactionTraceService (com.newrelic.agent.trace.TransactionTraceService)27 MockHarvestService (com.newrelic.agent.MockHarvestService)26 MockCoreService (com.newrelic.agent.MockCoreService)21 StatsService (com.newrelic.agent.stats.StatsService)21 StatsServiceImpl (com.newrelic.agent.stats.StatsServiceImpl)21 Before (org.junit.Before)19 ErrorServiceImpl (com.newrelic.agent.errors.ErrorServiceImpl)16 SqlTraceServiceImpl (com.newrelic.agent.sql.SqlTraceServiceImpl)16 SqlTraceService (com.newrelic.agent.sql.SqlTraceService)15