Skip to content

Commit 3b41d62

Browse files
authored
Add extra delay if test failed due to "too many requests" (#139)
1 parent 3f7f19f commit 3b41d62

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected async Task<ChannelState> CreateTempUniqueChannelAsync(string channelTy
9494
}
9595

9696
var channelState = await Try(() => LowLevelClient.ChannelApi.GetOrCreateChannelAsync(channelType, channelId,
97-
channelGetOrCreateRequest), channelState => channelState != null);
97+
channelGetOrCreateRequest), state => state != null);
9898

9999
_tempChannelsCidsToDelete.Add(channelState.Channel.Cid);
100100
return channelState;
@@ -116,7 +116,20 @@ protected static async Task<T> Try<T>(Func<Task<T>> task, Predicate<T> successCo
116116
{
117117
attempt++;
118118

119-
response = await task();
119+
try
120+
{
121+
response = await task();
122+
}
123+
catch (StreamApiException streamApiException)
124+
{
125+
// Check for "Too many requests" error
126+
if (streamApiException.StatusCode == 429)
127+
{
128+
const int tooManyRequestsDelay = 4;
129+
Debug.Log($"Wait {tooManyRequestsDelay} seconds due to \"too many requests\" error");
130+
await Task.Delay(tooManyRequestsDelay * 1000);
131+
}
132+
}
120133

121134
if (successCondition(response))
122135
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private async Task Send_message_with_url_Async()
128128
Limit = 30,
129129
Offset = 0,
130130
},
131-
}),channelState => channelState.Messages != null && channelState.Messages.Count > 0);
131+
}),state => state.Messages != null && state.Messages.Count > 0);
132132

133133
Assert.IsNotNull(channelState.Messages);
134134
Assert.IsNotEmpty(channelState.Messages);

Assets/Plugins/StreamChat/Tests/StreamTestClients.cs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ namespace StreamChat.Tests
2121
/// </summary>
2222
internal class StreamTestClients
2323
{
24-
public static StreamTestClients Instance => _instance ??= new StreamTestClients();
24+
public static StreamTestClients Instance
25+
{
26+
get
27+
{
28+
if (_instance == null)
29+
{
30+
_instance = new StreamTestClients();
31+
}
32+
33+
return _instance;
34+
}
35+
}
2536

2637
public void AddLock(object owner) => _locks.Add(owner);
2738

@@ -44,9 +55,31 @@ public IStreamChatLowLevelClient LowLevelClient
4455
}
4556
}
4657

47-
public StreamChatClient StateClient => _stateClient ??= CreateStateClient();
58+
public StreamChatClient StateClient
59+
{
60+
get
61+
{
62+
if (_stateClient == null)
63+
{
64+
_stateClient = CreateStateClient();
65+
}
66+
67+
return _stateClient;
68+
}
69+
}
70+
71+
public StreamChatClient OtherStateClient
72+
{
73+
get
74+
{
75+
if (_otherStateClient == null)
76+
{
77+
_otherStateClient = CreateStateClient();
78+
}
4879

49-
public StreamChatClient OtherStateClient => _otherStateClient ??= CreateStateClient();
80+
return _otherStateClient;
81+
}
82+
}
5083

5184
public OwnUser LowLevelClientOwnUser { get; private set; }
5285

@@ -110,7 +143,7 @@ private static async Task<IStreamChatClient> ConnectStateClientAsync(IStreamChat
110143
const int timeout = 5000;
111144
var timer = new Stopwatch();
112145
timer.Start();
113-
146+
114147
var connectTask = client.ConnectUserAsync(credentials);
115148
while (!connectTask.IsCompleted)
116149
{
@@ -125,7 +158,7 @@ private static async Task<IStreamChatClient> ConnectStateClientAsync(IStreamChat
125158
throw new TimeoutException($"Reached timeout when trying to connect user: {credentials.UserId}");
126159
}
127160
}
128-
161+
129162
timer.Stop();
130163

131164
Debug.Log(

0 commit comments

Comments
 (0)