private void UT_SubscribeToEvents(Context context, IList<KeyEvent> expectedEvents, KeyEventSubscriptionType? eventSubscriptionType = null, string key = null, KeyEvent? eventType = null) { ConcurrentQueue<KeyValuePair<string, KeyEvent>> result = new ConcurrentQueue<KeyValuePair<string, KeyEvent>>(); CountdownEvent handle = new CountdownEvent(expectedEvents.Count); Action<string, KeyEvent> action = (k, e) => { result.Enqueue(new KeyValuePair<string, KeyEvent>(k, e)); handle.Signal(); }; Action unsubscribeAction = () => { }; if (key == null && !eventType.HasValue && eventSubscriptionType.HasValue) { context.KeyEvents.Subscribe(eventSubscriptionType.Value, action); unsubscribeAction = () => context.KeyEvents.Unsubscribe(eventSubscriptionType.Value); } else if (key != null) { context.KeyEvents.Subscribe(key, action); unsubscribeAction = () => context.KeyEvents.Unsubscribe(key); } else if (eventType.HasValue) { context.KeyEvents.Subscribe(eventType.Value, action); unsubscribeAction = () => context.KeyEvents.Unsubscribe(eventType.Value); } var objectKey = key ?? Guid.NewGuid().ToString(); context.Cache.SetObject(objectKey, new { Name = "alex", Created = DateTime.UtcNow }); context.Cache.Remove(objectKey); Assert.IsTrue(handle.Wait(5000)); Assert.AreEqual(expectedEvents.Count, result.Count); foreach (var expectedEvent in expectedEvents) { KeyValuePair<string, KeyEvent> e; Assert.IsTrue(result.TryDequeue(out e)); Assert.AreEqual(expectedEvent, e.Value); Assert.AreEqual(objectKey, e.Key); } //Now test Unsubscribe. No more events should be received in queue and handle will timeout. handle.Reset(1); unsubscribeAction(); context.Cache.SetObject(objectKey, new { Name = "alex", Created = DateTime.UtcNow }, TimeSpan.FromMilliseconds(500)); Assert.IsFalse(handle.Wait(1000)); Assert.IsTrue(result.IsEmpty); context.KeyEvents.Unsubscribe(KeyEventSubscriptionType.All); }
private static string GetEventChannelName(KeyEventSubscriptionType subscriptionType, string key = null, KeyEvent? eventType = null) { switch (subscriptionType) { case KeyEventSubscriptionType.All: return "__key*__:*"; case KeyEventSubscriptionType.KeyEvent: return string.Format("__keyevent@*__:{0}", eventType.HasValue ? TextAttributeCache<KeyEvent>.Instance.GetEnumText(eventType.Value) : "*"); case KeyEventSubscriptionType.KeySpace: return string.Format("__keyspace@*__:{0}", key ?? "*"); default: throw new NotImplementedException(string.Format("Subscription not implemented for type: {0}", subscriptionType)); } }
private static string GetEventChannelName(KeyEventSubscriptionType subscriptionType, string key = null, KeyEvent?eventType = null) { switch (subscriptionType) { case KeyEventSubscriptionType.All: return("__key*__:*"); case KeyEventSubscriptionType.KeyEvent: return(string.Format("__keyevent@*__:{0}", eventType.HasValue ? TextAttributeCache <KeyEvent> .Instance.GetEnumText(eventType.Value) : "*")); case KeyEventSubscriptionType.KeySpace: return(string.Format("__keyspace@*__:{0}", key ?? "*")); default: throw new NotImplementedException(string.Format("Subscription not implemented for type: {0}", subscriptionType)); } }
public void Unsubscribe(KeyEventSubscriptionType subscriptionType) { UnsubscribeFromRedis(GetEventChannelName(subscriptionType)); }
public void Subscribe(KeyEventSubscriptionType subscriptionType, Action <string, KeyEvent> action) { SubscribeToRedis(GetEventChannelName(subscriptionType), action); }
public void Subscribe(KeyEventSubscriptionType subscriptionType, Action<string, KeyEvent> action) { SubscribeToRedis(GetEventChannelName(subscriptionType), action); }