public void TestSingleInitializePerKey() { TestHystrixMetricsPublisher publisher = new TestHystrixMetricsPublisher(); HystrixPlugins.RegisterMetricsPublisher(publisher); HystrixMetricsPublisherFactory factory = new HystrixMetricsPublisherFactory(); List <Task> threads = new List <Task>(); for (int i = 0; i < 20; i++) { threads.Add(new Task(() => { factory.GetPublisherForCommand(TestCommandKey.TEST_A, null, null, null, null); factory.GetPublisherForCommand(TestCommandKey.TEST_B, null, null, null, null); factory.GetPublisherForThreadPool(TestThreadPoolKey.TEST_A, null, null); }, CancellationToken.None, TaskCreationOptions.LongRunning)); } // start them foreach (Task t in threads) { t.Start(); } // wait for them to finish Task.WaitAll(threads.ToArray()); Assert.Equal(2, factory.commandPublishers.Count); Assert.Equal(1, factory.threadPoolPublishers.Count); // we should see 2 commands and 1 threadPool publisher created Assert.Equal(2, publisher.commandCounter.Value); Assert.Equal(1, publisher.threadCounter.Value); }
public void TestMetricsPublisherReset() { // precondition: HystrixMetricsPublisherFactory class is not loaded. Calling HystrixPlugins.reset() here should be good enough to run this with other tests. // set first custom publisher IHystrixCommandKey key = HystrixCommandKeyDefault.AsKey("key"); IHystrixMetricsPublisherCommand firstCommand = new HystrixMetricsPublisherCommandDefault(key, null, null, null, null); HystrixMetricsPublisher firstPublisher = new CustomPublisher(firstCommand); HystrixPlugins.RegisterMetricsPublisher(firstPublisher); // ensure that first custom publisher is used IHystrixMetricsPublisherCommand cmd = HystrixMetricsPublisherFactory.CreateOrRetrievePublisherForCommand(key, null, null, null, null); Assert.True(firstCommand == cmd); // reset, then change to second custom publisher HystrixPlugins.Reset(); IHystrixMetricsPublisherCommand secondCommand = new HystrixMetricsPublisherCommandDefault(key, null, null, null, null); HystrixMetricsPublisher secondPublisher = new CustomPublisher(secondCommand); HystrixPlugins.RegisterMetricsPublisher(secondPublisher); // ensure that second custom publisher is used cmd = HystrixMetricsPublisherFactory.CreateOrRetrievePublisherForCommand(key, null, null, null, null); Assert.True(firstCommand != cmd); Assert.True(secondCommand == cmd); }
public void EnsureThreadPoolInstanceIsTheOneRegisteredWithMetricsPublisherAndThreadPoolCache() { HystrixPlugins.RegisterMetricsPublisher(new MyHystrixMetricsPublisher()); IHystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("threadPoolFactoryConcurrencyTest"); IHystrixThreadPool poolOne = new HystrixThreadPoolDefault( threadPoolKey, HystrixThreadPoolOptionsTest.GetUnitTestPropertiesBuilder()); IHystrixThreadPool poolTwo = new HystrixThreadPoolDefault( threadPoolKey, HystrixThreadPoolOptionsTest.GetUnitTestPropertiesBuilder()); Assert.Equal(poolOne.GetScheduler(), poolTwo.GetScheduler()); // Now that we get the threadPool from the metrics object, this will always be equal HystrixMetricsPublisherThreadPoolContainer hystrixMetricsPublisherThreadPool = (HystrixMetricsPublisherThreadPoolContainer)HystrixMetricsPublisherFactory .CreateOrRetrievePublisherForThreadPool(threadPoolKey, null, null); IHystrixTaskScheduler threadPoolExecutor = hystrixMetricsPublisherThreadPool.HystrixThreadPoolMetrics.TaskScheduler; // assert that both HystrixThreadPools share the same ThreadPoolExecutor as the one in HystrixMetricsPublisherThreadPool Assert.True(threadPoolExecutor.Equals(poolOne.GetScheduler()) && threadPoolExecutor.Equals(poolTwo.GetScheduler())); Assert.False(threadPoolExecutor.IsShutdown); // Now the HystrixThreadPool ALWAYS has the same reference to the ThreadPoolExecutor so that it no longer matters which // wins to be inserted into the HystrixThreadPool.Factory.threadPools cache. poolOne.Dispose(); poolTwo.Dispose(); }