use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class RefreshableMicroserviceCacheTest method refresh.
@Test
public void refresh() {
ArrayList<MicroserviceInstance> instances = new ArrayList<>();
findServiceInstancesOprHolder.value = params -> {
Assert.assertEquals("consumerId", params[0]);
Assert.assertEquals("app", params[1]);
Assert.assertEquals("svc", params[2]);
Assert.assertEquals("0.0.0.0+", params[3]);
Assert.assertNull(params[4]);
MicroserviceInstances microserviceInstances = new MicroserviceInstances();
microserviceInstances.setNeedRefresh(true);
microserviceInstances.setRevision("rev0");
microserviceInstances.setMicroserviceNotExist(false);
FindInstancesResponse instancesResponse = new FindInstancesResponse();
instancesResponse.setInstances(instances);
microserviceInstances.setInstancesResponse(instancesResponse);
return microserviceInstances;
};
// at the beginning, no instances in cache
List<MicroserviceInstance> cachedInstances = microserviceCache.getInstances();
Assert.assertEquals(0, cachedInstances.size());
Assert.assertNull(microserviceCache.getRevisionId());
// find 1 instance from sc
MicroserviceInstance microserviceInstance = new MicroserviceInstance();
instances.add(microserviceInstance);
microserviceInstance.setInstanceId("instanceId00");
microserviceCache.refresh();
Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
cachedInstances = microserviceCache.getInstances();
Assert.assertEquals(1, cachedInstances.size());
MicroserviceInstance instance = cachedInstances.iterator().next();
Assert.assertEquals("instanceId00", instance.getInstanceId());
Assert.assertEquals("rev0", microserviceCache.getRevisionId());
// 2nd time, find 2 instances, one of them is the old instance
MicroserviceInstance microserviceInstance1 = new MicroserviceInstance();
instances.add(microserviceInstance1);
microserviceInstance1.setInstanceId("instanceId01");
findServiceInstancesOprHolder.value = params -> {
Assert.assertEquals("consumerId", params[0]);
Assert.assertEquals("app", params[1]);
Assert.assertEquals("svc", params[2]);
Assert.assertEquals("0.0.0.0+", params[3]);
Assert.assertEquals("rev0", params[4]);
MicroserviceInstances microserviceInstances = new MicroserviceInstances();
microserviceInstances.setNeedRefresh(true);
microserviceInstances.setRevision("rev1");
microserviceInstances.setMicroserviceNotExist(false);
FindInstancesResponse instancesResponse = new FindInstancesResponse();
instancesResponse.setInstances(instances);
microserviceInstances.setInstancesResponse(instancesResponse);
return microserviceInstances;
};
microserviceCache.refresh();
Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
cachedInstances = microserviceCache.getInstances();
Assert.assertEquals(2, cachedInstances.size());
Assert.assertEquals("instanceId00", cachedInstances.get(0).getInstanceId());
Assert.assertEquals("instanceId01", cachedInstances.get(1).getInstanceId());
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestMicroserviceInstanceRegisterTask method setup.
@Before
public void setup() {
ConfigUtil.installDynamicConfig();
eventBus = new EventBus();
taskList = new ArrayList<>();
eventBus.register(new Object() {
@Subscribe
public void onEvent(MicroserviceInstanceRegisterTask task) {
taskList.add(task);
}
});
microservice = new Microservice();
microservice.setAppId("app");
microservice.setServiceName("ms");
microservice.setServiceId("serviceId");
microservice.setInstance(new MicroserviceInstance());
HealthCheck healthCheck = new HealthCheck();
healthCheck.setMode(HealthCheckMode.HEARTBEAT);
microservice.getInstance().setHealthCheck(healthCheck);
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestMicroserviceInstanceRegisterTask method registerHostSuccess.
@Test
public void registerHostSuccess() {
MicroserviceInstance instance = microservice.getInstance();
new Expectations(RegistrationManager.class) {
{
RegistrationManager.getPublishHostName();
result = "hostName";
}
};
new Expectations(RegistryUtils.class) {
{
serviceRegistryConfig.isPreferIpAddress();
result = false;
serviceRegistryConfig.getHeartbeatInterval();
result = 10;
serviceRegistryConfig.getResendHeartBeatTimes();
result = 20;
srClient.registerMicroserviceInstance(instance);
result = "instanceId";
}
};
MicroserviceInstanceRegisterTask registerTask = new MicroserviceInstanceRegisterTask(eventBus, serviceRegistryConfig, srClient, microservice);
registerTask.taskStatus = TaskStatus.READY;
registerTask.run();
Assert.assertEquals(true, registerTask.isRegistered());
Assert.assertEquals("hostName", instance.getHostName());
Assert.assertEquals("instanceId", instance.getInstanceId());
Assert.assertEquals(10, instance.getHealthCheck().getInterval());
Assert.assertEquals(20, instance.getHealthCheck().getTimes());
Assert.assertEquals(1, taskList.size());
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestMicroserviceRegisterTask method setup.
@Before
public void setup() {
collector = new LogCollector();
eventBus = new EventBus();
taskList = new ArrayList<>();
eventBus.register(new Object() {
@Subscribe
public void onEvent(MicroserviceRegisterTask task) {
taskList.add(task);
}
});
microservice = new Microservice();
microservice.setAppId("app");
microservice.setServiceName("ms");
microservice.setInstance(new MicroserviceInstance());
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestLoadBalanceHandler2 method testConfigEndpoint.
@Test
public void testConfigEndpoint() {
ReferenceConfig referenceConfig = Mockito.mock(ReferenceConfig.class);
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
InvocationRuntimeType invocationRuntimeType = Mockito.mock(InvocationRuntimeType.class);
SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
MicroserviceMeta microserviceMeta = Mockito.mock(MicroserviceMeta.class);
when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
when(schemaMeta.getMicroserviceName()).thenReturn("testMicroserviceName");
when(microserviceMeta.getAppId()).thenReturn("testApp");
when(referenceConfig.getVersionRule()).thenReturn("0.0.0+");
when(referenceConfig.getTransport()).thenReturn("rest");
Invocation invocation = new Invocation(referenceConfig, operationMeta, invocationRuntimeType, new HashMap<>());
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
InstanceCacheManager instanceCacheManager = Mockito.mock(InstanceCacheManager.class);
TransportManager transportManager = Mockito.mock(TransportManager.class);
Transport transport = Mockito.mock(Transport.class);
ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", "false");
// set up data
MicroserviceInstance myself = new MicroserviceInstance();
MicroserviceInstance findInstance = new MicroserviceInstance();
List<String> findEndpoint = new ArrayList<>();
findEndpoint.add("rest://localhost:9092");
findInstance.setEndpoints(findEndpoint);
findInstance.setInstanceId("findInstance");
Map<String, MicroserviceInstance> data = new HashMap<>();
DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
scbEngine.setTransportManager(transportManager);
SCBEngine.getInstance().setTransportManager(transportManager);
LocalRegistryStore.INSTANCE.initSelfWithMocked(null, myself);
mockUpInstanceCacheManager(instanceCacheManager);
when(instanceCacheManager.getOrCreateVersionedCache("testApp", "testMicroserviceName", "0.0.0+")).thenReturn(parent);
when(transportManager.findTransport("rest")).thenReturn(transport);
data.put("findInstance", findInstance);
parent.cacheVersion(1);
LoadbalanceHandler handler = new LoadbalanceHandler();
try {
handler.handle(invocation, asyncResp);
} catch (Exception e) {
}
Assert.assertEquals("rest://localhost:9092", invocation.getEndpoint().getEndpoint());
// reset
invocation.setEndpoint(null);
// success
invocation.addLocalContext("scb-endpoint", "rest://127.0.0.1:8080?sslEnabled=true&protocol=http2");
try {
handler.handle(invocation, asyncResp);
} catch (Exception e) {
}
Assert.assertEquals("rest://127.0.0.1:8080?sslEnabled=true&protocol=http2", invocation.getEndpoint().getEndpoint());
// reset
invocation.setEndpoint(null);
// endpoint format is not correct
invocation.addLocalContext("scb-endpoint", "127.0.0.1:8080");
try {
handler.handle(invocation, asyncResp);
Assert.assertEquals("endpoint's format is not correct, throw exception", " but not throw exception");
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("Illegal character in scheme name"));
}
// reset
invocation.setEndpoint(null);
// transport is not find
invocation.addLocalContext("scb-endpoint", "my://127.0.0.1:8080?sslEnabled=true&protocol=http2");
try {
handler.handle(invocation, asyncResp);
Assert.assertEquals("endpoint's transport not found, throw exception", "but not throw exception");
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("the endpoint's transport is not found."));
}
}
Aggregations