Skip to content

Commit

Permalink
Merge pull request Netflix#1147 from caarlos0/junit
Browse files Browse the repository at this point in the history
Proposal: HystrixRequestContext junit rule
  • Loading branch information
mattrjacobs committed Apr 19, 2016
2 parents 9d64acc + 3ee6006 commit bd2a6ee
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
4 changes: 4 additions & 0 deletions hystrix-contrib/hystrix-junit/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
compile project(':hystrix-core')
compile "junit:junit:4.11"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright 2016 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.hystrix.junit;

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.junit.rules.ExternalResource;

/**
* JUnit rule to be used to simplify tests that require a HystrixRequestContext.
*
* Example of usage:
*
* <blockquote>
* <pre>
* @Rule
* public HystrixRequestContextRule context = new HystrixRequestContextRule();
* </pre>
* </blockquote>
*
*/
public final class HystrixRequestContextRule extends ExternalResource {
private HystrixRequestContext context;

@Override
protected void before() throws Throwable {
this.context = HystrixRequestContext.initializeContext();
}

@Override
protected void after() {
if (this.context != null) {
this.context.shutdown();
this.context = null;
}
}

public HystrixRequestContext context() {
return this.context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.hystrix.junit;

import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.Rule;
import org.junit.Test;

public final class HystrixRequestContextRuleTest {
@Rule
public HystrixRequestContextRule request = new HystrixRequestContextRule();

@Test
public void initsContext() {
MatcherAssert.assertThat(this.request.context(), CoreMatchers.notNullValue());
}

@Test
public void manuallyShutdownContextDontBreak() {
this.request.after();
this.request.after();
MatcherAssert.assertThat(this.request.context(), CoreMatchers.nullValue());
}
}
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ include 'hystrix-core', \
'hystrix-contrib/hystrix-yammer-metrics-publisher', \
'hystrix-contrib/hystrix-network-auditor-agent', \
'hystrix-contrib/hystrix-javanica', \
'hystrix-contrib/hystrix-junit', \
'hystrix-dashboard'

project(':hystrix-contrib/hystrix-clj').name = 'hystrix-clj'
Expand All @@ -21,4 +22,5 @@ project(':hystrix-contrib/hystrix-rx-netty-metrics-stream').name = 'hystrix-rx-n
project(':hystrix-contrib/hystrix-codahale-metrics-publisher').name = 'hystrix-codahale-metrics-publisher'
project(':hystrix-contrib/hystrix-yammer-metrics-publisher').name = 'hystrix-yammer-metrics-publisher'
project(':hystrix-contrib/hystrix-network-auditor-agent').name = 'hystrix-network-auditor-agent'
project(':hystrix-contrib/hystrix-javanica').name = 'hystrix-javanica'
project(':hystrix-contrib/hystrix-javanica').name = 'hystrix-javanica'
project(':hystrix-contrib/hystrix-junit').name = 'hystrix-junit'

0 comments on commit bd2a6ee

Please sign in to comment.