fix:fix discovery junit. (#728)

pull/748/head
Haotian Zhang 2 years ago committed by GitHub
parent ce24195916
commit 06d7b51e9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,3 +11,4 @@
- [docs:update README.](https://github.com/Tencent/spring-cloud-tencent/pull/719) - [docs:update README.](https://github.com/Tencent/spring-cloud-tencent/pull/719)
- [Code optimization & correct comment](https://github.com/Tencent/spring-cloud-tencent/pull/723) - [Code optimization & correct comment](https://github.com/Tencent/spring-cloud-tencent/pull/723)
- [fix:fix javadoc.](https://github.com/Tencent/spring-cloud-tencent/pull/726) - [fix:fix javadoc.](https://github.com/Tencent/spring-cloud-tencent/pull/726)
- [fix:fix discovery junit.](https://github.com/Tencent/spring-cloud-tencent/pull/728)

@ -62,13 +62,11 @@ public class PolarisDiscoveryClientTest {
@Test @Test
public void testGetServices() { public void testGetServices() {
when(polarisServiceDiscovery.getServices()).thenReturn(singletonList(SERVICE_PROVIDER)); when(polarisServiceDiscovery.getServices()).thenReturn(singletonList(SERVICE_PROVIDER));
List<String> services = client.getServices(); List<String> services = client.getServices();
assertThat(services).contains(SERVICE_PROVIDER).size().isEqualTo(1); assertThat(services).contains(SERVICE_PROVIDER).size().isEqualTo(1);
} }
@Test @Test

@ -19,89 +19,67 @@ package com.tencent.cloud.polaris.discovery;
import java.util.List; import java.util.List;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.api.pojo.DefaultInstance;
import com.tencent.polaris.test.mock.discovery.NamingServer; import com.tencent.polaris.api.pojo.DefaultServiceInstances;
import com.tencent.polaris.test.mock.discovery.NamingService; import com.tencent.polaris.api.pojo.ServiceInfo;
import org.junit.AfterClass; import com.tencent.polaris.api.rpc.InstancesResponse;
import org.junit.BeforeClass; import com.tencent.polaris.api.rpc.ServicesResponse;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER; import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/** /**
* Test for {@link PolarisServiceDiscovery}. * Test for {@link PolarisServiceDiscovery}.
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class PolarisServiceDiscoveryTest { @RunWith(MockitoJUnitRunner.class)
private static NamingServer namingServer; public class PolarisServiceDiscoveryTest {
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PolarisContextAutoConfiguration.class,
PolarisServiceDiscoveryTest.PolarisPropertiesConfiguration.class,
PolarisDiscoveryClientConfiguration.class,
PolarisDiscoveryAutoConfiguration.class,
PolarisContextAutoConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
.withPropertyValues("server.port=" + PORT)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
.withPropertyValues("spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
@BeforeClass
public static void beforeClass() throws Exception {
namingServer = NamingServer.startNamingServer(10081);
// add service with 3 instances
NamingService.InstanceParameter instanceParameter = new NamingService.InstanceParameter();
instanceParameter.setHealthy(true);
instanceParameter.setIsolated(false);
instanceParameter.setWeight(100);
ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER);
namingServer.getNamingService().batchAddInstances(serviceKey, PORT, 3, instanceParameter);
}
@AfterClass @Mock
public static void afterClass() { private PolarisDiscoveryHandler polarisDiscoveryHandler;
if (null != namingServer) { @InjectMocks
namingServer.terminate(); private PolarisServiceDiscovery polarisServiceDiscovery;
}
}
@Test @Test
public void testGetInstances() { public void testGetInstances() {
this.contextRunner.run(context -> { DefaultServiceInstances mockDefaultServiceInstances = mock(DefaultServiceInstances.class);
PolarisServiceDiscovery polarisServiceDiscovery = context.getBean(PolarisServiceDiscovery.class); when(mockDefaultServiceInstances.getInstances()).thenReturn(singletonList(mock(DefaultInstance.class)));
InstancesResponse mockInstancesResponse = mock(InstancesResponse.class);
when(mockInstancesResponse.toServiceInstances()).thenReturn(mockDefaultServiceInstances);
when(polarisDiscoveryHandler.getHealthyInstances(anyString())).thenReturn(mockInstancesResponse);
List<ServiceInstance> serviceInstances = polarisServiceDiscovery.getInstances(SERVICE_PROVIDER); List<ServiceInstance> serviceInstances = polarisServiceDiscovery.getInstances(SERVICE_PROVIDER);
assertThat(serviceInstances.isEmpty()).isFalse();
assertThat(serviceInstances).hasSize(3); assertThat(serviceInstances).isNotEmpty();
assertThat(serviceInstances.get(0).getPort()).isEqualTo(PORT);
assertThat(serviceInstances.get(1).getPort()).isEqualTo(PORT + 1);
assertThat(serviceInstances.get(2).getPort()).isEqualTo(PORT + 2);
});
} }
@Test @Test
public void testGetServices() throws PolarisException { public void testGetServices() throws PolarisException {
this.contextRunner.run(context -> { ServiceInfo mockServiceInfo = mock(ServiceInfo.class);
PolarisServiceDiscovery polarisServiceDiscovery = context.getBean(PolarisServiceDiscovery.class); when(mockServiceInfo.getService()).thenReturn(SERVICE_PROVIDER);
ServicesResponse mockServicesResponse = mock(ServicesResponse.class);
when(mockServicesResponse.getServices()).thenReturn(singletonList(mockServiceInfo));
when(polarisDiscoveryHandler.getServices()).thenReturn(mockServicesResponse);
List<String> services = polarisServiceDiscovery.getServices(); List<String> services = polarisServiceDiscovery.getServices();
assertThat(services.size()).isEqualTo(1);
});
assertThat(services).size().isEqualTo(1);
} }
@Configuration @Configuration

Loading…
Cancel
Save