Back to Repositories

Testing HystrixContextScheduler Thread Management in Netflix Hystrix

This test suite evaluates the HystrixContextScheduler’s handling of thread interruption and unsubscription in Netflix’s Hystrix library. It verifies the proper cleanup of resources and thread management when operations are cancelled or unsubscribed.

Test Coverage Overview

The test suite focuses on verifying the unsubscription behavior of wrapped schedulers in Hystrix’s concurrency management.

Key areas covered include:
  • Thread interruption handling
  • Scheduler worker lifecycle management
  • Proper cleanup on unsubscription
  • Timing constraints for operation completion

Implementation Analysis

The testing approach utilizes JUnit’s timeout mechanism and concurrent utilities to validate scheduler behavior.

Key patterns include:
  • CountDownLatch for synchronization control
  • AtomicBoolean for thread-safe state tracking
  • RxJava Scheduler integration testing
  • Controlled thread sleep and interruption scenarios

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • RxJava Schedulers
  • Java concurrent utilities (CountDownLatch, AtomicBoolean)
  • Hystrix concurrency management components
  • Thread manipulation utilities

Best Practices Demonstrated

The test demonstrates several quality testing practices:

  • Proper resource cleanup in try-finally blocks
  • Explicit timeout constraints
  • Thread-safe state verification
  • Controlled concurrency testing
  • Clear separation of setup, execution, and verification phases

netflix/hystrix

hystrix-core/src/test/java/com/netflix/hystrix/strategy/concurrency/HystrixContextSchedulerTest.java

            
/**
 * Copyright 2015 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.strategy.concurrency;

import static org.junit.Assert.assertTrue;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.Test;

import rx.Scheduler;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

public class HystrixContextSchedulerTest {
    
    @Test(timeout = 2500)
    public void testUnsubscribeWrappedScheduler() throws InterruptedException {
        Scheduler s = Schedulers.newThread();
        final AtomicBoolean interrupted = new AtomicBoolean();
        final CountDownLatch start = new CountDownLatch(1);
        final CountDownLatch end = new CountDownLatch(1);

        HystrixContextScheduler hcs = new HystrixContextScheduler(s);

        Scheduler.Worker w = hcs.createWorker();
        try {
            w.schedule(new Action0() {
                @Override
                public void call() {
                    start.countDown();
                    try {
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException ex) {
                            interrupted.set(true);
                        }
                    } finally {
                        end.countDown();
                    }
                }
            });
            
            start.await();
            
            w.unsubscribe();
            
            end.await();
            
            assertTrue(interrupted.get());
        } finally {
            w.unsubscribe();
        }
    }
}