Skip to content

Commit 9b05e60

Browse files
committed
Refactor delays
1 parent deb6034 commit 9b05e60

1 file changed

Lines changed: 21 additions & 60 deletions

File tree

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

Lines changed: 21 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using NUnit.Framework;
1010
using StreamChat.Core;
1111
using StreamChat.Core.Exceptions;
12-
using StreamChat.Core.InternalDTO.Requests;
1312
using StreamChat.Core.Requests;
1413
using StreamChat.Core.StatefulModels;
1514
using StreamChat.Libs.Auth;
@@ -59,33 +58,9 @@ protected async Task<IStreamChannel> CreateUniqueTempChannelAsync(string name =
5958
var channelId = "random-channel-11111-" + Guid.NewGuid();
6059
var client = overrideClient ?? Client;
6160

62-
const int maxAttempts = 20;
63-
for (var i = 0; i < maxAttempts; i++)
64-
{
65-
try
66-
{
67-
var channelState = await client.InternalGetOrCreateChannelWithIdAsync(ChannelType.Messaging, channelId, name, watch: watch);
68-
_tempChannels.Add(channelState);
69-
return channelState;
70-
}
71-
catch (StreamApiException e)
72-
{
73-
if (i == maxAttempts - 1)
74-
{
75-
throw;
76-
}
77-
78-
if (e.IsRateLimitExceededError())
79-
{
80-
await Task.Delay(i * 5 * 1000);
81-
continue;
82-
}
83-
84-
throw;
85-
}
86-
}
87-
88-
throw new InvalidOperationException($"{nameof(CreateUniqueTempChannelAsync)} failed to due to max attempts reached.");
61+
var channelState = await client.InternalGetOrCreateChannelWithIdAsync(ChannelType.Messaging, channelId, name, watch: watch);
62+
_tempChannels.Add(channelState);
63+
return channelState;
8964
}
9065

9166
/// <summary>
@@ -143,7 +118,7 @@ protected static async Task WaitWhileTrueAsync(Func<bool> condition, int maxSeco
143118
throw new TimeoutException("Timeout while waiting for condition");
144119
}
145120

146-
var delay = (int)Math.Max(1, Math.Min(400, Math.Pow(2, i)));
121+
var delay = (int)Math.Min(100 * 1000, Math.Pow(2, i + 9));
147122
await Task.Delay(delay);
148123
}
149124

@@ -167,7 +142,7 @@ protected static async Task WaitWhileFalseAsync(Func<bool> condition, int maxSec
167142
throw new TimeoutException("Timeout while waiting for condition");
168143
}
169144

170-
var delay = (int)Math.Max(1, Math.Min(400, Math.Pow(2, i)));
145+
var delay = (int)Math.Min(100 * 1000, Math.Pow(2, i + 9));
171146
await Task.Delay(delay);
172147
}
173148

@@ -183,29 +158,33 @@ protected static async Task WaitWithTimeoutAsync(Task task, string exceptionMsg,
183158
}
184159

185160
/// <summary>
186-
/// Timeout will be doubled on each subsequent attempt. So max timeout = <see cref="initTimeoutMs"/> * 2^<see cref="maxAttempts"/>
161+
/// Timeout will be doubled on each subsequent attempt. So max timeout = <see cref="initTimeoutMs"/> * 2^<see cref="maxSeconds"/>
187162
/// </summary>
188-
protected static async Task<T> TryAsync<T>(Func<Task<T>> task, Predicate<T> successCondition, int maxAttempts = 20,
189-
int initTimeoutMs = 150)
163+
protected static async Task<T> TryAsync<T>(Func<Task<T>> task, Predicate<T> successCondition,
164+
int maxSeconds = 1000)
190165
{
191-
var response = default(T);
166+
var sw = new Stopwatch();
167+
sw.Start();
192168

193-
var attemptsLeft = maxAttempts;
194-
while (attemptsLeft > 0)
169+
for (int i = 0; i < int.MaxValue; i++)
195170
{
196-
response = await task();
171+
var response = await task();
197172

198173
if (successCondition(response))
199174
{
200175
return response;
201176
}
177+
178+
if (sw.Elapsed.Seconds > maxSeconds)
179+
{
180+
throw new TimeoutException("Timeout while waiting for condition");
181+
}
202182

203-
var delay = initTimeoutMs * Math.Pow(2, (maxAttempts - attemptsLeft));
204-
await Task.Delay((int)delay);
205-
attemptsLeft--;
183+
var delay = (int)Math.Min(100 * 1000, Math.Pow(2, i + 9));
184+
await Task.Delay(delay);
206185
}
207186

208-
return response;
187+
throw new TimeoutException("Timeout while waiting for condition");
209188
}
210189

211190
private readonly List<IStreamChannel> _tempChannels = new List<IStreamChannel>();
@@ -253,25 +232,7 @@ private async Task DeleteTempChannelsAsync()
253232
return;
254233
}
255234

256-
for (int i = 0; i < 5; i++)
257-
{
258-
try
259-
{
260-
await Client.DeleteMultipleChannelsAsync(_tempChannels, isHardDelete: true);
261-
}
262-
catch (StreamApiException streamApiException)
263-
{
264-
if (streamApiException.Code == StreamApiException.RateLimitErrorHttpStatusCode)
265-
{
266-
await Task.Delay(1000);
267-
}
268-
269-
Debug.Log($"Attempt {i} failed. Try {nameof(DeleteTempChannelsAsync)} again due to exception: " + streamApiException);
270-
continue;
271-
}
272-
273-
break;
274-
}
235+
await Client.DeleteMultipleChannelsAsync(_tempChannels, isHardDelete: true);
275236

276237
_tempChannels.Clear();
277238
}

0 commit comments

Comments
 (0)