use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.
the class TestServiceCombLoadBalancerStats method testServiceExpire.
@Test
public void testServiceExpire(@Injectable Transport transport, @Mocked SPIServiceUtils utils, @Injectable MicroserviceInstancePing ping) {
MicroserviceInstance instance = new MicroserviceInstance();
instance.setInstanceId("instance1");
new Expectations() {
{
SPIServiceUtils.getPriorityHighestService(MicroserviceInstancePing.class);
result = ping;
ping.ping(instance);
result = false;
}
};
ServiceCombLoadBalancerStats serviceCombLoadBalancerStats = new ServiceCombLoadBalancerStats();
serviceCombLoadBalancerStats.setServerExpireInSeconds(2);
serviceCombLoadBalancerStats.setTimerIntervalInMillis(500);
serviceCombLoadBalancerStats.init();
ServiceCombServer serviceCombServer = new ServiceCombServer(null, transport, new CacheEndpoint("rest://localhost:8080", instance));
serviceCombLoadBalancerStats.markSuccess(serviceCombServer);
ServiceCombServerStats stats = serviceCombLoadBalancerStats.getServiceCombServerStats(serviceCombServer);
Assert.assertEquals(serviceCombLoadBalancerStats.getPingView().size(), 1);
await().atMost(5, TimeUnit.SECONDS).until(() -> serviceCombLoadBalancerStats.getPingView().size() <= 0);
Assert.assertEquals(serviceCombLoadBalancerStats.getPingView().size(), 0);
System.out.print(stats.getFailedRequests());
Assert.assertTrue(stats.getFailedRequests() >= 1);
}
use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.
the class TestServiceCombLoadBalancerStats method testSimpleThread.
@Test
public void testSimpleThread(@Injectable Transport transport) {
long time = System.currentTimeMillis();
MicroserviceInstance instance = new MicroserviceInstance();
instance.setInstanceId("instance1");
ServiceCombServer serviceCombServer = new ServiceCombServer(null, transport, new CacheEndpoint("rest://localhost:8080", instance));
ServiceCombLoadBalancerStats.INSTANCE.markFailure(serviceCombServer);
ServiceCombLoadBalancerStats.INSTANCE.markFailure(serviceCombServer);
Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getContinuousFailureCount(), 2);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(serviceCombServer);
Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getContinuousFailureCount(), 0);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(serviceCombServer);
Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getTotalRequests(), 4);
Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRate(), 50);
Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getSuccessRate(), 50);
Assert.assertTrue(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getLastVisitTime() <= System.currentTimeMillis() && ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getLastVisitTime() >= time);
}
use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.
the class TestSessionSticknessRule method testLastServerNotExist.
@Test
public void testLastServerNotExist() {
SessionStickinessRule rule = new SessionStickinessRule();
Transport transport = mock(Transport.class);
Invocation invocation = mock(Invocation.class);
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setInstanceId("1234");
ServiceCombServer mockedServer = new ServiceCombServer(null, transport, new CacheEndpoint("rest:127.0.0.1:8890", instance1));
mockedServer.setAlive(true);
mockedServer.setReadyToServe(true);
mockedServer.setId("mockedServer");
List<ServiceCombServer> allServers = Arrays.asList(mockedServer);
LoadBalancer lb = new LoadBalancer(rule, "mockedServer");
when(invocation.getLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST)).thenReturn(allServers);
rule.setLoadBalancer(lb);
ServiceCombServer server = new ServiceCombServer(null, transport, new CacheEndpoint("rest:127.0.0.1:8890", instance1));
Deencapsulation.setField(rule, "lastServer", server);
new MockUp<SessionStickinessRule>(rule) {
@Mock
private boolean isTimeOut() {
return false;
}
@Mock
private boolean isErrorThresholdMet() {
return false;
}
};
Server s = rule.choose(allServers, invocation);
Assert.assertEquals(mockedServer, s);
}
use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.
the class TestSessionSticknessRule method testRuleFullOperation.
@Test
public void testRuleFullOperation() {
SessionStickinessRule rule = new SessionStickinessRule();
LoadBalancer mockedLb = mock(LoadBalancer.class);
Transport transport = mock(Transport.class);
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setInstanceId("1234");
ServiceCombServer mockedServer = new ServiceCombServer(null, transport, new CacheEndpoint("rest:127.0.0.1:8889", instance1));
Invocation invocation = mock(Invocation.class);
LoadBalancerStats stats = mock(LoadBalancerStats.class);
Mockito.when(mockedLb.getLoadBalancerStats()).thenReturn(stats);
Deencapsulation.invoke(rule, "chooseServerWhenTimeout", Arrays.asList(mockedServer), invocation);
mockedServer.setAlive(true);
mockedServer.setReadyToServe(true);
List<ServiceCombServer> allServers = Arrays.asList(mockedServer);
rule.setLoadBalancer(mockedLb);
Server s = rule.choose(allServers, invocation);
Assert.assertEquals(s, mockedServer);
s = rule.choose(allServers, invocation);
Assert.assertEquals(s, mockedServer);
}
use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.
the class IsolationDiscoveryFilterTest method before.
@Before
public void before() {
discoveryContext = new DiscoveryContext();
discoveryContext.setInputParameters(invocation);
discoveryTreeNode = new DiscoveryTreeNode();
Mockito.doAnswer(a -> a.getArguments()[0]).when(transport).parseAddress(Mockito.anyString());
data = new HashMap<>();
for (int i = 0; i < 3; ++i) {
MicroserviceInstance instance = new MicroserviceInstance();
instance.setInstanceId("i" + i);
String endpoint = "rest://127.0.0.1:" + i;
instance.setEndpoints(Collections.singletonList(endpoint));
data.put(instance.getInstanceId(), instance);
ServiceCombServer serviceCombServer = new ServiceCombServer(invocation.getMicroserviceName(), transport, new CacheEndpoint(endpoint, instance));
ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer);
}
discoveryTreeNode.data(data);
filter = new IsolationDiscoveryFilter();
TestServiceCombServerStats.releaseTryingChance();
}
Aggregations