Added missing tests

This commit is contained in:
Marcin Grzejszczak
2016-03-09 17:49:22 +01:00
parent 3e9a2252a3
commit 24fa3514da
6 changed files with 399 additions and 8 deletions

View File

@@ -33,8 +33,8 @@ import org.springframework.cloud.sleuth.TraceKeys;
*/
public class TraceableScheduledExecutorService extends TraceableExecutorService implements ScheduledExecutorService {
public TraceableScheduledExecutorService(final ScheduledExecutorService delegate,
final Tracer tracer, TraceKeys traceKeys, SpanNamer spanNamer) {
public TraceableScheduledExecutorService(ScheduledExecutorService delegate,
Tracer tracer, TraceKeys traceKeys, SpanNamer spanNamer) {
super(delegate, tracer, traceKeys, spanNamer);
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.instrument.async;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cloud.sleuth.SpanNamer;
import org.springframework.cloud.sleuth.TraceKeys;
import org.springframework.cloud.sleuth.Tracer;
import static org.mockito.BDDMockito.then;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
/**
* @author Marcin Grzejszczak
*/
@RunWith(MockitoJUnitRunner.class)
public class TraceableScheduledExecutorServiceTest {
@Mock Tracer tracer;
@Mock TraceKeys traceKeys;
@Mock SpanNamer spanNamer;
@Mock ScheduledExecutorService scheduledExecutorService;
@InjectMocks TraceableScheduledExecutorService traceableScheduledExecutorService;
@Test
public void should_schedule_a_local_component_trace_runnable() throws Exception {
this.traceableScheduledExecutorService.schedule(aRunnable(), 1L, TimeUnit.DAYS);
then(this.scheduledExecutorService).should().schedule(any(
LocalComponentTraceRunnable.class), anyLong(), any(TimeUnit.class));
}
@Test
@SuppressWarnings("unchecked")
public void should_schedule_a_local_component_trace_callable() throws Exception {
this.traceableScheduledExecutorService.schedule(aCallable(), 1L, TimeUnit.DAYS);
then(this.scheduledExecutorService).should().schedule(any(
LocalComponentTraceCallable.class), anyLong(), any(TimeUnit.class));
}
@Test
public void should_schedule_at_fixed_rate_a_local_component_trace_runnable() throws Exception {
this.traceableScheduledExecutorService.scheduleAtFixedRate(aRunnable(), 1L, 1L, TimeUnit.DAYS);
then(this.scheduledExecutorService).should().scheduleAtFixedRate(any(
LocalComponentTraceRunnable.class), anyLong(), anyLong(), any(TimeUnit.class));
}
@Test
public void should_schedule_with_fixed_delay_a_local_component_trace_runnable() throws Exception {
this.traceableScheduledExecutorService.scheduleWithFixedDelay(aRunnable(), 1L, 1L, TimeUnit.DAYS);
then(this.scheduledExecutorService).should().scheduleWithFixedDelay(any(
LocalComponentTraceRunnable.class), anyLong(), anyLong(), any(TimeUnit.class));
}
Runnable aRunnable() {
return () -> {};
}
Callable aCallable() {
return () -> null;
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.sampler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.SpanAccessor;
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.BDDMockito.given;
/**
* @author Marcin Grzejszczak
*/
@RunWith(MockitoJUnitRunner.class)
public class IsTracingSamplerTest {
@Mock SpanAccessor spanAccessor;
@Mock Span span;
@InjectMocks IsTracingSampler isTracingSampler;
@Test
public void should_sample_when_tracing_is_on() throws Exception {
given(this.spanAccessor.isTracing()).willReturn(true);
then(this.isTracingSampler.isSampled(this.span)).isTrue();
}
@Test
public void should_not_sample_when_tracing_is_off() throws Exception {
given(this.spanAccessor.isTracing()).willReturn(false);
then(this.isTracingSampler.isSampled(this.span)).isFalse();
}
}

View File

@@ -0,0 +1,121 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.stream;
import java.net.URI;
import java.util.Map;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.commons.util.InetUtils;
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.BDDMockito.given;
/**
* @author Marcin Grzejszczak
*/
public class DiscoveryClientHostLocatorTest {
DiscoveryClient discoveryClient = Mockito.mock(DiscoveryClient.class);
DiscoveryClientHostLocator discoveryClientHostLocator =
new DiscoveryClientHostLocator(this.discoveryClient);
@Test(expected = IllegalArgumentException.class)
public void should_throw_exception_when_no_discovery_client_is_present() throws Exception {
new DiscoveryClientHostLocator(null);
}
@Test
public void should_create_Host_with_0_ip_when_exception_occurs_on_resolving_host() throws Exception {
given(this.discoveryClient.getLocalServiceInstance()).willReturn(serviceInstanceWithInvalidHost());
Host host = this.discoveryClientHostLocator.locate(null);
then(host.getServiceName()).isEqualTo("serviceId");
then(host.getPort()).isEqualTo((short)8_000);
then(host.getIpv4()).isEqualTo(0);
}
@Test
public void should_create_valid_Host_when_proper_host_is_passed() throws Exception {
given(this.discoveryClient.getLocalServiceInstance()).willReturn(serviceInstanceWithValidHost());
Host host = this.discoveryClientHostLocator.locate(null);
then(host.getServiceName()).isEqualTo("serviceId");
then(host.getPort()).isEqualTo((short)8_000);
then(host.getIpv4()).isEqualTo(InetUtils.getIpAddressAsInt("localhost"));
}
private ServiceInstance serviceInstanceWithInvalidHost() {
return new ServiceInstance() {
@Override public String getServiceId() {
return "serviceId";
}
@Override public String getHost() {
throw new RuntimeException();
}
@Override public int getPort() {
return 8000;
}
@Override public boolean isSecure() {
return false;
}
@Override public URI getUri() {
return null;
}
@Override public Map<String, String> getMetadata() {
return null;
}
};
}
private ServiceInstance serviceInstanceWithValidHost() {
return new ServiceInstance() {
@Override public String getServiceId() {
return "serviceId";
}
@Override public String getHost() {
return "localhost";
}
@Override public int getPort() {
return 8000;
}
@Override public boolean isSecure() {
return false;
}
@Override public URI getUri() {
return null;
}
@Override public Map<String, String> getMetadata() {
return null;
}
};
}
}

View File

@@ -0,0 +1,128 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.zipkin;
import java.net.URI;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.sleuth.zipkin.DiscoveryClientEndpointLocator.NoServiceInstanceAvailableException;
import zipkin.Endpoint;
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.BDDMockito.given;
/**
* @author Marcin Grzejszczak
*/
@RunWith(MockitoJUnitRunner.class)
public class DiscoveryClientEndpointLocatorTest {
@Mock DiscoveryClient discoveryClient;
@InjectMocks DiscoveryClientEndpointLocator discoveryClientEndpointLocator;
@Test(expected = NoServiceInstanceAvailableException.class)
public void should_throw_exception_when_no_instances_are_available() throws Exception {
this.discoveryClientEndpointLocator.local();
}
@Test
public void should_create_endpoint_with_0_ip_when_exception_occurs_on_resolving_host() throws Exception {
given(this.discoveryClient.getLocalServiceInstance()).willReturn(serviceInstanceWithInvalidHost());
Endpoint local = this.discoveryClientEndpointLocator.local();
then(local.serviceName).isEqualTo("serviceid");
then(local.port).isEqualTo((short)8_000);
then(local.ipv4).isEqualTo(0);
}
@Test
public void should_create_valid_endpoint_when_proper_host_is_passed() throws Exception {
given(this.discoveryClient.getLocalServiceInstance()).willReturn(serviceInstanceWithValidHost());
Endpoint local = this.discoveryClientEndpointLocator.local();
then(local.serviceName).isEqualTo("serviceid");
then(local.port).isEqualTo((short)8_000);
then(local.ipv4).isEqualTo(InetUtils.getIpAddressAsInt("localhost"));
}
private ServiceInstance serviceInstanceWithInvalidHost() {
return new ServiceInstance() {
@Override public String getServiceId() {
return "serviceId";
}
@Override public String getHost() {
throw new RuntimeException();
}
@Override public int getPort() {
return 8000;
}
@Override public boolean isSecure() {
return false;
}
@Override public URI getUri() {
return null;
}
@Override public Map<String, String> getMetadata() {
return null;
}
};
}
private ServiceInstance serviceInstanceWithValidHost() {
return new ServiceInstance() {
@Override public String getServiceId() {
return "serviceId";
}
@Override public String getHost() {
return "localhost";
}
@Override public int getPort() {
return 8000;
}
@Override public boolean isSecure() {
return false;
}
@Override public URI getUri() {
return null;
}
@Override public Map<String, String> getMetadata() {
return null;
}
};
}
}

View File

@@ -2,12 +2,13 @@ package org.springframework.cloud.sleuth.zipkin;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.BDDMockito;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import zipkin.Endpoint;
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.BDDMockito.given;
@RunWith(MockitoJUnitRunner.class)
public class FallbackHavingEndpointLocatorTests {
@@ -18,7 +19,7 @@ public class FallbackHavingEndpointLocatorTests {
@Test
public void should_use_system_property_locator_if_discovery_client_locator_is_not_present() {
BDDMockito.given(this.serverPropertiesEndpointLocator.local()).willReturn(this.expectedEndpoint);
given(this.serverPropertiesEndpointLocator.local()).willReturn(this.expectedEndpoint);
FallbackHavingEndpointLocator sut = new FallbackHavingEndpointLocator(null,
this.serverPropertiesEndpointLocator);
@@ -29,8 +30,8 @@ public class FallbackHavingEndpointLocatorTests {
@Test
public void should_use_system_property_locator_if_discovery_client_locator_throws_an_exception() {
BDDMockito.given(this.discoveryClientEndpointLocator.local()).willThrow(new RuntimeException());
BDDMockito.given(this.serverPropertiesEndpointLocator.local()).willReturn(this.expectedEndpoint);
given(this.discoveryClientEndpointLocator.local()).willThrow(new RuntimeException());
given(this.serverPropertiesEndpointLocator.local()).willReturn(this.expectedEndpoint);
FallbackHavingEndpointLocator sut = new FallbackHavingEndpointLocator(this.discoveryClientEndpointLocator,
this.serverPropertiesEndpointLocator);
@@ -41,8 +42,8 @@ public class FallbackHavingEndpointLocatorTests {
@Test
public void should_use_discovery_client_locator_by_default() {
BDDMockito.given(this.discoveryClientEndpointLocator.local()).willReturn(this.expectedEndpoint);
BDDMockito.given(this.serverPropertiesEndpointLocator.local()).willThrow(new RuntimeException());
given(this.discoveryClientEndpointLocator.local()).willReturn(this.expectedEndpoint);
given(this.serverPropertiesEndpointLocator.local()).willThrow(new RuntimeException());
FallbackHavingEndpointLocator sut = new FallbackHavingEndpointLocator(this.discoveryClientEndpointLocator,
this.serverPropertiesEndpointLocator);