Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded Jackson from 1.x to 2.5.2 and split out HystrixMetricsPoller unit test #763

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hystrix-contrib/hystrix-metrics-event-stream/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies {
compile project(':hystrix-core')
compile 'org.codehaus.jackson:jackson-core-asl:1.9.2'
compile 'com.fasterxml.jackson.core:jackson-core:2.5.2'
provided 'javax.servlet:servlet-api:2.5'
provided 'junit:junit-dep:4.10'
testCompile 'junit:junit-dep:4.10'
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.netflix.hystrix.contrib.metrics.eventstream;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.StringWriter;
import java.util.concurrent.Executors;
Expand All @@ -26,19 +24,15 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.netflix.hystrix.HystrixCollapserKey;
import com.netflix.hystrix.HystrixCollapserMetrics;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.hystrix.HystrixCircuitBreaker;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandMetrics;
import com.netflix.hystrix.HystrixCommandMetrics.HealthCounts;
Expand Down Expand Up @@ -186,7 +180,7 @@ private String getCommandJson(HystrixCommandMetrics commandMetrics) throws IOExc
HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(key);

StringWriter jsonString = new StringWriter();
JsonGenerator json = jsonFactory.createJsonGenerator(jsonString);
JsonGenerator json = jsonFactory.createGenerator(jsonString);

json.writeStartObject();
json.writeStringField("type", "HystrixCommand");
Expand Down Expand Up @@ -396,74 +390,4 @@ public Thread newThread(Runnable r) {
return thread;
}
}

public static class UnitTest {

@Test
public void testStartStopStart() {
final AtomicInteger metricsCount = new AtomicInteger();

HystrixMetricsPoller poller = new HystrixMetricsPoller(new MetricsAsJsonPollerListener() {

@Override
public void handleJsonMetric(String json) {
System.out.println("Received: " + json);
metricsCount.incrementAndGet();
}
}, 100);
try {

HystrixCommand<Boolean> test = new HystrixCommand<Boolean>(HystrixCommandGroupKey.Factory.asKey("HystrixMetricsPollerTest")) {

@Override
protected Boolean run() {
return true;
}

};
test.execute();

poller.start();

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

int v1 = metricsCount.get();

assertTrue(v1 > 0);

poller.pause();

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

int v2 = metricsCount.get();

// they should be the same since we were paused
assertTrue(v2 == v1);

poller.start();

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

int v3 = metricsCount.get();

// we should have more metrics again
assertTrue(v3 > v1);

} finally {
poller.shutdown();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright 2012 Netflix, Inc.
*
* 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 com.netflix.hystrix.contrib.metrics.eventstream;

import static org.junit.Assert.*;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

/**
* Polls Hystrix metrics and output JSON strings for each metric to a MetricsPollerListener.
* <p>
* Polling can be stopped/started. Use shutdown() to permanently shutdown the poller.
*/
public class HystrixMetricsPollerTest {

@Test
public void testStartStopStart() {
final AtomicInteger metricsCount = new AtomicInteger();

HystrixMetricsPoller poller = new HystrixMetricsPoller(new HystrixMetricsPoller.MetricsAsJsonPollerListener() {

@Override
public void handleJsonMetric(String json) {
System.out.println("Received: " + json);
metricsCount.incrementAndGet();
}
}, 100);
try {

HystrixCommand<Boolean> test = new HystrixCommand<Boolean>(HystrixCommandGroupKey.Factory.asKey("HystrixMetricsPollerTest")) {

@Override
protected Boolean run() {
return true;
}

};
test.execute();

poller.start();

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

int v1 = metricsCount.get();

assertTrue(v1 > 0);

poller.pause();

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

int v2 = metricsCount.get();

// they should be the same since we were paused
assertTrue(v2 == v1);

poller.start();

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

int v3 = metricsCount.get();

// we should have more metrics again
assertTrue(v3 > v1);

} finally {
poller.shutdown();
}
}
}