Back to Repositories

Testing Hystrix Metrics Poller Lifecycle Management in Netflix/Hystrix

This test suite validates the functionality of the Hystrix Metrics Poller, focusing on the start, stop, and restart capabilities of the metrics collection system. The tests ensure proper metrics gathering and event streaming functionality in Netflix’s Hystrix circuit breaker library.

Test Coverage Overview

The test suite provides comprehensive coverage of the HystrixMetricsPoller’s core functionality.

Key areas tested include:
  • Metrics collection initialization and counting
  • Poller start/stop/restart lifecycle management
  • Metrics accumulation verification
  • Thread sleep timing and state transitions
Edge cases covered include pause state validation and metric count consistency checks.

Implementation Analysis

The testing approach utilizes JUnit’s framework for systematic validation of the metrics polling mechanism. The implementation employs atomic counters and command execution patterns specific to Hystrix.

Notable patterns include:
  • AtomicInteger for thread-safe metrics counting
  • HystrixCommand implementation for command execution
  • Listener pattern for metrics collection
  • Time-based state verification

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • HystrixMetricsPoller with custom listener
  • Thread management for timing control
  • JSON metric handling capabilities
  • Atomic operations for thread safety

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java and Hystrix contexts.

Notable practices include:
  • Proper resource cleanup with shutdown()
  • Clear test method naming
  • Effective use of assertion statements
  • Proper exception handling
  • Structured test setup and teardown

netflix/hystrix

hystrix-contrib/hystrix-metrics-event-stream/src/test/java/com/netflix/hystrix/contrib/metrics/eventstream/HystrixMetricsPollerTest.java

            
/**
 * Copyright 2012 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.metrics.eventstream;

import static org.junit.Assert.*;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

/**
 * Polls Hystrix metrics and output JSON strings for each metric to a MetricsPollerListener.
 * <p>
 * Polling can be stopped/started. Use shutdown() to permanently shutdown the poller.
 */
public class HystrixMetricsPollerTest {

    @Test
    public void testStartStopStart() {
        final AtomicInteger metricsCount = new AtomicInteger();

        HystrixMetricsPoller poller = new HystrixMetricsPoller(new HystrixMetricsPoller.MetricsAsJsonPollerListener() {

            @Override
            public void handleJsonMetric(String json) {
                System.out.println("Received: " + json);
                metricsCount.incrementAndGet();
            }
        }, 100);
        try {

            HystrixCommand<Boolean> test = new HystrixCommand<Boolean>(HystrixCommandGroupKey.Factory.asKey("HystrixMetricsPollerTest")) {

                @Override
                protected Boolean run() {
                    return true;
                }

            };
            test.execute();

            poller.start();

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            int v1 = metricsCount.get();

            assertTrue(v1 > 0);

            poller.pause();

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            int v2 = metricsCount.get();

            // they should be the same since we were paused
            System.out.println("First poll got : " + v1 + ", second got : " + v2);
            assertTrue(v2 == v1);

            poller.start();

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            int v3 = metricsCount.get();

            // we should have more metrics again
            assertTrue(v3 > v1);

        } finally {
            poller.shutdown();
        }
    }
}