Search in sources :

Example 46 with ApolloConfigNotification

use of com.ctrip.framework.apollo.core.dto.ApolloConfigNotification in project apollo by ctripcorp.

the class NotificationControllerIntegrationTest method testPollNotificationWithDefaultNamespaceAsFile.

@Test(timeout = 5000L)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testPollNotificationWithDefaultNamespaceAsFile() throws Exception {
    AtomicBoolean stop = new AtomicBoolean();
    periodicSendMessage(executorService, assembleKey(someAppId, someCluster, defaultNamespace), stop);
    ResponseEntity<ApolloConfigNotification> result = restTemplate.getForEntity("http://{baseurl}/notifications?appId={appId}&cluster={clusterName}&namespace={namespace}", ApolloConfigNotification.class, getHostUrl(), someAppId, someCluster, defaultNamespace + ".properties");
    stop.set(true);
    ApolloConfigNotification notification = result.getBody();
    assertEquals(HttpStatus.OK, result.getStatusCode());
    assertEquals(defaultNamespace, notification.getNamespaceName());
    assertNotEquals(0, notification.getNotificationId());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 47 with ApolloConfigNotification

use of com.ctrip.framework.apollo.core.dto.ApolloConfigNotification in project apollo by ctripcorp.

the class EmbeddedApollo method mockLongPollBody.

private String mockLongPollBody(String notificationsStr) {
    List<ApolloConfigNotification> oldNotifications = GSON.fromJson(notificationsStr, notificationType);
    List<ApolloConfigNotification> newNotifications = new ArrayList<>();
    for (ApolloConfigNotification notification : oldNotifications) {
        newNotifications.add(new ApolloConfigNotification(notification.getNamespaceName(), notification.getNotificationId() + 1));
    }
    return GSON.toJson(newNotifications);
}
Also used : ArrayList(java.util.ArrayList) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)

Example 48 with ApolloConfigNotification

use of com.ctrip.framework.apollo.core.dto.ApolloConfigNotification in project apollo by ctripcorp.

the class RemoteConfigLongPollService method updateRemoteNotifications.

private void updateRemoteNotifications(List<ApolloConfigNotification> deltaNotifications) {
    for (ApolloConfigNotification notification : deltaNotifications) {
        if (Strings.isNullOrEmpty(notification.getNamespaceName())) {
            continue;
        }
        if (notification.getMessages() == null || notification.getMessages().isEmpty()) {
            continue;
        }
        ApolloNotificationMessages localRemoteMessages = m_remoteNotificationMessages.get(notification.getNamespaceName());
        if (localRemoteMessages == null) {
            localRemoteMessages = new ApolloNotificationMessages();
            m_remoteNotificationMessages.put(notification.getNamespaceName(), localRemoteMessages);
        }
        localRemoteMessages.mergeFrom(notification.getMessages());
    }
}
Also used : ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)

Example 49 with ApolloConfigNotification

use of com.ctrip.framework.apollo.core.dto.ApolloConfigNotification in project apollo by ctripcorp.

the class RemoteConfigLongPollService method notify.

private void notify(ServiceDTO lastServiceDto, List<ApolloConfigNotification> notifications) {
    if (notifications == null || notifications.isEmpty()) {
        return;
    }
    for (ApolloConfigNotification notification : notifications) {
        String namespaceName = notification.getNamespaceName();
        // create a new list to avoid ConcurrentModificationException
        List<RemoteConfigRepository> toBeNotified = Lists.newArrayList(m_longPollNamespaces.get(namespaceName));
        ApolloNotificationMessages originalMessages = m_remoteNotificationMessages.get(namespaceName);
        ApolloNotificationMessages remoteMessages = originalMessages == null ? null : originalMessages.clone();
        // since .properties are filtered out by default, so we need to check if there is any listener for it
        toBeNotified.addAll(m_longPollNamespaces.get(String.format("%s.%s", namespaceName, ConfigFileFormat.Properties.getValue())));
        for (RemoteConfigRepository remoteConfigRepository : toBeNotified) {
            try {
                remoteConfigRepository.onLongPollNotified(lastServiceDto, remoteMessages);
            } catch (Throwable ex) {
                Tracer.logError(ex);
            }
        }
    }
}
Also used : ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)

Example 50 with ApolloConfigNotification

use of com.ctrip.framework.apollo.core.dto.ApolloConfigNotification in project apollo by ctripcorp.

the class RemoteConfigRepositoryTest method testLongPollingRefresh.

@Test
public void testLongPollingRefresh() throws Exception {
    Map<String, String> configurations = ImmutableMap.of("someKey", "someValue");
    ApolloConfig someApolloConfig = assembleApolloConfig(configurations);
    when(someResponse.getStatusCode()).thenReturn(200);
    when(someResponse.getBody()).thenReturn(someApolloConfig);
    final SettableFuture<Boolean> longPollFinished = SettableFuture.create();
    RepositoryChangeListener someListener = mock(RepositoryChangeListener.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            longPollFinished.set(true);
            return null;
        }
    }).when(someListener).onRepositoryChange(any(String.class), any(Properties.class));
    RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
    remoteConfigRepository.addChangeListener(someListener);
    final ArgumentCaptor<Properties> captor = ArgumentCaptor.forClass(Properties.class);
    Map<String, String> newConfigurations = ImmutableMap.of("someKey", "anotherValue");
    ApolloConfig newApolloConfig = assembleApolloConfig(newConfigurations);
    ApolloNotificationMessages notificationMessages = new ApolloNotificationMessages();
    String someKey = "someKey";
    long someNotificationId = 1;
    notificationMessages.put(someKey, someNotificationId);
    ApolloConfigNotification someNotification = mock(ApolloConfigNotification.class);
    when(someNotification.getNamespaceName()).thenReturn(someNamespace);
    when(someNotification.getMessages()).thenReturn(notificationMessages);
    when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_OK);
    when(pollResponse.getBody()).thenReturn(Lists.newArrayList(someNotification));
    when(someResponse.getBody()).thenReturn(newApolloConfig);
    longPollFinished.get(30_000, TimeUnit.MILLISECONDS);
    remoteConfigLongPollService.stopLongPollingRefresh();
    verify(someListener, times(1)).onRepositoryChange(eq(someNamespace), captor.capture());
    assertEquals(newConfigurations, captor.getValue());
    final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
    verify(httpClient, atLeast(2)).doGet(httpRequestArgumentCaptor.capture(), eq(ApolloConfig.class));
    HttpRequest request = httpRequestArgumentCaptor.getValue();
    assertTrue(request.getUrl().contains("messages=%7B%22details%22%3A%7B%22someKey%22%3A1%7D%7D"));
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Properties(java.util.Properties) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) Test(org.junit.Test)

Aggregations

ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)53 Test (org.junit.Test)41 ApolloNotificationMessages (com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages)28 Sql (org.springframework.test.context.jdbc.Sql)26 List (java.util.List)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 ReleaseMessage (com.ctrip.framework.apollo.biz.entity.ReleaseMessage)9 ResponseEntity (org.springframework.http.ResponseEntity)8 HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)5 HttpResponse (com.ctrip.framework.apollo.util.http.HttpResponse)5 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)4 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)3 Config (com.ctrip.framework.apollo.Config)3 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)3 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)3 Map (java.util.Map)3 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)3 DeferredResultWrapper (com.ctrip.framework.apollo.configservice.wrapper.DeferredResultWrapper)2