forked from Netflix/Hystrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Netflix#1147 from caarlos0/junit
Proposal: HystrixRequestContext junit rule
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dependencies { | ||
compile project(':hystrix-core') | ||
compile "junit:junit:4.11" | ||
} |
53 changes: 53 additions & 0 deletions
53
hystrix-contrib/hystrix-junit/src/main/java/com/hystrix/junit/HystrixRequestContextRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...-contrib/hystrix-junit/src/test/java/com/hystrix/junit/HystrixRequestContextRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters