Skip to content

Commit

Permalink
Added await step to metrics-concurrency test and reordered command ex…
Browse files Browse the repository at this point in the history
…ecution in circuitbreaker test
  • Loading branch information
Matt Jacobs committed Jun 13, 2016
1 parent 2e32ecc commit d47ba09
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@

import com.netflix.hystrix.HystrixCircuitBreaker.HystrixCircuitBreakerImpl;
import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifierDefault;
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
import com.netflix.hystrix.util.HystrixRollingNumberEvent;
import rx.Observable;

/**
Expand Down Expand Up @@ -211,9 +209,9 @@ public void testCircuitDoesNotTripOnFailuresBelowThreshold() {
cmd3.execute();
HystrixCommand<Boolean> cmd4 = new SuccessCommand(key, 1);
cmd4.execute();
HystrixCommand<Boolean> cmd5 = new FailureCommand(key, 1);
HystrixCommand<Boolean> cmd5 = new SuccessCommand(key, 1);
cmd5.execute();
HystrixCommand<Boolean> cmd6 = new SuccessCommand(key, 1);
HystrixCommand<Boolean> cmd6 = new FailureCommand(key, 1);
cmd6.execute();
HystrixCommand<Boolean> cmd7 = new SuccessCommand(key, 1);
cmd7.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import com.hystrix.junit.HystrixRequestContextRule;
import com.netflix.hystrix.exception.HystrixBadRequestException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import rx.Observable;
import rx.Subscriber;
import rx.observers.SafeSubscriber;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;


public class HystrixCommandMetricsTest {

@Rule
public HystrixRequestContextRule ctx = new HystrixRequestContextRule();

@Before
public void init() {
HystrixCommandMetrics.reset();
Expand Down Expand Up @@ -128,26 +141,53 @@ public void testBadRequestsDoNotAffectErrorPercentage() {
}

@Test
public void testCurrentConcurrentExecutionCount() {
public void testCurrentConcurrentExecutionCount() throws InterruptedException {
String key = "cmd-metrics-C";

HystrixCommandMetrics metrics = null;
List<Observable<Boolean>> cmdResults = new ArrayList<Observable<Boolean>>();

int NUM_CMDS = 8;
for (int i = 0; i < NUM_CMDS; i++) {
HystrixCommand<Boolean> cmd = new SuccessCommand(key, 900);
if (metrics == null) {
metrics = cmd.metrics;
}
cmd.queue();
Observable<Boolean> eagerObservable = cmd.observe();
cmdResults.add(eagerObservable);
}

try {
Thread.sleep(150);
} catch (InterruptedException ie) {
fail(ie.getMessage());
}
System.out.println("ReqLog: " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
assertEquals(NUM_CMDS, metrics.getCurrentConcurrentExecutionCount());

final CountDownLatch latch = new CountDownLatch(1);
Observable.merge(cmdResults).subscribe(new Subscriber<Boolean>() {
@Override
public void onCompleted() {
System.out.println("All commands done");
latch.countDown();
}

@Override
public void onError(Throwable e) {
System.out.println("Error duing command execution");
e.printStackTrace();
latch.countDown();
}

@Override
public void onNext(Boolean aBoolean) {

}
});

latch.await(10000, TimeUnit.MILLISECONDS);
assertEquals(0, metrics.getCurrentConcurrentExecutionCount());
}

private class Command extends HystrixCommand<Boolean> {
Expand Down

0 comments on commit d47ba09

Please sign in to comment.