Commit cd42566d by Marcin Grzejszczak

Added tests for HystrixStream

parent 5d2065b3
......@@ -67,7 +67,8 @@ public class HystrixStreamTask implements ApplicationContextAware {
@Autowired
private HystrixStreamProperties properties;
private final LinkedBlockingQueue<String> jsonMetrics = new LinkedBlockingQueue<>(
// Visible for testing
final LinkedBlockingQueue<String> jsonMetrics = new LinkedBlockingQueue<>(
1000);
private final JsonFactory jsonFactory = new JsonFactory();
......
package org.springframework.cloud.netflix.hystrix.stream;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandMetrics;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesCommandDefault;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.BDDMockito;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.ApplicationContext;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* @author Marcin Grzejszczak
*/
@RunWith(MockitoJUnitRunner.class)
public class HystrixStreamTaskTests {
@Mock MessageChannel outboundChannel;
@Mock DiscoveryClient discoveryClient;
@Mock ApplicationContext context;
@Spy HystrixStreamProperties properties;
@Mock ServiceInstance serviceInstance;
@InjectMocks HystrixStreamTask hystrixStreamTask;
@Test
public void should_not_send_metrics_when_they_are_empty() throws Exception {
this.hystrixStreamTask.sendMetrics();
verifyZeroInteractions(this.outboundChannel);
}
@Test
public void should_send_metrics_when_they_are_not_empty() throws Exception {
this.hystrixStreamTask.jsonMetrics.put("someJson");
this.hystrixStreamTask.sendMetrics();
then(this.outboundChannel).should().send(any(Message.class));
}
@Test
public void should_gather_json_metrics() throws Exception {
HystrixCommandKey hystrixCommandKey = HystrixCommandKey.Factory.asKey("commandKey");
HystrixCommandMetrics.getInstance(hystrixCommandKey,
HystrixCommandGroupKey.Factory.asKey("commandGroupKey"),
new HystrixPropertiesCommandDefault(hystrixCommandKey, HystrixCommandProperties.defaultSetter()));
given(this.discoveryClient.getLocalServiceInstance()).willReturn(this.serviceInstance);
this.hystrixStreamTask.gatherMetrics();
assertThat(this.hystrixStreamTask.jsonMetrics.isEmpty(), is(false));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment