Search in sources :

Example 6 with CacheEndpoint

use of org.apache.servicecomb.serviceregistry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class TestIpPortManager method testGetAvailableAddress.

@Test
public void testGetAvailableAddress(@Injectable ServiceRegistryConfig config, @Injectable InstanceCacheManager cacheManager, @Injectable InstanceCache cache) {
    ArrayList<IpPort> ipPortList = new ArrayList<>();
    ipPortList.add(new IpPort("127.0.0.1", 9980));
    ipPortList.add(new IpPort("127.0.0.1", 9981));
    new Expectations() {

        {
            config.getIpPort();
            result = ipPortList;
            config.getTransport();
            result = "rest";
            config.isRegistryAutoDiscovery();
            result = true;
        }
    };
    IpPortManager manager = new IpPortManager(config, cacheManager);
    IpPort address1 = manager.getAvailableAddress();
    if (address1.getPort() == 9980) {
        Assert.assertEquals("127.0.0.1", address1.getHostOrIp());
        Assert.assertEquals(9980, address1.getPort());
    } else {
        Assert.assertEquals("127.0.0.1", address1.getHostOrIp());
        Assert.assertEquals(9981, address1.getPort());
    }
    IpPort address2 = manager.getNextAvailableAddress(address1);
    if (address1.getPort() == 9980) {
        Assert.assertEquals("127.0.0.1", address2.getHostOrIp());
        Assert.assertEquals(9981, address2.getPort());
    } else {
        Assert.assertEquals("127.0.0.1", address2.getHostOrIp());
        Assert.assertEquals(9980, address2.getPort());
    }
    IpPort address3 = manager.getAvailableAddress();
    if (address1.getPort() == 9980) {
        Assert.assertEquals("127.0.0.1", address3.getHostOrIp());
        Assert.assertEquals(9981, address3.getPort());
    } else {
        Assert.assertEquals("127.0.0.1", address3.getHostOrIp());
        Assert.assertEquals(9980, address3.getPort());
    }
    Map<String, List<CacheEndpoint>> addresses = new HashMap<>();
    List<CacheEndpoint> instances = new ArrayList<>();
    instances.add(new CacheEndpoint("http://127.0.0.1:9982", null));
    addresses.put("rest", instances);
    new Expectations() {

        {
            cacheManager.getOrCreate("default", "SERVICECENTER", "latest");
            result = cache;
            cache.getOrCreateTransportMap();
            result = addresses;
        }
    };
    manager.initAutoDiscovery();
    IpPort address4 = manager.getNextAvailableAddress(address3);
    if (address1.getPort() == 9980) {
        Assert.assertEquals("127.0.0.1", address4.getHostOrIp());
        Assert.assertEquals(9982, address4.getPort());
    } else {
        address4 = manager.getNextAvailableAddress(address1);
        Assert.assertEquals("127.0.0.1", address4.getHostOrIp());
        Assert.assertEquals(9982, address4.getPort());
    }
    IpPort address5 = manager.getNextAvailableAddress(address4);
    Assert.assertEquals("127.0.0.1", address5.getHostOrIp());
    Assert.assertEquals(9980, address5.getPort());
}
Also used : Expectations(mockit.Expectations) HashMap(java.util.HashMap) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) ArrayList(java.util.ArrayList) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 7 with CacheEndpoint

use of org.apache.servicecomb.serviceregistry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class TestCseServer method testEqualsMethod.

@Test
public void testEqualsMethod() {
    Assert.assertFalse(cs.equals((Object) "abcd"));
    CseServer other = new CseServer(transport, new CacheEndpoint("1234", null));
    Assert.assertFalse(cs.equals(other));
    other = new CseServer(transport, new CacheEndpoint("abcd", null));
    Assert.assertTrue(cs.equals(other));
}
Also used : CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) Test(org.junit.Test)

Example 8 with CacheEndpoint

use of org.apache.servicecomb.serviceregistry.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);
    CseServer mockedServer = new CseServer(transport, new CacheEndpoint("rest:127.0.0.1:8889", null));
    Object key = Mockito.mock(Object.class);
    LoadBalancerStats stats = mock(LoadBalancerStats.class);
    Mockito.when(mockedLb.getLoadBalancerStats()).thenReturn(stats);
    Deencapsulation.invoke(rule, "chooseServerWhenTimeout", key);
    mockedServer.setAlive(true);
    mockedServer.setReadyToServe(true);
    List<Server> allServers = Arrays.asList(mockedServer);
    when(mockedLb.getReachableServers()).thenReturn(allServers);
    when(mockedLb.getAllServers()).thenReturn(allServers);
    rule.setLoadBalancer(mockedLb);
    Assert.assertEquals(rule.getLoadBalancer(), mockedLb);
    Server s = rule.choose("default");
    Assert.assertEquals(s, mockedServer);
    s = rule.choose("default");
    Assert.assertEquals(s, mockedServer);
}
Also used : Server(com.netflix.loadbalancer.Server) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) Transport(org.apache.servicecomb.core.Transport) Test(org.junit.Test)

Aggregations

CacheEndpoint (org.apache.servicecomb.serviceregistry.cache.CacheEndpoint)8 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 HashMap (java.util.HashMap)4 Expectations (mockit.Expectations)4 Transport (org.apache.servicecomb.core.Transport)4 Server (com.netflix.loadbalancer.Server)3 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)2 IRule (com.netflix.loadbalancer.IRule)2 Map (java.util.Map)2 Status (javax.ws.rs.core.Response.Status)2 Holder (javax.xml.ws.Holder)2 Deencapsulation (mockit.Deencapsulation)2 Injectable (mockit.Injectable)2 Mock (mockit.Mock)2 MockUp (mockit.MockUp)2 Mocked (mockit.Mocked)2 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)2 ConfigUtil (org.apache.servicecomb.config.ConfigUtil)2