Search in sources :

Example 81 with MicroserviceInstance

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);
}
Also used : Expectations(mockit.Expectations) Invocation(org.apache.servicecomb.core.Invocation) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Endpoint(org.apache.servicecomb.core.Endpoint) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 82 with MicroserviceInstance

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);
}
Also used : Expectations(mockit.Expectations) Invocation(org.apache.servicecomb.core.Invocation) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Endpoint(org.apache.servicecomb.core.Endpoint) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 83 with MicroserviceInstance

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());
}
Also used : Expectations(mockit.Expectations) ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 84 with MicroserviceInstance

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));
}
Also used : Expectations(mockit.Expectations) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) RSAKeyPairEntry(org.apache.servicecomb.foundation.common.utils.RSAKeyPairEntry) HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) RSAConsumerTokenManager(org.apache.servicecomb.authentication.consumer.RSAConsumerTokenManager) Test(org.junit.Test)

Example 85 with MicroserviceInstance

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));
}
Also used : HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Aggregations

MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)232 Test (org.junit.Test)136 ArrayList (java.util.ArrayList)60 HashMap (java.util.HashMap)42 Invocation (org.apache.servicecomb.core.Invocation)40 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)40 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)38 Expectations (mockit.Expectations)36 MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)32 CacheEndpoint (org.apache.servicecomb.registry.cache.CacheEndpoint)30 List (java.util.List)22 Transport (org.apache.servicecomb.core.Transport)22 Before (org.junit.Before)22 Map (java.util.Map)18 TransportManager (org.apache.servicecomb.core.transport.TransportManager)18 InstanceCacheManager (org.apache.servicecomb.registry.cache.InstanceCacheManager)18 MockUp (mockit.MockUp)16 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)14 ServiceCombServer (org.apache.servicecomb.loadbalance.ServiceCombServer)14 Server (com.netflix.loadbalancer.Server)12