private static HystrixCommandKey initCommandKey(final HystrixCommandKey fromConstructor, Class<?> clazz) { if (fromConstructor == null || fromConstructor.name().trim().equals("")) { final String keyName = getDefaultNameFromClass(clazz); return HystrixCommandKey.Factory.asKey(keyName); } else { return fromConstructor; } }
@Override TestHystrixObservableCommand<Integer> getCommand(ExecutionIsolationStrategy isolationStrategy, AbstractTestHystrixCommand.ExecutionResult executionResult, int executionLatency, AbstractTestHystrixCommand.FallbackResult fallbackResult, int fallbackLatency, TestCircuitBreaker circuitBreaker, HystrixThreadPool threadPool, int timeout, AbstractTestHystrixCommand.CacheEnabled cacheEnabled, Object value, AbstractCommand.TryableSemaphore executionSemaphore, AbstractCommand.TryableSemaphore fallbackSemaphore, boolean circuitBreakerDisabled) { HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey("FlexibleObservable-" + uniqueNameCounter.getAndIncrement()); return FlexibleTestHystrixObservableCommand.from(commandKey, isolationStrategy, executionResult, executionLatency, fallbackResult, fallbackLatency, circuitBreaker, threadPool, timeout, cacheEnabled, value, executionSemaphore, fallbackSemaphore, circuitBreakerDisabled); }
@Override TestHystrixCommand<Integer> getCommand(ExecutionIsolationStrategy isolationStrategy, AbstractTestHystrixCommand.ExecutionResult executionResult, int executionLatency, AbstractTestHystrixCommand.FallbackResult fallbackResult, int fallbackLatency, TestCircuitBreaker circuitBreaker, HystrixThreadPool threadPool, int timeout, AbstractTestHystrixCommand.CacheEnabled cacheEnabled, Object value, TryableSemaphore executionSemaphore, TryableSemaphore fallbackSemaphore, boolean circuitBreakerDisabled) { HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey("Flexible-" + uniqueNameCounter.getAndIncrement()); return FlexibleTestHystrixCommand.from(commandKey, isolationStrategy, executionResult, executionLatency, fallbackResult, fallbackLatency, circuitBreaker, threadPool, timeout, cacheEnabled, value, executionSemaphore, fallbackSemaphore, circuitBreakerDisabled); }
@Test public void testEmptyStreamProducesZeros() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-CumulativeCounter-A"); stream = CumulativeCommandEventCounterStream.getInstance(key, 10, 500); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(5).subscribe(getSubscriber(latch)); //no writes try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); assertFalse(hasData(stream.getLatest())); }
@Test public void testStartsAndEndsInSameBucketProduceValue() throws InterruptedException { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-B"); stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 500); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(5).subscribe(getSubscriber(latch)); Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 100); Command cmd2 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 100); cmd1.observe(); Thread.sleep(1); cmd2.observe(); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); assertEquals(2, stream.getLatestRollingMax()); }
@Test public void testEmptyStreamProducesZeros() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-A"); stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 500); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(5).subscribe(getSubscriber(latch)); //no writes try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(0, stream.getLatestRollingMax()); }
@Test public void testCacheWithoutRequestContext() { HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance(); //HystrixRequestContext context = HystrixRequestContext.initializeContext(); try { HystrixRequestCache cache1 = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy); //this should fail, as there's no HystrixRequestContext instance to place the cache into cache1.putIfAbsent("valueA", new TestObservable("a1")); fail("should throw an exception on cache put"); } catch (Exception e) { //expected e.printStackTrace(); } }
public HystrixCommandMetrics getMetrics(String commandKey) { return HystrixCommandMetrics.getInstance(HystrixCommandKey.Factory.asKey(commandKey)); }
private LatchedSemaphoreCommand(String commandName, TestCircuitBreaker circuitBreaker, TryableSemaphore semaphore, CountDownLatch startLatch, CountDownLatch waitLatch) { super(testPropsBuilder() .setCommandKey(HystrixCommandKey.Factory.asKey(commandName)) .setCircuitBreaker(circuitBreaker) .setMetrics(circuitBreaker.metrics) .setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter() .withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE) .withCircuitBreakerEnabled(false)) .setExecutionSemaphore(semaphore)); this.startLatch = startLatch; this.waitLatch = waitLatch; }
private LatchedSemaphoreCommand(String commandName, TestCircuitBreaker circuitBreaker, TryableSemaphoreActual semaphore, CountDownLatch startLatch, CountDownLatch waitLatch) { super(testPropsBuilder() .setCommandKey(HystrixCommandKey.Factory.asKey(commandName)) .setCircuitBreaker(circuitBreaker) .setMetrics(circuitBreaker.metrics) .setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter() .withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE) .withCircuitBreakerEnabled(false)) .setExecutionSemaphore(semaphore)); this.startLatch = startLatch; this.waitLatch = waitLatch; }
public HystrixThreadEventStreamTest() { commandKey = HystrixCommandKey.Factory.asKey("CMD-ThreadStream"); threadPoolKey = HystrixThreadPoolKey.Factory.asKey("TP-ThreadStream"); writeToStream = HystrixThreadEventStream.getInstance(); readCommandStream = HystrixCommandCompletionStream.getInstance(commandKey); readThreadPoolStream = HystrixThreadPoolCompletionStream.getInstance(threadPoolKey); }
@Test(expected = IllegalStateException.class) public void testCacheWithoutContext() { HystrixRequestCache.getInstance( HystrixCommandKey.Factory.asKey("command1"), HystrixConcurrencyStrategyDefault.getInstance() ).get("any"); }
protected SynchronousObservableCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GROUP")) .andCommandKey(HystrixCommandKey.Factory.asKey("SyncObservable")) .andCommandPropertiesDefaults(new HystrixCommandProperties.Setter().withExecutionIsolationSemaphoreMaxConcurrentRequests(1000)) ); }
public SlowCacheableCommand(TestCircuitBreaker circuitBreaker, String value, int duration) { super(testPropsBuilder() .setCommandKey(HystrixCommandKey.Factory.asKey("ObservableSlowCacheable")) .setCircuitBreaker(circuitBreaker).setMetrics(circuitBreaker.metrics)); this.value = value; this.duration = duration; }
protected TestCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("testTimeoutConcurrency")) .andCommandKey(HystrixCommandKey.Factory.asKey("testTimeoutConcurrencyCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(3) .withCircuitBreakerEnabled(false) .withFallbackIsolationSemaphoreMaxConcurrentRequests(NUM_CONCURRENT_COMMANDS)) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withCoreSize(NUM_CONCURRENT_COMMANDS) .withMaxQueueSize(NUM_CONCURRENT_COMMANDS) .withQueueSizeRejectionThreshold(NUM_CONCURRENT_COMMANDS))); }
public Command(String commandKey, boolean shouldFail, boolean shouldFailWithBadRequest, long latencyToAdd, int sleepWindow, int requestVolumeThreshold) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Command")).andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)). andCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter(). withExecutionTimeoutInMilliseconds(500). withCircuitBreakerRequestVolumeThreshold(requestVolumeThreshold). withCircuitBreakerSleepWindowInMilliseconds(sleepWindow))); this.shouldFail = shouldFail; this.shouldFailWithBadRequest = shouldFailWithBadRequest; this.latencyToAdd = latencyToAdd; }
public TestCommand(String commandName, String value, boolean fail, boolean failOnFallback) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RequestLogTestCommand")).andCommandKey(HystrixCommandKey.Factory.asKey(commandName))); this.value = value; this.fail = fail; this.failOnFallback = failOnFallback; this.timeout = false; this.useFallback = true; this.useCache = true; }
public HystrixObservableCommand.Setter buildObservableCommandSetter() { HystrixObservableCommand.Setter setter = HystrixObservableCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); try { setter.andCommandPropertiesDefaults(HystrixPropertiesManager.initializeCommandProperties(commandProperties)); } catch (IllegalArgumentException e) { throw new HystrixPropertyException("Failed to set Command properties. " + getInfo(), e); } return setter; }
public Command(String commandKey, boolean shouldFail, boolean shouldFailWithBadRequest, long latencyToAdd) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Command")) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)) .andCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter() .withExecutionTimeoutInMilliseconds(1000) .withCircuitBreakerRequestVolumeThreshold(20))); this.shouldFail = shouldFail; this.shouldFailWithBadRequest = shouldFailWithBadRequest; this.latencyToAdd = latencyToAdd; }
protected AsynchronousObservableCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GROUP")) .andCommandKey(HystrixCommandKey.Factory.asKey("AsyncObservable")) .andCommandPropertiesDefaults(new HystrixCommandProperties.Setter().withExecutionIsolationSemaphoreMaxConcurrentRequests(1000)) ); }