Skip to content

Commit 1726358

Browse files
authored
Update all tests clients from the background task instead of relying on test logic to update them (#174)
1 parent 713ca35 commit 1726358

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

Assets/Plugins/StreamChat/Tests/LowLevelClient/Integration/DeviceApiIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class DeviceApiIntegrationTests : BaseIntegrationTests
1919
public IEnumerator Test_complete_device_cycle_add_list_remove()
2020
{
2121
yield return LowLevelClient.WaitForClientToConnect();
22-
yield return Test_complete_device_cycle_add_list_remove_Async().RunTaskAsEnumerator(LowLevelClient);
22+
yield return Test_complete_device_cycle_add_list_remove_Async().RunTaskAsEnumerator();
2323
}
2424

2525
private async Task Test_complete_device_cycle_add_list_remove_Async()

Assets/Plugins/StreamChat/Tests/StatefulClient/BaseStateIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected void SkipThisTempChannelDeletionInTearDown(IStreamChannel channel)
8787

8888
protected static IEnumerator ConnectAndExecute(Func<Task> test)
8989
{
90-
yield return ConnectAndExecuteAsync(test).RunAsIEnumerator(statefulClient: Client);
90+
yield return ConnectAndExecuteAsync(test).RunAsIEnumerator();
9191
}
9292

9393
protected Task<StreamChatClient> GetConnectedOtherClientAsync()

Assets/Plugins/StreamChat/Tests/StreamTestClients.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.Linq;
7+
using System.Threading;
78
using System.Threading.Tasks;
89
using NUnit.Framework.Interfaces;
910
using StreamChat.Core;
1011
using StreamChat.Core.Configs;
1112
using StreamChat.Core.LowLevelClient;
1213
using StreamChat.Core.LowLevelClient.Models;
1314
using StreamChat.Libs.Auth;
15+
using StreamChat.Libs.ChatInstanceRunner;
1416
using Debug = UnityEngine.Debug;
1517
using Random = UnityEngine.Random;
1618

@@ -113,6 +115,9 @@ public Task<StreamChatClient> ConnectOtherStateClientAsync()
113115
private StreamChatClient _otherStateClient;
114116

115117
private bool _runFinished;
118+
119+
private Task _updateTask;
120+
private CancellationTokenSource _updateTaskCts;
116121

117122
private StreamTestClients()
118123
{
@@ -131,6 +136,32 @@ private StreamTestClients()
131136
AdminSecondaryCredentials = GetCredentialsFromSet(testAuthSets.Admins, optionalTestDataIndex + offset);
132137
UserPrimaryCredentials = GetCredentialsFromSet(testAuthSets.Admins, optionalTestDataIndex);
133138
UserSecondaryCredentials = GetCredentialsFromSet(testAuthSets.Admins, optionalTestDataIndex + offset);
139+
140+
_updateTaskCts = new CancellationTokenSource();
141+
_updateTask = UpdateTaskAsync();
142+
}
143+
144+
private async Task UpdateTaskAsync()
145+
{
146+
Debug.LogWarning("UpdateTaskAsync STARTED");
147+
while (!_updateTaskCts.Token.IsCancellationRequested)
148+
{
149+
try
150+
{
151+
_updateTaskCts.Token.ThrowIfCancellationRequested();
152+
}
153+
catch (Exception)
154+
{
155+
Debug.LogWarning("UpdateTaskAsync STOPPED");
156+
throw;
157+
}
158+
159+
((IStreamChatClientEventsListener)_stateClient)?.Update();
160+
((IStreamChatClientEventsListener)_otherStateClient)?.Update();
161+
_lowLevelClient?.Update(0.1f);
162+
163+
await Task.Delay(1);
164+
}
134165
}
135166

136167
private AuthCredentials GetCredentialsFromSet(AuthCredentials[] set, int? forcedIndex)
@@ -209,6 +240,8 @@ private Task TryDisposeInstancesAsync()
209240

210241
Debug.Log("------------ Tests finished - dispose client instances");
211242

243+
_updateTaskCts.Cancel();
244+
212245
DisposeLowLevelClient();
213246
return DisposeStateClientsAsync();
214247
}

Assets/Plugins/StreamChat/Tests/UnityTestUtils.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,18 @@ public static IEnumerator RunAsIEnumerator<TResponse>(this Task<TResponse> task,
3131
onFaulted(ex);
3232
yield break;
3333
}
34-
34+
3535
throw ex;
3636
}
3737

3838
onSuccess?.Invoke(task.Result);
3939
}
4040

4141
public static IEnumerator RunAsIEnumerator(this Task task,
42-
Action onSuccess = null, Action<Exception> onFaulted = null, IStreamChatClient statefulClient = null)
42+
Action onSuccess = null, Action<Exception> onFaulted = null)
4343
{
4444
while (!task.IsCompleted)
4545
{
46-
statefulClient?.Update();
4746
yield return null;
4847
}
4948

@@ -55,13 +54,13 @@ public static IEnumerator RunAsIEnumerator(this Task task,
5554
onFaulted(ex);
5655
yield break;
5756
}
58-
57+
5958
throw ex;
6059
}
6160

6261
onSuccess?.Invoke();
6362
}
64-
63+
6564
public static IEnumerator RunAsIEnumerator(this Task task,
6665
IStreamChatLowLevelClient lowLevelClient, Action onSuccess = null, Action<Exception> onFaulted = null)
6766
{
@@ -79,7 +78,7 @@ public static IEnumerator RunAsIEnumerator(this Task task,
7978
onFaulted(ex);
8079
yield break;
8180
}
82-
81+
8382
throw ex;
8483
}
8584

@@ -92,7 +91,7 @@ public static IEnumerator WaitForClientToConnect(this IStreamChatLowLevelClient
9291
{
9392
yield break;
9493
}
95-
94+
9695
const float maxTimeToConnect = 3;
9796
var timeStarted = EditorApplication.timeSinceStartup;
9897

@@ -106,8 +105,6 @@ public static IEnumerator WaitForClientToConnect(this IStreamChatLowLevelClient
106105
break;
107106
}
108107

109-
lowLevelClient.Update(0.1f);
110-
111108
if (lowLevelClient.ConnectionState == ConnectionState.Connecting)
112109
{
113110
yield return null;
@@ -126,19 +123,18 @@ public static IEnumerator WaitForClientToConnect(this IStreamChatLowLevelClient
126123
}
127124
}
128125

129-
public static IEnumerator RunTaskAsEnumerator(this Task task, IStreamChatLowLevelClient client)
126+
public static IEnumerator RunTaskAsEnumerator(this Task task)
130127
{
131128
while (!task.IsCompleted)
132129
{
133-
client.Update(0.1f);
134130
yield return null;
135131
}
136-
132+
137133
if (!task.IsFaulted)
138134
{
139135
yield break;
140136
}
141-
137+
142138
if (task.Exception is AggregateException aggregateException &&
143139
aggregateException.InnerExceptions.Count == 1)
144140
{
@@ -147,8 +143,8 @@ public static IEnumerator RunTaskAsEnumerator(this Task task, IStreamChatLowLeve
147143

148144
throw task.Exception;
149145
}
150-
151-
public static string ToRfc3339String(this DateTime dateTime)
146+
147+
public static string ToRfc3339String(this DateTime dateTime)
152148
=> dateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz", DateTimeFormatInfo.InvariantInfo);
153149

154150
private static Exception UnwrapAggregateException(Exception exception)

0 commit comments

Comments
 (0)