Skip to content

Commit e002319

Browse files
author
Jon Elverkilde
authored
Merge pull request #110 from pusher/bugs/channel-event-emitter-#109
Fixed issue #109: Channel event emitter was not filtering on event name
2 parents e2c6306 + 3a74ac5 commit e002319

15 files changed

+179
-68
lines changed

AppConfig.sample.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"AppKey": "",
44
"AppSecret": "",
55
"Cluster": "mt1",
6-
"Encrypted": true
6+
"Encrypted": true,
7+
"EnableAuthorizationLatency": true
78
}

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 2.0.1
4+
* [FIXED] Filter on event name in event emitter.
5+
36
## 2.0.0
47
* [FIXED] Infinite loop when failing to connect for the first time.
58
* [FIXED] Bug: GenericPresenceChannel<T>'s AddMember and RemoveMember events were not being emitted.

PusherClient.Tests.Utilities/ApplicationConfig.cs

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
/// </summary>
66
public class ApplicationConfig : IApplicationConfig
77
{
8+
/// <summary>
9+
/// Instantiates an instance of an <see cref="ApplicationConfig"/> class.
10+
/// </summary>
11+
public ApplicationConfig()
12+
{
13+
this.EnableAuthorizationLatency = true;
14+
}
15+
816
/// <summary>
917
/// Gets or sets the Pusher application id.
1018
/// </summary>
@@ -29,5 +37,10 @@ public class ApplicationConfig : IApplicationConfig
2937
/// Gets or sets whether the connection will be encrypted.
3038
/// </summary>
3139
public bool Encrypted { get; set; }
40+
41+
/// <summary>
42+
/// Gets or sets whether an artificial latency is induced when authorizing a channel.
43+
/// </summary>
44+
public bool? EnableAuthorizationLatency { get; set; }
3245
}
3346
}

PusherClient.Tests.Utilities/ChannelNameFactory.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static string CreateUniqueChannelName(ChannelTypes channelType = ChannelT
2020
break;
2121
}
2222

23-
var mockChannelName = $"{channelPrefix}myTestChannel{Guid.NewGuid():N}";
23+
var mockChannelName = $"{channelPrefix}-test-{Guid.NewGuid():N}";
2424
return mockChannelName;
2525
}
2626
}

PusherClient.Tests.Utilities/Config.cs

+30-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
namespace PusherClient.Tests.Utilities
2-
{
1+
namespace PusherClient.Tests.Utilities
2+
{
33
/// <summary>
44
/// Contains the default test configuration.
5-
/// </summary>
6-
public static class Config
7-
{
8-
static Config()
9-
{
5+
/// </summary>
6+
public static class Config
7+
{
8+
static Config()
9+
{
1010
IApplicationConfig config = EnvironmentVariableConfigLoader.Default.Load();
1111
if (string.IsNullOrWhiteSpace(config.AppKey))
1212
{
@@ -17,9 +17,10 @@ static Config()
1717
AppKey = config.AppKey;
1818
AppSecret = config.AppSecret;
1919
Cluster = config.Cluster;
20-
Encrypted = config.Encrypted;
21-
}
22-
20+
Encrypted = config.Encrypted;
21+
EnableAuthorizationLatency = config.EnableAuthorizationLatency ?? true;
22+
}
23+
2324
/// <summary>
2425
/// Gets or sets the Pusher application id.
2526
/// </summary>
@@ -43,6 +44,22 @@ static Config()
4344
/// <summary>
4445
/// Gets or sets whether the connection will be encrypted.
4546
/// </summary>
46-
public static bool Encrypted { get; private set; }
47-
}
48-
}
47+
public static bool Encrypted { get; private set; }
48+
49+
/// <summary>
50+
/// Gets or sets whether an artificial latency is induced when authorizing a channel.
51+
/// </summary>
52+
public static bool EnableAuthorizationLatency { get; set; }
53+
54+
/// <summary>
55+
/// Gets the PusherServer HTTP host.
56+
/// </summary>
57+
public static string HttpHost
58+
{
59+
get
60+
{
61+
return $"api-{Cluster}.pusher.com";
62+
}
63+
}
64+
}
65+
}

PusherClient.Tests.Utilities/IApplicationConfig.cs

+5
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,10 @@ public interface IApplicationConfig
2929
/// Gets or sets whether the connection will be encrypted.
3030
/// </summary>
3131
bool Encrypted { get; set; }
32+
33+
/// <summary>
34+
/// Gets or sets whether an artificial latency is induced when authorizing a channel.
35+
/// </summary>
36+
bool? EnableAuthorizationLatency { get; set; }
3237
}
3338
}

