Skip to content

Commit 0d452c9

Browse files
author
Collin Dauphinee
committed
Adding checks for mixing SubscribeAsync and SubscribePresenceAsync
1 parent 1e2a11c commit 0d452c9

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

PusherClient.Tests/AcceptanceTests/PresenceChannel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void PresenceChannelShouldAddAMemberWhenGivenAMemberAsync()
9494
}
9595

9696
[Test]
97-
public void PresenceChannelShouldUseGenericWhenGivenAMemberAsync()
97+
public void PresenceChannelShouldAddATypedMemberWhenGivenAMemberAsync()
9898
{
9999
// Arrange
100100
var stubOptions = new PusherOptions

PusherClient.Tests/UnitTests/PusherTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,29 @@ public void PusherShouldThrowAnExceptionWhenSubscribePresenceIsCalledWithADiffer
194194
Assert.IsNotNull(caughtException);
195195
StringAssert.Contains("Cannot change channel member type; was previously defined as", caughtException.Message);
196196
}
197+
198+
[Test]
199+
public void PusherShouldThrowAnExceptionWhenSubscribePresenceIsCalledAfterSubscribe()
200+
{
201+
// Arrange
202+
InvalidOperationException caughtException = null;
203+
204+
// Act
205+
var pusher = new Pusher("FakeAppKey", new PusherOptions { Authorizer = new FakeAuthoriser("test") });
206+
AsyncContext.Run(() => pusher.SubscribeAsync("presence-123"));
207+
208+
try
209+
{
210+
AsyncContext.Run(() => pusher.SubscribePresenceAsync<int>("presence-123"));
211+
}
212+
catch (InvalidOperationException ex)
213+
{
214+
caughtException = ex;
215+
}
216+
217+
// Assert
218+
Assert.IsNotNull(caughtException);
219+
StringAssert.Contains("This presence channel has already been created without specifying the member info type", caughtException.Message);
220+
}
197221
}
198222
}

PusherClient/Pusher.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ public async Task<GenericPresenceChannel<MemberT>> SubscribePresenceAsync<Member
269269
if (channelType != ChannelTypes.Presence)
270270
throw new ArgumentException("The channel name must be refer to a presence channel", nameof(channelName));
271271

272+
// We need to keep track of the type we want the channel to be, in case it gets created or re-created later.
272273
_presenceChannelFactories.AddOrUpdate(channelName,
273274
(_) => Tuple.Create<Type, Func<Channel>>(typeof(MemberT),
274275
() => new GenericPresenceChannel<MemberT>(channelName, this)),
@@ -279,8 +280,18 @@ public async Task<GenericPresenceChannel<MemberT>> SubscribePresenceAsync<Member
279280
return existing;
280281
});
281282

282-
var result = await SubscribeAsync(channelName);
283-
return result as GenericPresenceChannel<MemberT>;
283+
var channel = await SubscribeAsync(channelName);
284+
285+
var result = channel as GenericPresenceChannel<MemberT>;
286+
if (result == null)
287+
{
288+
if (channel is PresenceChannel)
289+
throw new InvalidOperationException("This presence channel has already been created without specifying the member info type");
290+
else
291+
throw new InvalidOperationException($"The presence channel found is an unexpected type: {channel.GetType().Name}");
292+
}
293+
294+
return result;
284295
}
285296

286297
private async Task<Channel> SubscribeToChannel(string channelName)

0 commit comments

Comments
 (0)