Skip to content

Commit 1a5c224

Browse files
committed
More accurate connection counts.
1 parent f886bbd commit 1a5c224

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

packages/powersync/test/streaming_sync_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ void main() {
5959

6060
await pdb.close();
6161

62+
// Give some time for connections to close
63+
final watch = Stopwatch()..start();
64+
while (server.connectionCount != 0 && watch.elapsedMilliseconds < 100) {
65+
await Future.delayed(Duration(milliseconds: random.nextInt(10)));
66+
}
67+
68+
expect(server.connectionCount, equals(0));
69+
expect(server.maxConnectionCount, lessThanOrEqualTo(1));
70+
6271
server.close();
6372
}
6473
});

packages/powersync/test/test_server.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import 'package:shelf_router/shelf_router.dart';
1111
class TestServer {
1212
late HttpServer server;
1313
Router app = Router();
14-
int connectionCount = 0;
1514
int maxConnectionCount = 0;
1615
int tokenExpiresIn;
1716

@@ -27,19 +26,22 @@ class TestServer {
2726
return 'http://${server.address.host}:${server.port}';
2827
}
2928

29+
int get connectionCount {
30+
return server.connectionsInfo().total;
31+
}
32+
33+
HttpConnectionsInfo connectionsInfo() {
34+
return server.connectionsInfo();
35+
}
36+
3037
Future<Response> handleSyncStream(Request request) async {
31-
connectionCount += 1;
3238
maxConnectionCount = max(connectionCount, maxConnectionCount);
3339

3440
stream() async* {
35-
try {
36-
var blob = "*" * 5000;
37-
for (var i = 0; i < 50; i++) {
38-
yield {"token_expires_in": tokenExpiresIn, "blob": blob};
39-
await Future.delayed(Duration(microseconds: 1));
40-
}
41-
} finally {
42-
connectionCount -= 1;
41+
var blob = "*" * 5000;
42+
for (var i = 0; i < 50; i++) {
43+
yield {"token_expires_in": tokenExpiresIn, "blob": blob};
44+
await Future.delayed(Duration(microseconds: 1));
4345
}
4446
}
4547

0 commit comments

Comments
 (0)