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();
}
}
}
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);
}
}
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());
}
}
}
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());
}
});
}
}
}
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());
}
}
}
Aggregations