Search in sources :

Example 6 with Configuration

use of com.tencent.polaris.api.config.Configuration in project polaris-java by polarismesh.

the class ServiceExpireTest method testServiceExpiration.

@Test
public void testServiceExpiration() {
    ConsumerAPI consumerAPI = null;
    try {
        Configuration configuration = createConfiguration();
        consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration);
        Runnable firstThread = new GetServiceTask(consumerAPI, null, 0);
        Runnable secondThread = new GetServiceTask(consumerAPI, null, 1);
        firstThread.run();
        Utils.sleepUninterrupted(2000);
        secondThread.run();
        LOG.info("wait for expire");
        Utils.sleepUninterrupted(6000);
        firstThread.run();
        secondThread.run();
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        if (null != consumerAPI) {
            consumerAPI.destroy();
        }
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) IOException(java.io.IOException) PolarisException(com.tencent.polaris.api.exception.PolarisException) Test(org.junit.Test)

Example 7 with Configuration

use of com.tencent.polaris.api.config.Configuration in project polaris-java by polarismesh.

the class ConsumerTest method testGetNotExistsService.

@Test
public void testGetNotExistsService() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        for (int i = 0; i < 10; i++) {
            GetOneInstanceRequest request = new GetOneInstanceRequest();
            request.setNamespace(NAMESPACE_TEST);
            request.setService(NOT_EXISTS_SERVICE);
            InstancesResponse oneInstance = consumerAPI.getOneInstance(request);
            Assert.assertFalse(oneInstance.isServiceExist());
        }
        // 把实例加上去,可以重新获取
        InstanceParameter parameter = new InstanceParameter();
        parameter.setHealthy(true);
        parameter.setIsolated(false);
        parameter.setWeight(100);
        namingServer.getNamingService().batchAddInstances(new ServiceKey(NAMESPACE_TEST, NOT_EXISTS_SERVICE), 10100, 10, parameter);
        GetOneInstanceRequest request = new GetOneInstanceRequest();
        request.setNamespace(NAMESPACE_TEST);
        request.setService(NOT_EXISTS_SERVICE);
        InstancesResponse oneInstance = consumerAPI.getOneInstance(request);
        Assert.assertEquals(1, oneInstance.getInstances().length);
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) InstanceParameter(com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 8 with Configuration

use of com.tencent.polaris.api.config.Configuration in project polaris-java by polarismesh.

the class ConsumerTest method testUseBuiltinAsDiscover.

@Test
public void testUseBuiltinAsDiscover() {
    ValidParam validParam = validParams.get(Operation.ALL_HEALTHY);
    Configuration configuration = TestUtils.configWithEnvAddress();
    ConfigurationImpl configImpl = (ConfigurationImpl) configuration;
    configImpl.setDefault();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        for (int i = 0; i < ITERATE_COUNT; i++) {
            GetOneInstanceRequest request = new GetOneInstanceRequest();
            request.setNamespace(NAMESPACE_TEST);
            request.setService(validParam.getServiceName());
            InstancesResponse instancesResponse = consumerAPI.getOneInstance(request);
            Assert.assertEquals(1, instancesResponse.getInstances().length);
            Instance instance = instancesResponse.getInstances()[0];
            Assert.assertTrue(instance.isHealthy());
            Assert.assertFalse(instance.isIsolated());
            Assert.assertEquals(100, instance.getWeight());
        }
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) Instance(com.tencent.polaris.api.pojo.Instance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) ConfigurationImpl(com.tencent.polaris.factory.config.ConfigurationImpl) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 9 with Configuration

use of com.tencent.polaris.api.config.Configuration in project polaris-java by polarismesh.

the class ConsumerTest method concurrentTestSyncGetOneInstance.

public void concurrentTestSyncGetOneInstance(Operation operation) {
    ValidParam validParam = validParams.get(operation);
    Configuration configuration = TestUtils.configWithEnvAddress();
    ExecutorService executorService = Executors.newCachedThreadPool();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < ITERATE_COUNT; i++) {
            executorService.submit(new Runnable() {

                @Override
                public void run() {
                    GetOneInstanceRequest request = new GetOneInstanceRequest();
                    request.setNamespace(NAMESPACE_TEST);
                    request.setService(validParam.getServiceName());
                    InstancesResponse instancesResponse = consumerAPI.getOneInstance(request);
                    Assert.assertEquals(1, instancesResponse.getInstances().length);
                    Instance instance = instancesResponse.getInstances()[0];
                    Assert.assertTrue(instance.isHealthy());
                    Assert.assertFalse(instance.isIsolated());
                    Assert.assertEquals(100, instance.getWeight());
                }
            });
        }
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) Instance(com.tencent.polaris.api.pojo.Instance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) ExecutorService(java.util.concurrent.ExecutorService) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 10 with Configuration

use of com.tencent.polaris.api.config.Configuration in project polaris-java by polarismesh.

the class ConsumerTest method commonTestSyncGetInstances.

public void commonTestSyncGetInstances(Operation operation) {
    ValidParam validParam = validParams.get(operation);
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        for (int i = 0; i < ITERATE_COUNT; i++) {
            GetInstancesRequest request = new GetInstancesRequest();
            request.setNamespace(NAMESPACE_TEST);
            request.setService(validParam.getServiceName());
            InstancesResponse instancesResponse = consumerAPI.getInstances(request);
            Assert.assertEquals(validParam.getCountHealth(), instancesResponse.getInstances().length);
            Assert.assertEquals(validParam.getCountHealth() * 100, instancesResponse.getTotalWeight());
        }
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Aggregations

Configuration (com.tencent.polaris.api.config.Configuration)25 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)18 Test (org.junit.Test)16 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)15 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)9 PolarisException (com.tencent.polaris.api.exception.PolarisException)7 Instance (com.tencent.polaris.api.pojo.Instance)6 HashMap (java.util.HashMap)6 GetInstancesRequest (com.tencent.polaris.api.rpc.GetInstancesRequest)5 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)3 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)3 ConfigurationImpl (com.tencent.polaris.factory.config.ConfigurationImpl)3 IOException (java.io.IOException)3 ProviderAPI (com.tencent.polaris.api.core.ProviderAPI)2 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)2 HttpServer (com.sun.net.httpserver.HttpServer)1 StaticMetadataManager (com.tencent.cloud.common.metadata.StaticMetadataManager)1 DiscoveryPropertiesAutoConfiguration (com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration)1 PolarisDiscoveryProperties (com.tencent.cloud.polaris.PolarisDiscoveryProperties)1 APIConfig (com.tencent.polaris.api.config.global.APIConfig)1