@@ -29,27 +29,40 @@ class TestGlobalStore extends GlobalStore {
29
29
FakeApiConnection
30
30
> _apiConnections = {};
31
31
32
- /// Get or construct a [FakeApiConnection] with the given arguments.
33
- ///
34
- /// This breaches the base method's contract slightly, in that on repeated
35
- /// calls with the same arguments it returns the same connection, rather than
36
- /// a fresh one each time. That breach is very convenient for tests,
37
- /// enabling a test to get access to the same [FakeApiConnection] that the
38
- /// code under test will get, so as to use [FakeApiConnection.prepare]
39
- /// and [FakeApiConnection.lastRequest] .
32
+ /// Whether [apiConnection] should return a cached connection.
40
33
///
41
- /// However, if the connection returned by a previous call has been closed
42
- /// with [ApiConnection.close] , then that connection will be ignored in favor
34
+ /// If true, [apiConnection] will return a cached [FakeApiConnection]
35
+ /// from a previous call, if it is still open ([FakeApiConnection.isOpen] ).
36
+ /// If there is a cached connection but it has been closed
37
+ /// with [ApiConnection.close] , that connection will be ignored in favor
43
38
/// of returning (and saving for next time) a fresh connection after all.
39
+ ///
40
+ /// If false (the default), returns a fresh connection each time.
41
+ ///
42
+ /// Setting this to true is useful if a test needs to access the same
43
+ /// [FakeApiConnection] that the code under test will get, so as to use
44
+ /// [FakeApiConnection.prepare] or [FakeApiConnection.lastRequest] .
45
+ /// The behavior with `true` breaches the base method's contract slightly --
46
+ /// the base method would return a fresh connection each time --
47
+ /// but that breach is sometimes convenient for tests.
48
+ bool useCachedApiConnections = false ;
49
+
50
+ /// Get or construct a [FakeApiConnection] with the given arguments.
51
+ ///
52
+ /// To access the same [FakeApiConnection] that the code under test will get,
53
+ /// so as to use [FakeApiConnection.prepare] or [FakeApiConnection.lastRequest] ,
54
+ /// see [useCachedApiConnections] .
44
55
@override
45
56
FakeApiConnection apiConnection ({
46
57
required Uri realmUrl, required int ? zulipFeatureLevel,
47
58
String ? email, String ? apiKey}) {
48
59
final key = (realmUrl: realmUrl, zulipFeatureLevel: zulipFeatureLevel,
49
60
email: email, apiKey: apiKey);
50
- final connection = _apiConnections[key];
51
- if (connection != null && connection.isOpen) {
52
- return connection;
61
+ if (useCachedApiConnections) {
62
+ final connection = _apiConnections[key];
63
+ if (connection != null && connection.isOpen) {
64
+ return connection;
65
+ }
53
66
}
54
67
return (_apiConnections[key] = FakeApiConnection (
55
68
realmUrl: realmUrl, zulipFeatureLevel: zulipFeatureLevel,
0 commit comments