Search in sources :

Example 6 with ServiceInstancesByProto

use of com.tencent.polaris.client.pojo.ServiceInstancesByProto in project spring-cloud-tencent by Tencent.

the class PolarisServiceStatusChangeListenerTest method testOnResourceUpdated.

@Test
public void testOnResourceUpdated() {
    PolarisServiceStatusChangeListener polarisServiceStatusChangeListener = new PolarisServiceStatusChangeListener();
    polarisServiceStatusChangeListener.setApplicationEventPublisher(publisher);
    // Service update event
    ServiceEventKey serviceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.SERVICE);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setNamespace(NAMESPACE_TEST);
    serviceInfo.setService(SERVICE_PROVIDER);
    // Need update
    ServicesByProto oldServices = new ServicesByProto(Collections.emptyList());
    ServicesByProto newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
    verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
    // No need update
    oldServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
    verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
    // Instance update event
    ServiceEventKey instanceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.INSTANCE);
    DefaultInstance instance = new DefaultInstance();
    instance.setNamespace(NAMESPACE_TEST);
    instance.setService(SERVICE_PROVIDER);
    instance.setHost(HOST);
    instance.setPort(PORT);
    try {
        Field instances = ServiceInstancesByProto.class.getDeclaredField("instances");
        instances.setAccessible(true);
        // Need update
        ServiceInstancesByProto oldInstances = new ServiceInstancesByProto();
        instances.set(oldInstances, Collections.emptyList());
        ServiceInstancesByProto newInstances = new ServiceInstancesByProto();
        instances.set(newInstances, Collections.singletonList(instance));
        polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
        verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
        // No need update
        oldInstances = new ServiceInstancesByProto();
        instances.set(oldInstances, Collections.singletonList(instance));
        newInstances = new ServiceInstancesByProto();
        instances.set(newInstances, Collections.singletonList(instance));
        polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
        verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
    } catch (NoSuchFieldException | IllegalAccessException e) {
        Assertions.fail("Exception encountered.", e);
    }
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) Field(java.lang.reflect.Field) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ApplicationEvent(org.springframework.context.ApplicationEvent) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto) ServicesByProto(com.tencent.polaris.client.pojo.ServicesByProto) Test(org.junit.Test)

Aggregations

ServiceInstancesByProto (com.tencent.polaris.client.pojo.ServiceInstancesByProto)6 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)4 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)2 Instance (com.tencent.polaris.api.pojo.Instance)2 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)2 ServicesByProto (com.tencent.polaris.client.pojo.ServicesByProto)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Sets (com.google.common.collect.Sets)1 AbstractResourceEventListener (com.tencent.polaris.api.plugin.registry.AbstractResourceEventListener)1 CircuitBreakerStatus (com.tencent.polaris.api.pojo.CircuitBreakerStatus)1 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)1 RegistryCacheValue (com.tencent.polaris.api.pojo.RegistryCacheValue)1 ServiceChangeEvent (com.tencent.polaris.api.pojo.ServiceChangeEvent)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)1 StatusDimension (com.tencent.polaris.api.pojo.StatusDimension)1 Level (com.tencent.polaris.api.pojo.StatusDimension.Level)1