forked from dart-archive/http_io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_reuse_server_port_test.dart
71 lines (64 loc) · 1.92 KB
/
http_reuse_server_port_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'package:http_io/http_io.dart';
import 'package:test/test.dart';
Future<int> runServer(int port, int connections, bool clean) {
Completer<int> completer = Completer<int>();
HttpServer.bind("127.0.0.1", port).then((server) {
int i = 0;
server.listen((request) {
request.pipe(request.response);
i++;
if (!clean && i == 10) {
int port = server.port;
server.close().then((_) => completer.complete(port));
}
});
Future.wait(List.generate(connections, (_) {
var client = HttpClient();
return client
.get("127.0.0.1", server.port, "/")
.then((request) => request.close())
.then((response) => response.drain())
.catchError((e) {
if (clean) throw e;
});
})).then((_) {
if (clean) {
int port = server.port;
server.close().then((_) => completer.complete(port));
}
});
});
return completer.future;
}
Future<Null> testReusePort() {
final completer = Completer<Null>();
runServer(0, 10, true).then((int port) {
// Stress test the port reusing it 10 times.
Future.forEach(List(10), (_) {
return runServer(port, 10, true);
}).then((_) {
completer.complete();
});
});
return completer.future;
}
Future<Null> testUncleanReusePort() {
final completer = Completer<Null>();
runServer(0, 10, false).then((int port) {
// Stress test the port reusing it 10 times.
Future.forEach(List(10), (_) {
return runServer(port, 10, false);
}).then((_) {
completer.complete();
});
});
return completer.future;
}
void main() {
test('reusePort', testReusePort);
test('uncleanedReusePort', testUncleanReusePort);
}