public async Task TriggerNotSubscribedErrorTestAsync() { // Arrange ChannelTypes channelType = ChannelTypes.Private; Pusher localPusher = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients); string testEventName = "client-pusher-event-test"; PusherEvent pusherEvent = CreatePusherEvent(channelType, testEventName); await localPusher.ConnectAsync().ConfigureAwait(false); Channel localChannel = await localPusher.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false); TriggerEventException exception = null; // Act await localPusher.UnsubscribeAllAsync().ConfigureAwait(false); try { await localChannel.TriggerAsync(testEventName, pusherEvent.Data); } catch (TriggerEventException error) { exception = error; } // Assert Assert.IsNotNull(exception, $"Expected a {nameof(TriggerEventException)}"); Assert.AreEqual(ErrorCodes.TriggerEventNotSubscribedError, exception.PusherCode); }
public ApiPusherEvent(PusherEvent db) { Timestamp = db.Timestamp; Channel = db.Channel; Event = db.Event; Data = db.Data; }
public async Task PusherEventEmitterPrivateEncryptedChannelTestAsync() { // Arrange byte[] encryptionKey = GenerateEncryptionMasterKey(); var pusherServer = new PusherServer.Pusher(Config.AppId, Config.AppKey, Config.AppSecret, new PusherServer.PusherOptions { Cluster = Config.Cluster, EncryptionMasterKey = encryptionKey, }); ChannelTypes channelType = ChannelTypes.PrivateEncrypted; Pusher localPusher = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients, encryptionKey: encryptionKey); string testEventName = "private-encrypted-event-test"; AutoResetEvent globalEventReceived = new AutoResetEvent(false); AutoResetEvent channelEventReceived = new AutoResetEvent(false); PusherEvent globalEvent = null; PusherEvent channelEvent = null; PusherEvent pusherEvent = CreatePusherEvent(channelType, testEventName); await localPusher.ConnectAsync().ConfigureAwait(false); Channel localChannel = await localPusher.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false); void GeneralListener(string eventName, PusherEvent eventData) { if (eventName == testEventName) { globalEvent = eventData; globalEventReceived.Set(); } } void Listener(PusherEvent eventData) { channelEvent = eventData; channelEventReceived.Set(); } EventTestData data = new EventTestData { TextField = ExpectedTextField, IntegerField = ExpectedIntegerField, }; // Act localPusher.BindAll(GeneralListener); localChannel.Bind(testEventName, Listener); await pusherServer.TriggerAsync(pusherEvent.ChannelName, testEventName, data).ConfigureAwait(false); // Assert Assert.IsTrue(globalEventReceived.WaitOne(TimeSpan.FromSeconds(5))); Assert.IsTrue(channelEventReceived.WaitOne(TimeSpan.FromSeconds(5))); AssertPusherEventsAreEqual(channelType, pusherEvent, globalEvent); AssertPusherEventsAreEqual(channelType, pusherEvent, channelEvent); }
private static PusherEvent CreatePusherEvent(ChannelTypes channelType, string eventName) { EventTestData data = new EventTestData { TextField = ExpectedTextField, IntegerField = ExpectedIntegerField, }; Dictionary <string, object> properties = new Dictionary <string, object> { { "channel", ChannelNameFactory.CreateUniqueChannelName(channelType: channelType) }, { "event", eventName }, { "data", data }, }; PusherEvent result = new PusherEvent(properties, JsonConvert.SerializeObject(properties)); return(result); }
public void EventEmitterShouldEmitAnEventToARegisteredPusherEventListener() { // Arrange PusherEvent emittedEvent = null; var myAction = new Action <PusherEvent>(o => emittedEvent = o); var emitter = new PusherClient.EventEmitter(); emitter.Bind("pusher event listener event", myAction); // Act emitter.EmitEvent("pusher event listener event", CreateTestEvent()); // Assert Assert.IsNotNull(emittedEvent); StringAssert.AreEqualIgnoringCase("channel event", emittedEvent.EventName); StringAssert.AreEqualIgnoringCase("{\"stuff\":1234}", emittedEvent.Data); }
public void EventEmitterShouldNotEmitAnEventToAnUnregisteredPusherEventEventName() { // Arrange PusherEvent emittedEvent = null; PusherEvent emittedEvent2 = null; var myAction = new Action <PusherEvent>(o => emittedEvent = o); var myAction2 = new Action <PusherEvent>(o => emittedEvent2 = o); var emitter = new PusherClient.EventEmitter(); emitter.Bind("pusher event listener event", myAction); emitter.Bind("pusher event listener event", myAction2); emitter.Unbind("pusher event listener event"); // Act emitter.EmitEvent("pusher event listener event", CreateTestEvent()); // Assert Assert.IsNull(emittedEvent); Assert.IsNull(emittedEvent2); }
private void GlobalHandler(string _, PusherEvent evt) { // Get timestamp earliest possible (outside async) var timestamp = _clock.GetCurrentInstant(); async Task Inner() { await Task.Yield(); try { var data = TgbUtils.TryDecodePusherData(evt.Data); await _pusherEventStore.SaveEvent(evt.ChannelName, evt.EventName, timestamp, data, evt.Data); } catch (Exception e) { _logger.Error(e, "Error handling Pusher event {ChannelName}/{EventName}", evt.ChannelName, evt.EventName); } } // Fork var __ = Inner(); }
public async Task TriggerNullEventNameErrorTestAsync() { // Arrange ChannelTypes channelType = ChannelTypes.Public; Pusher localPusher = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients); string testEventName = null; PusherEvent pusherEvent = CreatePusherEvent(channelType, testEventName); Channel localChannel = await localPusher.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false); ArgumentNullException exception = null; // Act try { await localChannel.TriggerAsync(testEventName, pusherEvent.Data); } catch (ArgumentNullException error) { exception = error; } // Assert Assert.IsNotNull(exception, $"Expected a {nameof(ArgumentNullException)}"); Assert.IsTrue(exception.Message.Contains("eventName")); }
private static void AssertPusherEventsAreEqual(ChannelTypes channelType, PusherEvent expected, PusherEvent actual) { Assert.IsNotNull(expected, nameof(expected)); Assert.IsNotNull(actual, nameof(actual)); if (channelType == ChannelTypes.Presence) { Assert.IsNull(expected.UserId); Assert.IsNotNull(actual.UserId); } else { Assert.IsNull(expected.UserId); Assert.IsNull(actual.UserId); } Assert.IsNotNull(actual.ChannelName); Assert.AreEqual(expected.ChannelName, actual.ChannelName); Assert.IsNotNull(actual.EventName); Assert.AreEqual(expected.EventName, actual.EventName); AssertPusherEventData(expected.Data, actual.Data); }
public ApiPusherEventRaw(PusherEvent db) : base(db) { Raw = db.Raw; }
private async Task PusherEventEmitterUnbindTestAsync(ChannelTypes channelType, IList <int> listenersToUnbind) { // Arrange Pusher localPusher = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients); string testEventName = "client-pusher-event-test"; PusherEvent pusherEvent = CreatePusherEvent(channelType, testEventName); string[] testEventNames = new string[] { testEventName, testEventName, testEventName, testEventName, }; AutoResetEvent[] receivedEvents = new AutoResetEvent[testEventNames.Length]; int[] numberEventsReceived = new int[testEventNames.Length]; int[] totalEventsExpected = new int[testEventNames.Length]; bool[] eventExpected = new bool[testEventNames.Length]; for (int i = 0; i < testEventNames.Length; i++) { receivedEvents[i] = new AutoResetEvent(false); numberEventsReceived[i] = 0; eventExpected[i] = true; if (listenersToUnbind.Contains(i)) { totalEventsExpected[i] = 1; } else { totalEventsExpected[i] = 2; } } await localPusher.ConnectAsync().ConfigureAwait(false); Channel remoteChannel = await _remoteClient.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false); Channel localChannel = await localPusher.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false); void Listener(int index, string eventName) { if (eventName == testEventNames[index]) { numberEventsReceived[index]++; if (eventExpected[index]) { receivedEvents[index].Set(); } } } void GeneralListener0(string eventName, PusherEvent eventData) { Listener(0, eventName); } void GeneralListener1(string eventName, PusherEvent eventData) { Listener(1, eventName); } void Listener2(PusherEvent eventData) { Listener(2, eventData.EventName); } void Listener3(PusherEvent eventData) { Listener(3, eventData.EventName); } localPusher.BindAll(GeneralListener0); localPusher.BindAll(GeneralListener1); localChannel.Bind(testEventName, Listener2); localChannel.Bind(testEventName, Listener3); await remoteChannel.TriggerAsync(testEventName, pusherEvent.Data).ConfigureAwait(false); for (int i = 0; i < testEventNames.Length; i++) { Assert.IsTrue(receivedEvents[i].WaitOne(TimeSpan.FromSeconds(5)), $"receivedEvents[{i}]"); receivedEvents[i].Reset(); } TimeSpan delayAfterTrigger = TimeSpan.FromMilliseconds(0); foreach (int index in listenersToUnbind) { eventExpected[index] = false; } // Act if (listenersToUnbind.Count == testEventNames.Length) { // Not expecting any events, so wait a bit and ensure that none come in. delayAfterTrigger = TimeSpan.FromMilliseconds(500); localPusher.UnbindAll(); localChannel.UnbindAll(); } else { if (listenersToUnbind.Contains(0)) { localPusher.Unbind(GeneralListener0); } if (listenersToUnbind.Contains(1)) { localPusher.Unbind(GeneralListener1); } if (listenersToUnbind.Contains(2) && listenersToUnbind.Contains(3)) { localChannel.Unbind(testEventName); } else { if (listenersToUnbind.Contains(2)) { localChannel.Unbind(testEventName, Listener2); } if (listenersToUnbind.Contains(3)) { localChannel.Unbind(testEventName, Listener3); } } } await remoteChannel.TriggerAsync(testEventName, pusherEvent.Data).ConfigureAwait(false); await Task.Delay(delayAfterTrigger).ConfigureAwait(false); // Assert for (int i = 0; i < testEventNames.Length; i++) { if (eventExpected[i]) { Assert.IsTrue(receivedEvents[i].WaitOne(TimeSpan.FromSeconds(5)), $"receivedEvents[{i}]"); } Assert.AreEqual(totalEventsExpected[i], numberEventsReceived[i], $"# Event[{i}]"); } }
private async Task PusherEventEmitterTestAsync(ChannelTypes channelType, bool raiseEmitterActionError = false) { // Arrange Pusher localPusher = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients); string testEventName = "client-pusher-event-test"; AutoResetEvent globalEventReceived = new AutoResetEvent(false); AutoResetEvent channelEventReceived = new AutoResetEvent(false); AutoResetEvent globalActionErrorReceived = raiseEmitterActionError ? new AutoResetEvent(false) : null; AutoResetEvent channelActionErrorReceived = raiseEmitterActionError ? new AutoResetEvent(false) : null; string globalActionErrorMsg = "Simulate error in BindAll action."; string channelActionErrorMsg = "Simulate error in Bind action."; PusherException globalActionError = null; EventEmitterActionException <PusherEvent> channelActionError = null; PusherEvent globalEvent = null; PusherEvent channelEvent = null; PusherEvent pusherEvent = CreatePusherEvent(channelType, testEventName); if (raiseEmitterActionError) { void HandleError(object sender, PusherException error) { if (error.Message == globalActionErrorMsg) { globalActionError = error; globalActionErrorReceived?.Set(); } else if (error.Message.Contains(channelActionErrorMsg)) { channelActionError = error as EventEmitterActionException <PusherEvent>; channelActionErrorReceived?.Set(); } if (raiseEmitterActionError) { throw new InvalidOperationException("Simulated error from error handler."); } } localPusher.Error += HandleError; } await localPusher.ConnectAsync().ConfigureAwait(false); Channel remoteChannel = await _remoteClient.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false); Channel localChannel = await localPusher.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false); void GeneralListener(string eventName, PusherEvent eventData) { if (eventName == testEventName) { globalEvent = eventData; globalEventReceived.Set(); if (raiseEmitterActionError) { throw new PusherException(globalActionErrorMsg, ErrorCodes.Unknown); } } } void Listener(PusherEvent eventData) { channelEvent = eventData; channelEventReceived.Set(); if (raiseEmitterActionError) { throw new InvalidOperationException(channelActionErrorMsg); } } // Act localPusher.BindAll(GeneralListener); localChannel.Bind(testEventName, Listener); remoteChannel.Trigger(testEventName, pusherEvent.Data); // Assert Assert.IsTrue(globalEventReceived.WaitOne(TimeSpan.FromSeconds(5))); Assert.IsTrue(channelEventReceived.WaitOne(TimeSpan.FromSeconds(5))); if (raiseEmitterActionError) { Assert.IsTrue(globalActionErrorReceived.WaitOne(TimeSpan.FromSeconds(5))); Assert.IsTrue(channelActionErrorReceived.WaitOne(TimeSpan.FromSeconds(5))); Assert.IsNotNull(globalActionError); Assert.IsNotNull(channelActionError); Assert.AreEqual(ErrorCodes.EventEmitterActionError, channelActionError.PusherCode); Assert.IsNotNull(channelActionError.EventData); Assert.AreEqual(testEventName, channelActionError.EventName); } AssertPusherEventsAreEqual(channelType, pusherEvent, globalEvent); AssertPusherEventsAreEqual(channelType, pusherEvent, channelEvent); }
public async Task PusherEventEmitterPrivateEncryptedChannelDecryptionFailureTestAsync() { // Arrange var pusherServer = new PusherServer.Pusher(Config.AppId, Config.AppKey, Config.AppSecret, new PusherServer.PusherOptions { Cluster = Config.Cluster, EncryptionMasterKey = GenerateEncryptionMasterKey(), }); ChannelDecryptionException decryptionException = null; ChannelTypes channelType = ChannelTypes.PrivateEncrypted; // Use a different encryption master key - decryption should fail Pusher localPusher = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients, encryptionKey: GenerateEncryptionMasterKey()); string testEventName = "private-encrypted-event-test"; AutoResetEvent globalEventReceived = new AutoResetEvent(false); AutoResetEvent channelEventReceived = new AutoResetEvent(false); AutoResetEvent errorReceived = new AutoResetEvent(false); PusherEvent pusherEvent = CreatePusherEvent(channelType, testEventName); await localPusher.ConnectAsync().ConfigureAwait(false); Channel localChannel = await localPusher.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false); void GeneralListener(string eventName, PusherEvent eventData) { if (eventName == testEventName) { globalEventReceived.Set(); } } void Listener(PusherEvent eventData) { channelEventReceived.Set(); } EventTestData data = new EventTestData { TextField = ExpectedTextField, IntegerField = ExpectedIntegerField, }; void ErrorHandler(object sender, PusherException error) { decryptionException = error as ChannelDecryptionException; if (decryptionException != null) { errorReceived.Set(); } } // Act localPusher.Error += ErrorHandler; localPusher.BindAll(GeneralListener); localChannel.Bind(testEventName, Listener); await pusherServer.TriggerAsync(pusherEvent.ChannelName, testEventName, data).ConfigureAwait(false); // Assert Assert.IsTrue(errorReceived.WaitOne(TimeSpan.FromSeconds(5)), "Expected to handle an error."); Assert.IsNotNull(decryptionException.SocketID, nameof(decryptionException.SocketID)); Assert.AreEqual(testEventName, decryptionException.EventName, nameof(decryptionException.EventName)); Assert.AreEqual(localChannel.Name, decryptionException.ChannelName, nameof(decryptionException.ChannelName)); Assert.IsFalse(globalEventReceived.WaitOne(TimeSpan.FromSeconds(0.1)), "Did not expect to get a global event raised."); Assert.IsFalse(channelEventReceived.WaitOne(TimeSpan.FromSeconds(0.1)), "Did not expect to get a channel event raised."); }