PusherClient.Tests.Utilities/LatencyInducer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class LatencyInducer : ILatencyInducer
1111
/// <summary>
1212
/// Gets or sets whether this latency inducer is enabled.
1313
/// </summary>
14-
public bool Enabled { get; set; } = true;
14+
public bool Enabled { get; set; } = Config.EnableAuthorizationLatency;
1515

1616
/// <summary>
1717
/// If enabled, pauses for a random period between <paramref name="minLatency"/> and <paramref name="maxLatency"/>.

PusherClient.Tests/AcceptanceTests/EventEmitterTest.PusherEventEmitter.cs

+94
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,100 @@ public async Task PusherEventEmitterPrivateChannelUnbindGeneralListenerTestAsync
8282
await PusherEventEmitterUnbindTestAsync(channelType, listenersToUnbind: new List<int> { 0 }).ConfigureAwait(false);
8383
}
8484

85+
[Test]
86+
public async Task PusherEventEmitterPrivateChannelMultipleEventHandlersTestAsync()
87+
{
88+
// Arrange
89+
var pusherServer = new PusherServer.Pusher(Config.AppId, Config.AppKey, Config.AppSecret, new PusherServer.PusherOptions()
90+
{
91+
HostName = Config.HttpHost,
92+
});
93+
94+
Pusher localPusher = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients);
95+
Dictionary<string, AutoResetEvent> channelEventReceived = new Dictionary<string, AutoResetEvent>
96+
{
97+
{"my-event-1", new AutoResetEvent(false)},
98+
{"my-event-2", new AutoResetEvent(false)},
99+
{"my-event-3", new AutoResetEvent(false)},
100+
};
101+
102+
Dictionary<string, PusherEvent> channelEvent = new Dictionary<string, PusherEvent>
103+
{
104+
{"my-event-1", null},
105+
{"my-event-2", null},
106+
{"my-event-3", null},
107+
};
108+
109+
Dictionary<string, int> channelEventReceivedCount = new Dictionary<string, int>
110+
{
111+
{"my-event-1", 0},
112+
{"my-event-2", 0},
113+
{"my-event-3", 0},
114+
};
115+
116+
await localPusher.ConnectAsync().ConfigureAwait(false);
117+
Channel localChannel = await localPusher.SubscribeAsync("private-multiple-event-channel").ConfigureAwait(false);
118+
119+
void Listener1(PusherEvent eventData)
120+
{
121+
string key = "my-event-1";
122+
channelEventReceivedCount[key]++;
123+
if (eventData.EventName == key)
124+
{
125+
channelEvent[key] = eventData;
126+
channelEventReceived[key].Set();
127+
}
128+
}
129+
130+
void Listener2(PusherEvent eventData)
131+
{
132+
string key = "my-event-2";
133+
channelEventReceivedCount[key]++;
134+
if (eventData.EventName == key)
135+
{
136+
channelEvent[key] = eventData;
137+
channelEventReceived[key].Set();
138+
}
139+
}
140+
141+
void Listener3(PusherEvent eventData)
142+
{
143+
string key = "my-event-3";
144+
channelEventReceivedCount[key]++;
145+
if (eventData.EventName == key)
146+
{
147+
channelEvent[key] = eventData;
148+
channelEventReceived[key].Set();
149+
}
150+
}
151+
152+
List<EventTestData> data = new List<EventTestData>()
153+
{
154+
new EventTestData { TextField = "1", IntegerField = 1, },
155+
new EventTestData { TextField = "2", IntegerField = 2, },
156+
new EventTestData { TextField = "3", IntegerField = 3, },
157+
};
158+
159+
// Act
160+
localChannel.Bind("my-event-1", Listener1);
161+
localChannel.Bind("my-event-2", Listener2);
162+
localChannel.Bind("my-event-3", Listener3);
163+
await pusherServer.TriggerAsync(localChannel.Name, "my-event-1", data[0]).ConfigureAwait(false);
164+
await pusherServer.TriggerAsync(localChannel.Name, "my-event-2", data[1]).ConfigureAwait(false);
165+
await pusherServer.TriggerAsync(localChannel.Name, "my-event-3", data[2]).ConfigureAwait(false);
166+
167+
// Assert
168+
foreach (var eventReceived in channelEventReceived)
169+
{
170+
Assert.IsTrue(eventReceived.Value.WaitOne(TimeSpan.FromSeconds(5)), $"Event not received for {eventReceived.Key}");
171+
}
172+
173+
foreach (var eventReceivedCount in channelEventReceivedCount)
174+
{
175+
Assert.AreEqual(1, eventReceivedCount.Value, $"#Events received for {eventReceivedCount.Key}");
176+
}
177+
}
178+
85179
#endregion
86180

