Skip to content

Commit

Permalink
Add extra delay if test failed due to "too many requests" (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
sierpinskid authored Mar 26, 2024
1 parent 3f7f19f commit 3b41d62
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected async Task<ChannelState> CreateTempUniqueChannelAsync(string channelTy
}

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

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

response = await task();
try
{
response = await task();
}
catch (StreamApiException streamApiException)
{
// Check for "Too many requests" error
if (streamApiException.StatusCode == 429)
{
const int tooManyRequestsDelay = 4;
Debug.Log($"Wait {tooManyRequestsDelay} seconds due to \"too many requests\" error");
await Task.Delay(tooManyRequestsDelay * 1000);
}
}

if (successCondition(response))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private async Task Send_message_with_url_Async()
Limit = 30,
Offset = 0,
},
}),channelState => channelState.Messages != null && channelState.Messages.Count > 0);
}),state => state.Messages != null && state.Messages.Count > 0);

Assert.IsNotNull(channelState.Messages);
Assert.IsNotEmpty(channelState.Messages);
Expand Down
43 changes: 38 additions & 5 deletions Assets/Plugins/StreamChat/Tests/StreamTestClients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ namespace StreamChat.Tests
/// </summary>
internal class StreamTestClients
{
public static StreamTestClients Instance => _instance ??= new StreamTestClients();
public static StreamTestClients Instance
{
get
{
if (_instance == null)
{
_instance = new StreamTestClients();
}

return _instance;
}
}

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

Expand All @@ -44,9 +55,31 @@ public IStreamChatLowLevelClient LowLevelClient
}
}

public StreamChatClient StateClient => _stateClient ??= CreateStateClient();
public StreamChatClient StateClient
{
get
{
if (_stateClient == null)
{
_stateClient = CreateStateClient();
}

return _stateClient;
}
}

public StreamChatClient OtherStateClient
{
get
{
if (_otherStateClient == null)
{
_otherStateClient = CreateStateClient();
}

public StreamChatClient OtherStateClient => _otherStateClient ??= CreateStateClient();
return _otherStateClient;
}
}

public OwnUser LowLevelClientOwnUser { get; private set; }

Expand Down Expand Up @@ -110,7 +143,7 @@ private static async Task<IStreamChatClient> ConnectStateClientAsync(IStreamChat
const int timeout = 5000;
var timer = new Stopwatch();
timer.Start();

var connectTask = client.ConnectUserAsync(credentials);
while (!connectTask.IsCompleted)
{
Expand All @@ -125,7 +158,7 @@ private static async Task<IStreamChatClient> ConnectStateClientAsync(IStreamChat
throw new TimeoutException($"Reached timeout when trying to connect user: {credentials.UserId}");
}
}

timer.Stop();

Debug.Log(
Expand Down

0 comments on commit 3b41d62

Please sign in to comment.