Skip to content

Commit

Permalink
Merge pull request #763 from mattrjacobs/upgrade-jackson-for-metrics-…
Browse files Browse the repository at this point in the history
…stream

Upgraded Jackson from 1.x to 2.5.2 and split out HystrixMetricsPoller unit test
  • Loading branch information
mattrjacobs committed Apr 18, 2015
2 parents 8c74e00 + 8867a04 commit be4da47
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 81 deletions.
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();
}
}
}

0 comments on commit be4da47

Please sign in to comment.