public void TestCacheWithoutRequestContext() { HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.GetInstance(); context.Dispose(); HystrixRequestCache cache1 = HystrixRequestCache.GetInstance(HystrixCommandKeyDefault.AsKey("command1")); Task <string> t1 = Task.FromResult("a1"); // this should fail, as there's no HystrixRequestContext instance to place the cache into Assert.Throws <InvalidOperationException>(() => { cache1.PutIfAbsent("valueA", t1); }); }
public void TestCache() { //HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.GetInstance(); //HystrixRequestContext context = HystrixRequestContext.nitializeContext(); try { Task <string> t1 = Task.FromResult("a1"); Task <string> t2 = Task.FromResult("a2"); Task <string> t3 = Task.FromResult("b1"); HystrixRequestCache cache1 = HystrixRequestCache.GetInstance(HystrixCommandKeyDefault.AsKey("command1")); cache1.PutIfAbsent("valueA", t1); cache1.PutIfAbsent("valueA", t2); cache1.PutIfAbsent("valueB", t3); HystrixRequestCache cache2 = HystrixRequestCache.GetInstance(HystrixCommandKeyDefault.AsKey("command2")); Task <string> t4 = Task.FromResult("a3"); cache2.PutIfAbsent("valueA", t4); Assert.Equal("a1", cache1.Get <Task <string> >("valueA").Result); Assert.Equal("b1", cache1.Get <Task <string> >("valueB").Result); Assert.Equal("a3", cache2.Get <Task <string> >("valueA").Result); Assert.Null(cache2.Get <Task <string> >("valueB")); } catch (Exception e) { Assert.False(true, "Exception: " + e.Message); output.WriteLine(e.ToString()); } finally { context.Dispose(); } context = HystrixRequestContext.InitializeContext(); try { // with a new context the instance should have nothing in it HystrixRequestCache cache = HystrixRequestCache.GetInstance(HystrixCommandKeyDefault.AsKey("command1")); Assert.Null(cache.Get <Task <string> >("valueA")); Assert.Null(cache.Get <Task <string> >("valueB")); } finally { context.Dispose(); } }
public void RequestCache_Cache() { IHystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.Instance; HystrixRequestContext context = HystrixRequestContext.InitializeContext(); try { HystrixRequestCache cache1 = HystrixRequestCache.GetInstance("command1", strategy); cache1.PutIfAbsent("valueA", new TestFuture("a1")); cache1.PutIfAbsent("valueA", new TestFuture("a2")); cache1.PutIfAbsent("valueB", new TestFuture("b1")); HystrixRequestCache cache2 = HystrixRequestCache.GetInstance("command2", strategy); cache2.PutIfAbsent("valueA", new TestFuture("a3")); Assert.AreEqual("a1", cache1.Get <string>("valueA").Get()); Assert.AreEqual("b1", cache1.Get <string>("valueB").Get()); Assert.AreEqual("a3", cache2.Get <string>("valueA").Get()); Assert.IsNull(cache2.Get <string>("valueB")); } catch (Exception e) { Assert.Fail("Exception: " + e.Message); Console.WriteLine(e.ToString()); } finally { context.Shutdown(); } context = HystrixRequestContext.InitializeContext(); try { // with a new context the instance should have nothing in it HystrixRequestCache cache = HystrixRequestCache.GetInstance("command1", strategy); Assert.IsNull(cache.Get <string>("valueA")); Assert.IsNull(cache.Get <string>("valueB")); } finally { context.Shutdown(); } }
public void TestClearCache() { HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.GetInstance(); try { HystrixRequestCache cache1 = HystrixRequestCache.GetInstance(HystrixCommandKeyDefault.AsKey("command1")); Task <string> t1 = Task.FromResult("a1"); cache1.PutIfAbsent("valueA", t1); Assert.Equal("a1", cache1.Get <Task <string> >("valueA").Result); cache1.Clear("valueA"); Assert.Null(cache1.Get <Task <string> >("valueA")); } catch (Exception e) { Assert.False(true, "Exception: " + e.Message); output.WriteLine(e.ToString()); } }
public void RequestCache_ClearCache() { IHystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.Instance; HystrixRequestContext context = HystrixRequestContext.InitializeContext(); try { HystrixRequestCache cache1 = HystrixRequestCache.GetInstance("command1", strategy); cache1.PutIfAbsent("valueA", new TestFuture("a1")); Assert.AreEqual("a1", cache1.Get <string>("valueA").Get()); cache1.Clear("valueA"); Assert.IsNull(cache1.Get <string>("valueA")); } catch (Exception e) { Assert.Fail("Exception: " + e.Message); Console.WriteLine(e.ToString()); } finally { context.Shutdown(); } }
public void TestCacheWithoutContext() { this.context.Dispose(); Assert.Throws <InvalidOperationException>(() => { HystrixRequestCache.GetInstance(HystrixCommandKeyDefault.AsKey("command1")).Get <Task <string> >("any"); }); }