Skip to content

Commit

Permalink
Unit test for timeout race
Browse files Browse the repository at this point in the history
  • Loading branch information
benjchristensen committed May 2, 2014
1 parent 58da8b3 commit 1ef8171
Showing 1 changed file with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.netflix.hystrix;
import org.junit.Test;

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

public class HystrixCommandTimeoutConcurrencyTesting {

@Test
public void testTimeoutRace() {
for (int i = 0; i < 2000; i++) {
String a = null;
String b = null;
try {
HystrixRequestContext.initializeContext();
a = new TestCommand().execute();
b = new TestCommand().execute();
if (a == null || b == null) {
System.err.println("Received NULL!");
throw new RuntimeException("Received NULL");
}

for (HystrixExecutableInfo<?> hi : HystrixRequestLog.getCurrentRequest().getAllExecutedCommands()) {
if (hi.isResponseTimedOut() && hi.getExecutionEvents().size() == 1) {
System.err.println("Missing fallback status!");
throw new RuntimeException("Missing fallback status on timeout.");
}
}

} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
throw new RuntimeException(e);
} finally {
System.out.println(a + " " + b + " ==> " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
HystrixRequestContext.getContextForCurrentThread().shutdown();
}
}

Hystrix.reset();
}

public static class TestCommand extends HystrixCommand<String> {

protected TestCommand() {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("testTimeoutConcurrency"))
.andCommandKey(HystrixCommandKey.Factory.asKey("testTimeoutConcurrencyCommand"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionIsolationThreadTimeoutInMilliseconds(1)));
}

@Override
protected String run() throws Exception {
// throw new RuntimeException("test");
// Thread.sleep(5);
return "hello";
}

@Override
protected String getFallback() {
return "failed";
}

}
}

0 comments on commit 1ef8171

Please sign in to comment.