Skip to content

Commit 55d3f86

Browse files
committed
fake_api [nfc]: Add support for simulating delayed connections
There are scenarios where we need to simulate the application's behavior while waiting for a connection to be established. This commit introduces the ability to do so by adding a `delay` parameter to the `prepare` method. This enhancement allows us to test and observe the application's response during connection delays, improving our ability to handle such situations gracefully.
1 parent 9525c4f commit 55d3f86

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

test/api/fake_api.dart

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ import 'package:zulip/model/store.dart';
88
import '../example_data.dart' as eg;
99

1010
sealed class _PreparedResponse {
11+
final Duration? delay;
12+
13+
_PreparedResponse({this.delay});
1114
}
1215

1316
class _PreparedException extends _PreparedResponse {
1417
final Object exception;
1518

16-
_PreparedException({required this.exception});
19+
_PreparedException({required this.exception, super.delay});
1720
}
1821

1922
class _PreparedSuccess extends _PreparedResponse {
2023
final int httpStatus;
2124
final List<int> bytes;
2225

23-
_PreparedSuccess({required this.httpStatus, required this.bytes});
26+
_PreparedSuccess({required this.httpStatus, required this.bytes, super.delay});
2427
}
2528

2629
/// An [http.Client] that accepts and replays canned responses, for testing.
@@ -46,19 +49,22 @@ class FakeHttpClient extends http.BaseClient {
4649
/// will be `body` if non-null, or `jsonEncode(json)` if `json` is non-null,
4750
/// or else ''. The `body` and `json` parameters must not both be non-null.
4851
///
52+
/// if `delay` is non-null, the response will be delayed by that duration.
53+
///
4954
/// If `exception` is non-null, then `httpStatus`, `body`, and `json` must
5055
/// all be null, and the next request will throw the given exception.
5156
void prepare({
5257
Object? exception,
5358
int? httpStatus,
5459
Map<String, dynamic>? json,
5560
String? body,
61+
Duration? delay,
5662
}) {
5763
assert(_nextResponse == null,
5864
'FakeApiConnection.prepare was called while already expecting a request');
5965
if (exception != null) {
6066
assert(httpStatus == null && json == null && body == null);
61-
_nextResponse = _PreparedException(exception: exception);
67+
_nextResponse = _PreparedException(exception: exception, delay: delay);
6268
} else {
6369
assert((json == null) || (body == null));
6470
final String resolvedBody = switch ((body, json)) {
@@ -69,6 +75,7 @@ class FakeHttpClient extends http.BaseClient {
6975
_nextResponse = _PreparedSuccess(
7076
httpStatus: httpStatus ?? 200,
7177
bytes: utf8.encode(resolvedBody),
78+
delay: delay,
7279
);
7380
}
7481
}
@@ -90,11 +97,11 @@ class FakeHttpClient extends http.BaseClient {
9097
_nextResponse = null;
9198

9299
switch (response) {
93-
case _PreparedException(:var exception):
94-
return Future(() => throw exception);
95-
case _PreparedSuccess(:var bytes, :var httpStatus):
100+
case _PreparedException(:var exception, :var delay):
101+
return Future.delayed(delay ?? Duration.zero, () => throw exception);
102+
case _PreparedSuccess(:var bytes, :var httpStatus, :var delay):
96103
final byteStream = http.ByteStream.fromBytes(bytes);
97-
return Future(() => http.StreamedResponse(
104+
return Future.delayed(delay ?? Duration.zero, () => http.StreamedResponse(
98105
byteStream, httpStatus, request: request));
99106
}
100107
}
@@ -201,15 +208,18 @@ class FakeApiConnection extends ApiConnection {
201208
/// will be `body` if non-null, or `jsonEncode(json)` if `json` is non-null,
202209
/// or else ''. The `body` and `json` parameters must not both be non-null.
203210
///
211+
/// if `delay` is non-null, the response will be delayed by that duration.
212+
///
204213
/// If `exception` is non-null, then `httpStatus`, `body`, and `json` must
205214
/// all be null, and the next request will throw the given exception.
206215
void prepare({
207216
Object? exception,
208217
int? httpStatus,
209218
Map<String, dynamic>? json,
210219
String? body,
220+
Duration? delay,
211221
}) {
212222
client.prepare(
213-
exception: exception, httpStatus: httpStatus, json: json, body: body);
223+
exception: exception, httpStatus: httpStatus, json: json, body: body, delay: delay);
214224
}
215225
}

0 commit comments

Comments
 (0)