Search in sources :

Example 1 with DefaultServiceInfo

use of com.navercorp.pinpoint.profiler.context.DefaultServiceInfo in project pinpoint by naver.

the class AgentInfoSenderTest method agentInfoShouldBeRefreshedOnServerMetaDataChangeFromMultipleThreads.

@Test
public void agentInfoShouldBeRefreshedOnServerMetaDataChangeFromMultipleThreads() {
    // Given
    final long agentInfoSendRetryIntervalMs = 1000L;
    final int threadCount = 50;
    final CountDownLatch initLatch = new CountDownLatch(threadCount);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch endLatch = new CountDownLatch(threadCount);
    final ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
    final Queue<Throwable> exceptions = new ConcurrentLinkedQueue<>();
    final ResponseServerMessageListenerFactory messageListenerFactory = new ResponseServerMessageListenerFactory();
    ResponseServerMessageListener messageListener = messageListenerFactory.create();
    TestPinpointServerAcceptor testPinpointServerAcceptor = new TestPinpointServerAcceptor(messageListenerFactory);
    int bindPort = testPinpointServerAcceptor.bind();
    PinpointClientFactory clientFactory = createPinpointClientFactory();
    EnhancedDataSender<MetaDataType> dataSender = newTcpDataSender(clientFactory, bindPort);
    final AgentInfoSender agentInfoSender = new AgentInfoSender.Builder(dataSender, agentInfoFactory).sendInterval(agentInfoSendRetryIntervalMs).setMessageConverter(resultResponseMessageConverter).build();
    serverMetaDataRegistryService.addListener(new ServerMetaDataRegistryService.OnChangeListener() {

        @Override
        public void onServerMetaDataChange() {
            agentInfoSender.refresh();
        }
    });
    // When
    for (int i = 0; i < threadCount; i++) {
        final String serviceName = "/name" + i;
        executorService.submit(new Runnable() {

            @Override
            public void run() {
                initLatch.countDown();
                try {
                    startLatch.await();
                    ServiceInfo serviceInfo = new DefaultServiceInfo(serviceName, Collections.<String>emptyList());
                    serverMetaDataRegistryService.addServiceInfo(serviceInfo);
                    serverMetaDataRegistryService.notifyListeners();
                } catch (final Throwable t) {
                    exceptions.add(t);
                } finally {
                    endLatch.countDown();
                }
            }
        });
    }
    await(initLatch, 3000);
    startLatch.countDown();
    await(endLatch, 3000);
    executorService.shutdown();
    try {
        waitExpectedRequestCount(threadCount, messageListener);
        waitExpectedSuccessCount(threadCount, messageListener);
    } finally {
        closeAll(agentInfoSender, clientFactory);
        testPinpointServerAcceptor.close();
    }
    // Then
    assertTrue("Failed with exceptions : " + exceptions, exceptions.isEmpty());
    assertEquals(threadCount, messageListener.getRequestCount());
    assertEquals(threadCount, messageListener.getSuccessCount());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ServiceInfo(com.navercorp.pinpoint.bootstrap.context.ServiceInfo) DefaultServiceInfo(com.navercorp.pinpoint.profiler.context.DefaultServiceInfo) TestPinpointServerAcceptor(com.navercorp.pinpoint.test.server.TestPinpointServerAcceptor) PinpointClientFactory(com.navercorp.pinpoint.rpc.client.PinpointClientFactory) DefaultPinpointClientFactory(com.navercorp.pinpoint.rpc.client.DefaultPinpointClientFactory) MetaDataType(com.navercorp.pinpoint.profiler.metadata.MetaDataType) ExecutorService(java.util.concurrent.ExecutorService) ServerMetaDataRegistryService(com.navercorp.pinpoint.profiler.context.ServerMetaDataRegistryService) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) DefaultServiceInfo(com.navercorp.pinpoint.profiler.context.DefaultServiceInfo) Test(org.junit.Test)

Aggregations

ServiceInfo (com.navercorp.pinpoint.bootstrap.context.ServiceInfo)1 DefaultServiceInfo (com.navercorp.pinpoint.profiler.context.DefaultServiceInfo)1 ServerMetaDataRegistryService (com.navercorp.pinpoint.profiler.context.ServerMetaDataRegistryService)1 MetaDataType (com.navercorp.pinpoint.profiler.metadata.MetaDataType)1 DefaultPinpointClientFactory (com.navercorp.pinpoint.rpc.client.DefaultPinpointClientFactory)1 PinpointClientFactory (com.navercorp.pinpoint.rpc.client.PinpointClientFactory)1 TestPinpointServerAcceptor (com.navercorp.pinpoint.test.server.TestPinpointServerAcceptor)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Test (org.junit.Test)1