Back to Repositories

Testing Hystrix Request Context Management in Netflix/Hystrix

This abstract test class provides foundational testing infrastructure for Hystrix command execution in a Java environment. It establishes core functionality for managing Hystrix request contexts and accessing thread pool properties during testing.

Test Coverage Overview

The test suite provides comprehensive coverage of Hystrix’s request context management and thread pool configuration.

Key functionality includes:
  • Request context lifecycle management
  • Thread pool property access and validation
  • Context reset capabilities
Integration points focus on Hystrix’s core components including HystrixRequestContext and HystrixThreadPool.

Implementation Analysis

The testing approach utilizes JUnit’s Rule annotation to manage test lifecycle and context setup.

Technical patterns include:
  • Reflection-based access to internal Hystrix components
  • Abstract class design for reusable test infrastructure
  • Exception handling with Google Guava’s Throwables

Technical Details

Testing tools and configuration:
  • JUnit test framework with @Rule annotation
  • HystrixRequestContextRule for context management
  • Java Reflection API for accessing protected Hystrix properties
  • Custom exception propagation through Guava’s utilities

Best Practices Demonstrated

The test implementation showcases several testing best practices.

Notable practices include:
  • Proper test isolation through context management
  • Reusable test infrastructure through abstract class design
  • Controlled access to internal components for thorough testing
  • Clean separation of concerns in test utilities

netflix/hystrix

hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/common/BasicHystrixTest.java

            
/**
 * 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.netflix.hystrix.contrib.javanica.test.common;

import com.google.common.base.Throwables;
import com.hystrix.junit.HystrixRequestContextRule;
import com.netflix.hystrix.HystrixInvokableInfo;
import com.netflix.hystrix.HystrixThreadPool;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.junit.Rule;

import java.lang.reflect.Field;

/**
 * Created by dmgcodevil
 */
public abstract class BasicHystrixTest {
    @Rule
    public HystrixRequestContextRule request = new HystrixRequestContextRule();

    protected final HystrixRequestContext getHystrixContext() {
        return request.context();
    }

    protected void resetContext() {
        request.reset();
    }

    protected final HystrixThreadPoolProperties getThreadPoolProperties(HystrixInvokableInfo<?> command) {
        try {
            Field field = command.getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("threadPool");
            field.setAccessible(true);
            HystrixThreadPool threadPool = (HystrixThreadPool) field.get(command);

            Field field2 = HystrixThreadPool.HystrixThreadPoolDefault.class.getDeclaredField("properties");
            field2.setAccessible(true);
            return (HystrixThreadPoolProperties) field2.get(threadPool);

        } catch (NoSuchFieldException e) {
            throw Throwables.propagate(e);
        } catch (IllegalAccessException e) {
            throw Throwables.propagate(e);
        }
    }
}