87181
#region Test helper functions

PusherClient.Tests/PusherClient.Tests.csproj

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<Import Project="..\packages\NUnit3TestAdapter.3.16.1\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\packages\NUnit3TestAdapter.3.16.1\build\net35\NUnit3TestAdapter.props')" />
4-
<Import Project="..\packages\NUnit.3.12.0\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.12.0\build\NUnit.props')" />
3+
<Import Project="..\packages\NUnit3TestAdapter.3.17.0\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\packages\NUnit3TestAdapter.3.17.0\build\net35\NUnit3TestAdapter.props')" />
4+
<Import Project="..\packages\NUnit.3.13.1\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.13.1\build\NUnit.props')" />
55
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
66
<PropertyGroup>
77
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -56,8 +56,11 @@
5656
<Reference Include="NSubstitute, Version=4.2.0.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
5757
<HintPath>..\packages\NSubstitute.4.2.1\lib\net46\NSubstitute.dll</HintPath>
5858
</Reference>
59-
<Reference Include="nunit.framework, Version=3.12.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
60-
<HintPath>..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll</HintPath>
59+
<Reference Include="nunit.framework, Version=3.13.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
60+
<HintPath>..\packages\NUnit.3.13.1\lib\net45\nunit.framework.dll</HintPath>
61+
</Reference>
62+
<Reference Include="PusherServer, Version=4.4.0.0, Culture=neutral, processorArchitecture=MSIL">
63+
<HintPath>..\packages\PusherServer.4.4.0\lib\net45\PusherServer.dll</HintPath>
6164
</Reference>
6265
<Reference Include="SuperSocket.ClientEngine, Version=0.10.0.0, Culture=neutral, PublicKeyToken=ee9af13f57f00acc, processorArchitecture=MSIL">
6366
<HintPath>..\packages\SuperSocket.ClientEngine.Core.0.10.0\lib\net45\SuperSocket.ClientEngine.dll</HintPath>
@@ -66,9 +69,6 @@
6669
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
6770
<HintPath>..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
6871
</Reference>
69-
<Reference Include="System.Collections.Immutable, Version=1.2.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
70-
<HintPath>..\packages\System.Collections.Immutable.1.7.0\lib\netstandard2.0\System.Collections.Immutable.dll</HintPath>
71-
</Reference>
7272
<Reference Include="System.Collections.Specialized, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
7373
<HintPath>..\packages\System.Collections.Specialized.4.3.0\lib\net46\System.Collections.Specialized.dll</HintPath>
7474
<Private>True</Private>
@@ -87,9 +87,6 @@
8787
<Private>True</Private>
8888
<Private>True</Private>
8989
</Reference>
90-
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
91-
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
92-
</Reference>
9390
<Reference Include="System.Net" />
9491
<Reference Include="System.Net.NameResolution, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
9592
<HintPath>..\packages\System.Net.NameResolution.4.3.0\lib\net46\System.Net.NameResolution.dll</HintPath>
@@ -107,9 +104,6 @@
107104
<Private>True</Private>
108105
</Reference>
109106
<Reference Include="System.Numerics" />
110-
<Reference Include="System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
111-
<HintPath>..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
112-
</Reference>
113107
<Reference Include="System.Runtime, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
114108
<HintPath>..\packages\System.Runtime.4.3.1\lib\net462\System.Runtime.dll</HintPath>
115109
<Private>True</Private>
@@ -205,8 +199,8 @@
205199
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
206200
</PropertyGroup>
207201
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets'))" />
208-
<Error Condition="!Exists('..\packages\NUnit.3.12.0\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.12.0\build\NUnit.props'))" />
209-
<Error Condition="!Exists('..\packages\NUnit3TestAdapter.3.16.1\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.3.16.1\build\net35\NUnit3TestAdapter.props'))" />
202+
<Error Condition="!Exists('..\packages\NUnit.3.13.1\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.13.1\build\NUnit.props'))" />
203+
<Error Condition="!Exists('..\packages\NUnit3TestAdapter.3.17.0\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.3.17.0\build\net35\NUnit3TestAdapter.props'))" />
210204
</Target>
211205
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
212206
Other similar extension points exist, see Microsoft.Common.targets.

