use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestLoadBalanceCreator method testLoadBalanceWithRoundRobinRuleAndFilter.
@Test
public void testLoadBalanceWithRoundRobinRuleAndFilter(@Injectable Invocation invocation, @Injectable Transport transport) {
// Robin components implementations require getReachableServers & getServerList have the same size, we add a test case for this.
RoundRobinRuleExt rule = new RoundRobinRuleExt();
List<ServiceCombServer> servers = new ArrayList<>();
Endpoint host1 = new Endpoint(transport, "host1");
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setInstanceId("instance1");
ServiceCombServer server = new ServiceCombServer(null, host1, instance1);
Endpoint host2 = new Endpoint(transport, "host2");
MicroserviceInstance instance2 = new MicroserviceInstance();
instance2.setInstanceId("instance2");
ServiceCombServer server2 = new ServiceCombServer(null, host2, instance2);
servers.add(server);
servers.add(server2);
LoadBalancer lb = new LoadBalancer(rule, "test");
List<ServerListFilterExt> filters = new ArrayList<>();
filters.add(new ServerListFilterExt() {
@Override
public List<ServiceCombServer> getFilteredListOfServers(List<ServiceCombServer> serverList, Invocation invocation) {
List<ServiceCombServer> filteredServers = new ArrayList<>();
for (ServiceCombServer server : servers) {
if (server.getHost().equals("host1")) {
continue;
}
filteredServers.add(server);
}
return filteredServers;
}
});
lb.setFilters(filters);
new Expectations() {
{
invocation.getLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST);
result = servers;
}
};
Server s = lb.chooseServer(invocation);
Assert.assertEquals(server2, s);
s = lb.chooseServer(invocation);
Assert.assertEquals(server2, s);
s = lb.chooseServer(invocation);
Assert.assertEquals(server2, s);
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestLoadBalanceCreator method testLoadBalanceWithRandomRuleAndFilter.
@Test
public void testLoadBalanceWithRandomRuleAndFilter(@Injectable Invocation invocation, @Injectable Transport transport) {
// Robin components implementations require getReachableServers & getServerList have the same size, we add a test case for this.
RandomRuleExt rule = new RandomRuleExt();
LoadBalancer lb = new LoadBalancer(rule, "service");
List<ServiceCombServer> servers = new ArrayList<>();
Endpoint host1 = new Endpoint(transport, "host1");
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setInstanceId("instance1");
ServiceCombServer server = new ServiceCombServer(null, host1, instance1);
Endpoint host2 = new Endpoint(transport, "host2");
MicroserviceInstance instance2 = new MicroserviceInstance();
instance2.setInstanceId("instance2");
ServiceCombServer server2 = new ServiceCombServer(null, host2, instance2);
servers.add(server);
servers.add(server2);
List<ServerListFilterExt> filters = new ArrayList<>();
filters.add(new ServerListFilterExt() {
@Override
public List<ServiceCombServer> getFilteredListOfServers(List<ServiceCombServer> serverList, Invocation invocation) {
List<ServiceCombServer> filteredServers = new ArrayList<>();
for (ServiceCombServer server : servers) {
if (server.getHost().equals("host1")) {
continue;
}
filteredServers.add(server);
}
return filteredServers;
}
});
lb.setFilters(filters);
new Expectations() {
{
invocation.getLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST);
result = servers;
}
};
Server s = lb.chooseServer(invocation);
Assert.assertEquals(server2, s);
s = lb.chooseServer(invocation);
Assert.assertEquals(server2, s);
s = lb.chooseServer(invocation);
Assert.assertEquals(server2, s);
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestServerDiscoveryFilter method createEndpointNormal.
@Test
public void createEndpointNormal(@Injectable DiscoveryContext context, @Injectable Invocation invocation) {
new Expectations(transportManager) {
{
transportManager.findTransport(anyString);
result = trasport;
context.getInputParameters();
result = invocation;
invocation.getMicroserviceName();
result = "test";
}
};
MicroserviceInstance instance = new MicroserviceInstance();
instance.setInstanceId("0000001");
ServiceCombServer server = (ServiceCombServer) filter.createEndpoint(context, Const.RESTFUL, "rest://localhost:8080", instance);
Assert.assertSame(instance, server.getInstance());
Assert.assertSame(trasport, server.getEndpoint().getTransport());
Assert.assertEquals("rest://localhost:8080", server.getEndpoint().getEndpoint());
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestRSAProviderTokenManager method testTokenFromValidatePool.
@Test
public void testTokenFromValidatePool() {
RSAKeyPairEntry rsaKeyPairEntry = RSAUtils.generateRSAKeyPair();
RSAKeypair4Auth.INSTANCE.setPrivateKey(rsaKeyPairEntry.getPrivateKey());
RSAKeypair4Auth.INSTANCE.setPublicKey(rsaKeyPairEntry.getPublicKey());
RSAKeypair4Auth.INSTANCE.setPublicKeyEncoded(rsaKeyPairEntry.getPublicKeyEncoded());
String serviceId = "c8636e5acf1f11e7b701286ed488fc20";
String instanceId = "e8a04b54cf2711e7b701286ed488fc20";
RSAConsumerTokenManager rsaConsumerTokenManager = new RSAConsumerTokenManager();
MicroserviceInstance microserviceInstance = new MicroserviceInstance();
microserviceInstance.setInstanceId(instanceId);
Map<String, String> properties = new HashMap<>();
microserviceInstance.setProperties(properties);
properties.put(DefinitionConst.INSTANCE_PUBKEY_PRO, rsaKeyPairEntry.getPublicKeyEncoded());
Microservice microservice = new Microservice();
microservice.setServiceId(serviceId);
new Expectations(RegistrationManager.INSTANCE) {
{
RegistrationManager.INSTANCE.getMicroservice();
result = microservice;
RegistrationManager.INSTANCE.getMicroserviceInstance();
result = microserviceInstance;
}
};
// Test Consumer first create token
String token = rsaConsumerTokenManager.getToken();
Assert.assertNotNull(token);
// use cache token
Assert.assertEquals(token, rsaConsumerTokenManager.getToken());
new Expectations(MicroserviceInstanceCache.class) {
{
MicroserviceInstanceCache.getOrCreate(serviceId, instanceId);
result = microserviceInstance;
MicroserviceInstanceCache.getOrCreate(serviceId);
result = microservice;
}
};
RSAProviderTokenManager rsaProviderTokenManager = new RSAProviderTokenManager();
// first validate need to verify use RSA
Assert.assertTrue(rsaProviderTokenManager.valid(token));
// second validate use validated pool
Assert.assertTrue(rsaProviderTokenManager.valid(token));
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestRSAProviderTokenManager method testTokenExpired.
@Test
public void testTokenExpired() {
String tokenStr = "e8a04b54cf2711e7b701286ed488fc20@c8636e5acf1f11e7b701286ed488fc20@1511315597475@9t0tp8ce80SUM5ts6iRGjFJMvCdQ7uvhpyh0RM7smKm3p4wYOrojr4oT1Pnwx7xwgcgEFbQdwPJxIMfivpQ1rHGqiLp67cjACvJ3Ke39pmeAVhybsLADfid6oSjscFaJ@WBYouF6hXYrXzBA31HC3VX8Bw9PNgJUtVqOPAaeW9ye3q/D7WWb0M+XMouBIWxWY6v9Un1dGu5Rkjlx6gZbnlHkb2VO8qFR3Y6lppooWCirzpvEBRjlJQu8LPBur0BCfYGq8XYrEZA2NU6sg2zXieqCSiX6BnMnBHNn4cR9iZpk=";
RSAProviderTokenManager tokenManager = new RSAProviderTokenManager();
MicroserviceInstance microserviceInstance = new MicroserviceInstance();
Map<String, String> properties = new HashMap<>();
microserviceInstance.setProperties(properties);
properties.put(DefinitionConst.INSTANCE_PUBKEY_PRO, "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxKl5TNUTec7fL2degQcCk6vKf3c0wsfNK5V6elKzjWxm0MwbRj/UeR20VSnicBmVIOWrBS9LiERPPvjmmWUOSS2vxwr5XfhBhZ07gCAUNxBOTzgMo5nE45DhhZu5Jzt5qSV6o10Kq7+fCCBlDZ1UoWxZceHkUt5AxcrhEDulFjQIDAQAB");
Assert.assertFalse(tokenManager.valid(tokenStr));
}
Aggregations