Skip to content

Commit 8d90608

Browse files
Implement Event Engine improvements (#12)
* Ported over Event Engine functionality from C-Core to internal Chat logic. * Replaced old way of creating connections with a more convenient, non-async, and on-demand approach. * Connect(), Join(), Disconnect(), and Leave() now work on a "request" basis so they no longer have to be awaited.
1 parent 73abbba commit 8d90608

24 files changed

+787
-605
lines changed

.pubnub.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
---
2-
version: v0.3.1
2+
version: v0.4.0
33
changelog:
4+
- date: 2025-03-04
5+
version: v0.4.0
6+
changes:
7+
- type: feature
8+
text: "Ported over Event Engine functionality from C-Core to internal Chat logic."
49
- date: 2025-02-25
510
version: v0.3.1
611
changes:

c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChannelTests.cs

+31-18
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public async Task Setup()
1717
chat = await Chat.CreateInstance(new PubnubChatConfig(
1818
PubnubTestsParameters.PublishKey,
1919
PubnubTestsParameters.SubscribeKey,
20-
"ctuuuuuuuuuuu")
20+
"ctuuuuuuuuuuuuuuuuuuuuuuuuuuuuu")
2121
);
2222
if (!chat.TryGetCurrentUser(out user))
2323
{
@@ -41,7 +41,7 @@ public async Task CleanUp()
4141
public async Task TestGetUserSuggestions()
4242
{
4343
var channel = await chat.CreatePublicConversation("user_suggestions_test_channel");
44-
await channel.Join();
44+
channel.Join();
4545

4646
await Task.Delay(5000);
4747

@@ -53,7 +53,8 @@ public async Task TestGetUserSuggestions()
5353
public async Task TestGetMemberships()
5454
{
5555
var channel = await chat.CreatePublicConversation("get_members_test_channel");
56-
await channel.Join();
56+
channel.Join();
57+
await Task.Delay(3500);
5758
var memberships = await channel.GetMemberships();
5859
Assert.That(memberships.Memberships.Count, Is.GreaterThanOrEqualTo(1));
5960
}
@@ -62,26 +63,30 @@ public async Task TestGetMemberships()
6263
public async Task TestStartTyping()
6364
{
6465
var channel = (await chat.CreateDirectConversation(talkUser, "sttc")).CreatedChannel;
65-
await channel.Join();
66+
channel.Join();
67+
channel.SetListeningForTyping(true);
6668

67-
var typingManualEvent = new ManualResetEvent(false);
69+
await Task.Delay(5500);
6870

71+
var typingManualEvent = new ManualResetEvent(false);
6972
channel.OnUsersTyping += typingUsers =>
7073
{
7174
Assert.That(typingUsers, Does.Contain(user.Id));
7275
typingManualEvent.Set();
7376
};
7477
await channel.StartTyping();
7578

76-
var receivedTyping = typingManualEvent.WaitOne(5000);
79+
var receivedTyping = typingManualEvent.WaitOne(12000);
7780
Assert.IsTrue(receivedTyping);
7881
}
7982

8083
[Test]
8184
public async Task TestStopTyping()
8285
{
8386
var channel = (await chat.CreateDirectConversation(talkUser, "stop_typing_test_channel")).CreatedChannel;
84-
await channel.Join();
87+
channel.Join();
88+
channel.SetListeningForTyping(true);
89+
await Task.Delay(2500);
8590

8691
await channel.StartTyping();
8792

@@ -103,7 +108,10 @@ public async Task TestStopTyping()
103108
public async Task TestStopTypingFromTimer()
104109
{
105110
var channel = (await chat.CreateDirectConversation(talkUser, "stop_typing_timeout_test_channel")).CreatedChannel;
106-
await channel.Join();
111+
channel.Join();
112+
channel.SetListeningForTyping(true);
113+
114+
await Task.Delay(4500);
107115

108116
await channel.StartTyping();
109117

@@ -116,19 +124,24 @@ public async Task TestStopTypingFromTimer()
116124
typingManualEvent.Set();
117125
};
118126

119-
var stoppedTyping = typingManualEvent.WaitOne(10000);
127+
var stoppedTyping = typingManualEvent.WaitOne(12000);
120128
Assert.IsTrue(stoppedTyping);
121129
}
122130

123131
[Test]
124132
public async Task TestPinMessage()
125133
{
126134
var channel = await chat.CreatePublicConversation("pin_message_test_channel_37");
127-
await channel.Join();
135+
channel.Join();
136+
await Task.Delay(3500);
128137

129138
var receivedManualEvent = new ManualResetEvent(false);
130139
channel.OnMessageReceived += async message =>
131140
{
141+
message.SetListeningForUpdates(true);
142+
143+
await Task.Delay(4000);
144+
132145
await channel.PinMessage(message);
133146

134147
await Task.Delay(2000);
@@ -138,15 +151,16 @@ public async Task TestPinMessage()
138151
};
139152
await channel.SendText("message to pin");
140153

141-
var received = receivedManualEvent.WaitOne(9000);
154+
var received = receivedManualEvent.WaitOne(19000);
142155
Assert.IsTrue(received);
143156
}
144157

145158
[Test]
146159
public async Task TestUnPinMessage()
147160
{
148161
var channel = await chat.CreatePublicConversation("unpin_message_test_channel");
149-
await channel.Join();
162+
channel.Join();
163+
await Task.Delay(3500);
150164
var receivedManualEvent = new ManualResetEvent(false);
151165
channel.OnMessageReceived += async message =>
152166
{
@@ -187,18 +201,17 @@ public async Task TestCreateMessageDraft()
187201
public async Task TestEmitUserMention()
188202
{
189203
var channel = await chat.CreatePublicConversation("user_mention_test_channel");
190-
await channel.Join();
191-
await Task.Delay(2000);
204+
channel.Join();
192205
var receivedManualEvent = new ManualResetEvent(false);
193-
chat.StartListeningForMentionEvents(user.Id);
194-
await Task.Delay(2000);
195-
chat.OnMentionEvent += mentionEvent =>
206+
user.SetListeningForMentionEvents(true);
207+
await Task.Delay(3000);
208+
user.OnMentionEvent += mentionEvent =>
196209
{
197210
Assert.True(mentionEvent.Payload.Contains("heyyy"));
198211
receivedManualEvent.Set();
199212
};
200213
await channel.EmitUserMention(user.Id, "99999999999999999", "heyyy");
201-
var received = receivedManualEvent.WaitOne(10000);
214+
var received = receivedManualEvent.WaitOne(7000);
202215
Assert.True(received);
203216
}
204217
}

c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChatEventTests.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public async Task Setup()
2424
{
2525
Assert.Fail();
2626
}
27-
await channel.Join();
27+
channel.Join();
28+
await Task.Delay(3500);
2829
}
2930

3031
[TearDown]
@@ -40,12 +41,13 @@ public async Task CleanUp()
4041
public async Task TestModerationEvents()
4142
{
4243
var manualModerationEvent = new ManualResetEvent(false);
43-
chat.OnModerationEvent += moderationEvent =>
44+
user.OnModerationEvent += moderationEvent =>
4445
{
4546
Assert.True(moderationEvent.Payload.Contains("some_reason"));
4647
manualModerationEvent.Set();
4748
};
48-
chat.StartListeningForModerationEvents(user.Id);
49+
user.SetListeningForModerationEvents(true);
50+
await Task.Delay(2500);
4951
await user.SetRestriction(channel.Id, new Restriction()
5052
{
5153
Ban = true,

c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChatTests.cs

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Diagnostics;
2-
using Newtonsoft.Json;
32
using PubNubChatAPI.Entities;
43
using PubnubChatApi.Entities.Data;
54
using PubnubChatApi.Enums;
@@ -24,13 +23,14 @@ public async Task Setup()
2423
{
2524
Assert.Fail();
2625
}
27-
await channel.Join();
26+
channel.Join();
27+
await Task.Delay(3500);
2828
}
2929

3030
[TearDown]
3131
public async Task CleanUp()
3232
{
33-
await channel.Leave();
33+
channel.Leave();
3434
await Task.Delay(1000);
3535
chat.Destroy();
3636
await Task.Delay(1000);
@@ -83,7 +83,7 @@ public async Task TestGetUsers()
8383
[Test]
8484
public async Task TestGetChannels()
8585
{
86-
await Task.Delay(3000);
86+
await Task.Delay(4000);
8787
var channels = await chat.GetChannels();
8888
Assert.True(channels.Channels.Any());
8989
}
@@ -126,9 +126,11 @@ public async Task TestForwardMessage()
126126
Assert.True(message.MessageText == "message_to_forward");
127127
messageForwardReceivedManualEvent.Set();
128128
};
129-
await forwardingChannel.Join();
129+
forwardingChannel.Join();
130+
await Task.Delay(2500);
130131

131-
await channel.Join();
132+
/*channel.Join();
133+
await Task.Delay(3500);*/
132134
channel.OnMessageReceived += async message => { await chat.ForwardMessage(message, forwardingChannel); };
133135

134136
await channel.SendText("message_to_forward");
@@ -141,15 +143,16 @@ public async Task TestForwardMessage()
141143
public async Task TestEmitEvent()
142144
{
143145
var reportManualEvent = new ManualResetEvent(false);
144-
chat.OnReportEvent += reportEvent =>
146+
channel.OnCustomEvent += customEvent =>
145147
{
146-
Assert.True(reportEvent.Payload == "{\"test\":\"some_nonsense\", \"type\": \"report\"}");
148+
Assert.True(customEvent.Payload == "{\"test\":\"some_nonsense\", \"type\": \"custom\"}");
147149
reportManualEvent.Set();
148150
};
149-
await channel.Join();
150-
await chat.EmitEvent(PubnubChatEventType.Report, channel.Id, "{\"test\":\"some_nonsense\"}");
151+
channel.SetListeningForCustomEvents(true);
152+
await Task.Delay(2500);
153+
await chat.EmitEvent(PubnubChatEventType.Custom, channel.Id, "{\"test\":\"some_nonsense\"}");
151154

152-
var eventReceived = reportManualEvent.WaitOne(5000);
155+
var eventReceived = reportManualEvent.WaitOne(8000);
153156
Assert.True(eventReceived);
154157
}
155158

@@ -195,12 +198,18 @@ public async Task TestReadReceipts()
195198
return;
196199
}
197200

198-
await otherChatChannel.Join();
201+
otherChatChannel.Join();
202+
otherChatChannel.SetListeningForReadReceiptsEvents(true);
203+
await Task.Delay(2500);
199204

200205
var receiptReset = new ManualResetEvent(false);
201-
otherChat.OnReadReceiptEvent += receiptEvent =>
206+
otherChatChannel.OnReadReceiptEvent += readReceipts =>
202207
{
203-
Assert.True(receiptEvent.ChannelId == channel.Id && receiptEvent.UserId == currentUser.Id);
208+
if (readReceipts.Count == 0)
209+
{
210+
return;
211+
}
212+
Assert.True(readReceipts.Values.Any(x => x != null && x.Contains(currentUser.Id)));
204213
receiptReset.Set();
205214
};
206215
await otherChatChannel.SendText("READ MEEEE");

c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MembershipTests.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public async Task Setup()
2424
{
2525
Assert.Fail();
2626
}
27-
await channel.Join();
27+
channel.Join();
28+
await Task.Delay(3500);
2829
}
2930

3031
[TearDown]
@@ -59,7 +60,7 @@ public async Task TestUpdateMemberships()
5960
Assert.True(membership.Id == testMembership.Id);
6061
manualUpdatedEvent.Set();
6162
};
62-
await testMembership.StartListeningForUpdates();
63+
testMembership.SetListeningForUpdates(true);
6364

6465
await Task.Delay(4000);
6566

@@ -97,7 +98,7 @@ public async Task TestInviteMultiple()
9798
public async Task TestLastRead()
9899
{
99100
var testChannel = await chat.CreatePublicConversation("last_read_test_channel_57");
100-
await testChannel.Join();
101+
testChannel.Join();
101102

102103
await Task.Delay(4000);
103104

@@ -136,12 +137,13 @@ public async Task TestLastRead()
136137
public async Task TestUnreadMessagesCount()
137138
{
138139
var unreadChannel = await chat.CreatePublicConversation($"test_channel_{Guid.NewGuid()}");
139-
await unreadChannel.Join();
140+
unreadChannel.Join();
141+
await Task.Delay(3500);
140142
await unreadChannel.SendText("one");
141143
await unreadChannel.SendText("two");
142144
await unreadChannel.SendText("three");
143145

144-
await Task.Delay(6000);
146+
await Task.Delay(8000);
145147

146148
var membership = (await chat.GetUserMemberships(user.Id, limit: 20)).Memberships
147149
.FirstOrDefault(x => x.ChannelId == unreadChannel.Id);

c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MessageDraftTests.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public async Task Setup()
2525
Assert.Fail();
2626
}
2727

28-
await channel.Join();
28+
channel.Join();
29+
await Task.Delay(3000);
30+
2931
if (!chat.TryGetUser("mock_user", out dummyUser))
3032
{
3133
dummyUser = await chat.CreateUser("mock_user", new ChatUserData()

0 commit comments

Comments
 (0)