Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more tests using HystrixRequestContext rule #1193

Merged
merged 1 commit into from
May 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hystrix-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
compile 'org.slf4j:slf4j-api:1.7.0'
compile 'org.hdrhistogram:HdrHistogram:2.1.7'
testCompile 'junit:junit-dep:4.10'
testCompile project(':hystrix-junit')
}


Expand Down Expand Up @@ -44,4 +45,4 @@ jmh {
warmup = '1s'
warmupBatchSize = 1
warmupIterations = 5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.netflix.hystrix;

import com.hystrix.junit.HystrixRequestContextRule;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
Expand All @@ -28,9 +29,8 @@
import java.util.concurrent.atomic.AtomicInteger;

import com.netflix.hystrix.strategy.properties.HystrixPropertiesCollapserDefault;
import com.netflix.hystrix.util.HystrixRollingNumberEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.netflix.hystrix.HystrixCollapser.CollapsedRequest;
Expand All @@ -48,21 +48,13 @@
import static org.junit.Assert.*;

public class HystrixCollapserTest {
private HystrixRequestContext context = null;
@Rule
public HystrixRequestContextRule context = new HystrixRequestContextRule();

@Before
public void init() {
// since we're going to modify properties of the same class between tests, wipe the cache each time
HystrixCollapser.reset();
HystrixCollapserMetrics.reset();
HystrixCommandMetrics.reset();
/* we must call this to simulate a new request lifecycle running and clearing caches */
context = HystrixRequestContext.initializeContext();
}

@After
public void cleanup() {
context.shutdown();
}

@Test
Expand Down Expand Up @@ -827,7 +819,7 @@ public void testRequestWithCommandShortCircuited() throws Exception {

/**
* Test a Void response type - null being set as response.
*
*
* @throws Exception
*/
@Test
Expand All @@ -851,7 +843,7 @@ public void testVoidResponseTypeFireAndForgetCollapsing1() throws Exception {

/**
* Test a Void response type - response never being set in mapResponseToRequest
*
*
* @throws Exception
*/
@Test
Expand Down Expand Up @@ -879,7 +871,7 @@ public void testVoidResponseTypeFireAndForgetCollapsing2() throws Exception {

/**
* Test a Void response type with execute - response being set in mapResponseToRequest to null
*
*
* @throws Exception
*/
@Test
Expand Down Expand Up @@ -1198,7 +1190,7 @@ public void clear() {
* You must call incrementTime multiple times each increment being larger than 'period' on subsequent calls to cause multiple executions.
* <p>
* This is because executing multiple times in a tight-loop would not achieve the correct behavior, such as batching, since it will all execute "now" not after intervals of time.
*
*
* @param timeInMilliseconds amount of time to increment
*/
public synchronized void incrementTime(int timeInMilliseconds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.netflix.hystrix;

import com.hystrix.junit.HystrixRequestContextRule;
import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.AbstractCommand.TryableSemaphore;
import com.netflix.hystrix.AbstractCommand.TryableSemaphoreActual;
Expand All @@ -29,7 +30,7 @@
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
import com.netflix.hystrix.strategy.properties.HystrixProperty;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import rx.Observable;
import rx.Observer;
Expand Down Expand Up @@ -57,21 +58,11 @@
import static org.junit.Assert.*;

public class HystrixCommandTest extends CommonHystrixCommandTests<TestHystrixCommand<Integer>> {

@Before
public void prepareForTest() {
/* we must call this to simulate a new request lifecycle running and clearing caches */
HystrixRequestContext.initializeContext();
}
@Rule
public HystrixRequestContextRule ctx = new HystrixRequestContextRule();

@After
public void cleanup() {
// instead of storing the reference from initialize we'll just get the current state and shutdown
if (HystrixRequestContext.getContextForCurrentThread() != null) {
// it could have been set NULL by the test
HystrixRequestContext.getContextForCurrentThread().shutdown();
}

// force properties to be clean as well
ConfigurationManager.getConfigInstance().clear();

Expand Down Expand Up @@ -2374,7 +2365,7 @@ public void testBadRequestExceptionViaQueueInThread() {
} catch (ExecutionException e) {
e.printStackTrace();
if (e.getCause() instanceof HystrixBadRequestException) {
// success
// success
} else {
fail("We expect a " + HystrixBadRequestException.class.getSimpleName() + " but got a " + e.getClass().getSimpleName());
}
Expand Down Expand Up @@ -2413,7 +2404,7 @@ public void testBadRequestExceptionViaQueueInThreadOnResponseFromCache() {
} catch (ExecutionException e) {
e.printStackTrace();
if (e.getCause() instanceof HystrixBadRequestException) {
// success
// success
} else {
fail("We expect a " + HystrixBadRequestException.class.getSimpleName() + " but got a " + e.getClass().getSimpleName());
}
Expand Down Expand Up @@ -2477,7 +2468,7 @@ public void testCheckedExceptionViaExecute() {

/**
* Test a java.lang.Error being thrown
*
*
* @throws InterruptedException
*/
@Test
Expand Down Expand Up @@ -3809,7 +3800,7 @@ private static class LatchedSemaphoreCommand extends TestHystrixCommand<Boolean>
private final CountDownLatch startLatch, waitLatch;

/**
*
*
* @param circuitBreaker circuit breaker (passed in so it may be shared)
* @param semaphore semaphore (passed in so it may be shared)
* @param startLatch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright 2014 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.
Expand All @@ -15,6 +15,7 @@
*/
package com.netflix.hystrix;

import com.hystrix.junit.HystrixRequestContextRule;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
Expand All @@ -35,8 +36,8 @@
import com.netflix.hystrix.collapser.RealCollapserTimer;
import com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesCollapserDefault;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import rx.Observable;
Expand Down Expand Up @@ -122,24 +123,14 @@ public String call(String s) {
}
};

@Rule
public HystrixRequestContextRule ctx = new HystrixRequestContextRule();
private static ExecutorService threadPool = new ThreadPoolExecutor(100, 100, 10, TimeUnit.MINUTES, new SynchronousQueue<Runnable>());

@Before
public void init() {
// since we're going to modify properties of the same class between tests, wipe the cache each time
HystrixCollapser.reset();
Hystrix.reset();
/* we must call this to simulate a new request lifecycle running and clearing caches */
HystrixRequestContext.initializeContext();
}

@After
public void cleanup() {
// instead of storing the reference from initialize we'll just get the current state and shutdown
if (HystrixRequestContext.getContextForCurrentThread() != null) {
// it may be null if a test shuts the context down manually
HystrixRequestContext.getContextForCurrentThread().shutdown();
}
}

@Test
Expand All @@ -165,7 +156,7 @@ public void stressTestRequestCollapser() throws Exception {
for(int i = 0; i < 10; i++) {
init();
testTwoRequests();
cleanup();
ctx.reset();
}
}

Expand Down Expand Up @@ -805,7 +796,7 @@ public void call(Subscriber<? super String> s) {
}
s.onNext(request.getArgument());
}

s.onCompleted();
}

Expand Down
Loading