Search in sources :

Example 6 with NamingMetadataManager

use of com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager in project nacos by alibaba.

the class ServiceMetadataProcessorTest method testOnApply.

@Test
public void testOnApply() throws NoSuchFieldException, IllegalAccessException {
    WriteRequest defaultInstance = WriteRequest.getDefaultInstance();
    Class<WriteRequest> writeRequestClass = WriteRequest.class;
    Field operation = writeRequestClass.getDeclaredField("operation_");
    operation.setAccessible(true);
    operation.set(defaultInstance, "ADD");
    MetadataOperation<ServiceMetadata> metadataOperation = new MetadataOperation<>();
    ServiceMetadata serviceMetadata = new ServiceMetadata();
    metadataOperation.setMetadata(serviceMetadata);
    metadataOperation.setServiceName("nacos");
    metadataOperation.setNamespace("namespace");
    metadataOperation.setGroup("group");
    Serializer aDefault = SerializeFactory.getDefault();
    ByteString bytes = ByteString.copyFrom(aDefault.serialize(metadataOperation));
    Field data = writeRequestClass.getDeclaredField("data_");
    data.setAccessible(true);
    data.set(defaultInstance, bytes);
    // ADD
    Response addResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Service service = Service.newService(metadataOperation.getNamespace(), metadataOperation.getGroup(), metadataOperation.getServiceName(), metadataOperation.getMetadata().isEphemeral());
    Service singleton = ServiceManager.getInstance().getSingleton(service);
    namingMetadataManager.updateServiceMetadata(singleton, metadataOperation.getMetadata());
    Assert.assertTrue(addResponse.getSuccess());
    verify(namingMetadataManager).getServiceMetadata(service);
    verify(namingMetadataManager).updateServiceMetadata(service, serviceMetadata);
    verify(context).getBean(DoubleWriteEventListener.class);
    // CHANGE
    operation.set(defaultInstance, "CHANGE");
    Response changeResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertTrue(changeResponse.getSuccess());
    verify(namingMetadataManager, times(2)).getServiceMetadata(service);
    verify(namingMetadataManager).updateServiceMetadata(service, serviceMetadata);
    verify(context, times(2)).getBean(DoubleWriteEventListener.class);
    // DELETE
    operation.set(defaultInstance, "DELETE");
    Response deleteResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertTrue(deleteResponse.getSuccess());
    verify(namingMetadataManager).removeServiceMetadata(service);
    verify(serviceStorage).removeData(service);
    // VERIFY
    operation.set(defaultInstance, "VERIFY");
    Response otherResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertFalse(otherResponse.getSuccess());
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) Field(java.lang.reflect.Field) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ByteString(com.google.protobuf.ByteString) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) Serializer(com.alibaba.nacos.consistency.Serializer) Test(org.junit.Test)

Example 7 with NamingMetadataManager

use of com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager in project nacos by alibaba.

the class NamingMetadataManagerTest method setUp.

@Before
public void setUp() throws Exception {
    namingMetadataManager = new NamingMetadataManager();
    Class<NamingMetadataManager> namingMetadataManagerClass = NamingMetadataManager.class;
    Field serviceMetadataMapField = namingMetadataManagerClass.getDeclaredField("serviceMetadataMap");
    serviceMetadataMapField.setAccessible(true);
    ConcurrentMap<Service, ServiceMetadata> serviceMetadataMap = (ConcurrentMap<Service, ServiceMetadata>) serviceMetadataMapField.get(namingMetadataManager);
    serviceMetadataMap.put(service, serviceMetadata);
    Field instanceMetadataMapField = namingMetadataManagerClass.getDeclaredField("instanceMetadataMap");
    instanceMetadataMapField.setAccessible(true);
    ConcurrentMap<Service, ConcurrentMap<String, InstanceMetadata>> instanceMetadataMap = (ConcurrentMap<Service, ConcurrentMap<String, InstanceMetadata>>) instanceMetadataMapField.get(namingMetadataManager);
    ConcurrentMap<String, InstanceMetadata> concurrentMap = new ConcurrentHashMap<>();
    concurrentMap.put(METADATA_ID, instanceMetadata);
    instanceMetadataMap.put(service, concurrentMap);
}
Also used : Field(java.lang.reflect.Field) ConcurrentMap(java.util.concurrent.ConcurrentMap) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Before(org.junit.Before)

Example 8 with NamingMetadataManager

use of com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager in project nacos by alibaba.

the class ClientBeatCheckTaskV2Test method setUp.

@Before
public void setUp() throws Exception {
    when(applicationContext.getBean(NamingMetadataManager.class)).thenReturn(namingMetadataManager);
    when(applicationContext.getBean(GlobalConfig.class)).thenReturn(globalConfig);
    when(applicationContext.getBean(DistroMapper.class)).thenReturn(distroMapper);
    when(distroMapper.responsible(anyString())).thenReturn(true);
    ApplicationUtils.injectContext(applicationContext);
    client = new IpPortBasedClient(CLIENT_ID, true);
    beatCheckTask = new ClientBeatCheckTaskV2(client);
}
Also used : IpPortBasedClient(com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient) Before(org.junit.Before)

Example 9 with NamingMetadataManager

use of com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager in project nacos by alibaba.

the class HealthCheckTaskV2Test method setUp.

@Before
public void setUp() {
    ApplicationUtils.injectContext(context);
    when(ApplicationUtils.getBean(SwitchDomain.class)).thenReturn(switchDomain);
    when(switchDomain.getTcpHealthParams()).thenReturn(new SwitchDomain.TcpHealthParams());
    when(ApplicationUtils.getBean(NamingMetadataManager.class)).thenReturn(new NamingMetadataManager());
    healthCheckTaskV2 = new HealthCheckTaskV2(ipPortBasedClient);
}
Also used : SwitchDomain(com.alibaba.nacos.naming.misc.SwitchDomain) NamingMetadataManager(com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager) Before(org.junit.Before)

Aggregations

Before (org.junit.Before)5 NamingMetadataManager (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager)4 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)3 Field (java.lang.reflect.Field)3 IpPortBasedClient (com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient)2 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)1 Serializer (com.alibaba.nacos.consistency.Serializer)1 Response (com.alibaba.nacos.consistency.entity.Response)1 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)1 ServiceManager (com.alibaba.nacos.naming.core.ServiceManager)1 InstanceMetadata (com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata)1 HealthCheckInstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)1 ServiceChangeV2Task (com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.ServiceChangeV2Task)1 ClientBeatCheckTaskV2 (com.alibaba.nacos.naming.healthcheck.heartbeat.ClientBeatCheckTaskV2)1 SwitchDomain (com.alibaba.nacos.naming.misc.SwitchDomain)1 ByteString (com.google.protobuf.ByteString)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1