PusherClient.Tests/packages.config

+3-13
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,17 @@
77
<package id="Mock4Net.Core" version="1.0.0-v20150608-4f463ad" targetFramework="net471" />
88
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net471" />
99
<package id="NSubstitute" version="4.2.1" targetFramework="net471" />
10-
<package id="NUnit" version="3.12.0" targetFramework="net471" />
11-
<package id="NUnit.Console" version="3.10.0" targetFramework="net471" />
12-
<package id="NUnit.ConsoleRunner" version="3.10.0" targetFramework="net471" />
13-
<package id="NUnit.Extension.NUnitProjectLoader" version="3.6.0" targetFramework="net45" />
14-
<package id="NUnit.Extension.NUnitV2Driver" version="3.8.0" targetFramework="net471" />
15-
<package id="NUnit.Extension.NUnitV2ResultWriter" version="3.6.0" targetFramework="net45" />
16-
<package id="NUnit.Extension.TeamCityEventListener" version="1.0.7" targetFramework="net471" />
17-
<package id="NUnit.Extension.VSProjectLoader" version="3.8.0" targetFramework="net471" />
18-
<package id="NUnit.Runners" version="3.10.0" targetFramework="net471" />
19-
<package id="NUnit3TestAdapter" version="3.16.1" targetFramework="net471" developmentDependency="true" />
10+
<package id="NUnit" version="3.13.1" targetFramework="net471" />
11+
<package id="NUnit3TestAdapter" version="3.17.0" targetFramework="net471" developmentDependency="true" />
12+
<package id="PusherServer" version="4.4.0" targetFramework="net471" />
2013
<package id="SuperSocket.ClientEngine.Core" version="0.10.0" targetFramework="net471" />
2114
<package id="System.Buffers" version="4.4.0" targetFramework="net471" />
22-
<package id="System.Collections.Immutable" version="1.7.0" targetFramework="net471" />
2315
<package id="System.Collections.Specialized" version="4.3.0" targetFramework="net471" />
2416
<package id="System.IO" version="4.3.0" targetFramework="net471" />
2517
<package id="System.Linq" version="4.3.0" targetFramework="net471" />
26-
<package id="System.Memory" version="4.5.3" targetFramework="net471" />
2718
<package id="System.Net.NameResolution" version="4.3.0" targetFramework="net471" />
2819
<package id="System.Net.Security" version="4.3.2" targetFramework="net471" />
2920
<package id="System.Net.Sockets" version="4.3.0" targetFramework="net471" />
30-
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net471" />
3121
<package id="System.Runtime" version="4.3.1" targetFramework="net471" />
3222
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net471" />
3323
<package id="System.Runtime.Extensions" version="4.3.1" targetFramework="net471" />

PusherClient/ChannelTypes.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
namespace PusherClient
22
{
33
/// <summary>
4-
/// An enum represnets the different types of <see cref="Channel"/>
4+
/// An enum represnets the different types of <see cref="Channel"/>.
55
/// </summary>
66
public enum ChannelTypes
77
{
88
/// <summary>
9-
/// A Public Channel
9+
/// A public channel.
1010
/// </summary>
1111
Public,
12+
1213
/// <summary>
13-
/// A Private Channel
14+
/// A private channel.
1415
/// </summary>
1516
Private,
17+
1618
/// <summary>
17-
/// Presence Channel
19+
/// A presence channel.
1820
/// </summary>
19-
Presence
21+
Presence,
2022
}
2123
}

PusherClient/EventEmitterGeneric.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ public void EmitEvent(string eventName, TData data)
164164

165165
if (_listeners.Count > 0)
166166
{
167-
foreach (var list in _listeners.Values)
167+
if (_listeners.ContainsKey(eventName))
168168
{
169-
listeners.AddRange(list);
169+
listeners.AddRange(_listeners[eventName]);
170170
}
171171
}
172172
}

0 commit comments

Comments
 (0)