Skip to content

Commit 3096626

Browse files
authored
Fix tests failing on docker container due to "upstream request timeout" response (#163)
1 parent 9feba6f commit 3096626

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

Assets/Plugins/StreamChat/Core/LowLevelClient/API/Internal/InternalApiClientBase.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,20 @@ private async Task<TResponse> HttpRequest<TResponse>(HttpMethodType httpMethod,
120120
}
121121
catch (Exception e)
122122
{
123+
// StreamTODO: verify where this error is coming from. This occurs when running tests in a docker container. Perhaps it's not returned from the API but by the network layer
124+
if (responseContent == "upstream request timeout")
125+
{
126+
// Handle API error returned as plain text
127+
apiError = new APIErrorInternalDTO
128+
{
129+
Message = responseContent,
130+
Code = 504,
131+
};
132+
throw new StreamApiException(apiError);
133+
}
134+
123135
LogRestCall(uri, endpoint, httpMethod, responseContent, success: false, logContent);
124-
136+
125137
#if STREAM_TESTS_ENABLED || STREAM_DEBUG_ENABLED
126138
var sb = new StringBuilder();
127139
sb.AppendLine("API Response Deserialization failed - StreamDeserializationException:");
@@ -130,10 +142,10 @@ private async Task<TResponse> HttpRequest<TResponse>(HttpMethodType httpMethod,
130142
sb.AppendLine(responseContent);
131143
_logs.Error(sb.ToString());
132144
#endif
133-
145+
134146
throw new StreamDeserializationException(responseContent, typeof(TResponse), e);
135147
}
136-
148+
137149
if (apiError.Code != InvalidAuthTokenErrorCode)
138150
{
139151
LogRestCall(uri, endpoint, httpMethod, responseContent, success: false, logContent);
@@ -142,15 +154,16 @@ private async Task<TResponse> HttpRequest<TResponse>(HttpMethodType httpMethod,
142154

143155
if (_lowLevelClient.ConnectionState == ConnectionState.Connected)
144156
{
145-
_logs.Info($"Http request failed due to expired token, connection id: {_lowLevelClient.ConnectionId}");
157+
_logs.Info(
158+
$"Http request failed due to expired token, connection id: {_lowLevelClient.ConnectionId}");
146159
await _lowLevelClient.DisconnectAsync();
147160
}
148161

149162
_logs.Info("New token required, connection state: " + _lowLevelClient.ConnectionState);
150163

151164
const int maxMsToWait = 500;
152165
var i = 0;
153-
166+
154167
//StreamTodo: we can create cancellation token instead of Task.Delay in loop
155168
while (_lowLevelClient.ConnectionState != ConnectionState.Connected)
156169
{
@@ -185,7 +198,7 @@ private async Task<TResponse> HttpRequest<TResponse>(HttpMethodType httpMethod,
185198
catch (Exception e)
186199
{
187200
LogRestCall(uri, endpoint, httpMethod, responseContent, success: false, logContent);
188-
201+
189202
#if STREAM_TESTS_ENABLED || STREAM_DEBUG_ENABLED
190203
var sb = new StringBuilder();
191204
sb.AppendLine("API Response Deserialization failed - StreamDeserializationException:");
@@ -194,13 +207,14 @@ private async Task<TResponse> HttpRequest<TResponse>(HttpMethodType httpMethod,
194207
sb.AppendLine(responseContent);
195208
_logs.Error(sb.ToString());
196209
#endif
197-
210+
198211
throw new StreamDeserializationException(responseContent, typeof(TResponse), e);
199212
}
200213
}
201214

202215
private static bool IsRequestBodyRequiredByHttpMethod(HttpMethodType httpMethod)
203-
=> httpMethod == HttpMethodType.Post || httpMethod == HttpMethodType.Put || httpMethod == HttpMethodType.Patch;
216+
=> httpMethod == HttpMethodType.Post || httpMethod == HttpMethodType.Put ||
217+
httpMethod == HttpMethodType.Patch;
204218

205219
private void LogFutureRequestIfDebug(Uri uri, string endpoint, HttpMethodType httpMethod, string request = null)
206220
{

Assets/Plugins/StreamChat/Libs/Http/HttpClientAdapter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public class HttpClientAdapter : IHttpClient
1313
public HttpClientAdapter()
1414
{
1515
_httpClient = new HttpClient();
16+
17+
#if STREAM_TESTS_ENABLED
18+
// Needed for tests run in a docker container. Timeouts can exceed the default value.
19+
_httpClient.Timeout = TimeSpan.FromSeconds(600);
20+
#endif
1621
}
1722

1823
public void SetDefaultAuthenticationHeader(string value)

0 commit comments

Comments
 (0)