OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:io'; |
| 6 |
| 7 import "package:async_helper/async_helper.dart"; |
| 8 |
| 9 // NOTE: This test tries to ensure that an HttpClient will close it's |
| 10 // underlying idle connections after [HttpClient.idleTimeout]. |
| 11 // |
| 12 // The main script spawns a server and a subprocess which does a connection back |
| 13 // to it. |
| 14 // The subprocess is expected to shut down it's idle sockets after |
| 15 // [HttpClient.idleTimeout] and the main script will assert that this happens |
| 16 // within +/- 2 <= seconds. |
| 17 |
| 18 const SECONDS = 10; |
| 19 |
| 20 void runServerProcess() { |
| 21 asyncStart(); |
| 22 HttpServer.bind('127.0.0.1', 0).then((server) { |
| 23 var url = 'http://127.0.0.1:${server.port}/'; |
| 24 |
| 25 server.idleTimeout = const Duration(hours: 1); |
| 26 |
| 27 var subscription = server.listen((HttpRequest request) { |
| 28 return request.response..write('hello world')..close(); |
| 29 }); |
| 30 |
| 31 var sw = new Stopwatch()..start(); |
| 32 var arguments = ['--package-root=${Platform.packageRoot}', |
| 33 '${Platform.script}', |
| 34 url]; |
| 35 Process.run(Platform.executable, arguments).then((res) { |
| 36 subscription.cancel(); |
| 37 if (res.exitCode != 0) { |
| 38 throw "Child exited with ${res.exitCode} instead of 0. " |
| 39 "(stdout: ${res.stdout}, stderr: ${res.stderr})"; |
| 40 } |
| 41 var seconds = sw.elapsed.inSeconds; |
| 42 // NOTE: There is a slight chance this will cause flakiness, but there is |
| 43 // no other good way of testing correctness of timing-dependent code |
| 44 // form the outside. |
| 45 if ((SECONDS - seconds).abs() > 2) { |
| 46 throw "Child did exit within $seconds seconds, but expected it to take " |
| 47 "roughly $SECONDS seconds."; |
| 48 } |
| 49 |
| 50 asyncEnd(); |
| 51 }); |
| 52 }); |
| 53 } |
| 54 |
| 55 void runClientProcess(String url) { |
| 56 var uri = Uri.parse(url); |
| 57 |
| 58 // NOTE: We make an HTTP client request and then *forget to close* the HTTP |
| 59 // client instance. The idle timer should fire after SECONDS. |
| 60 var client = new HttpClient(); |
| 61 client.idleTimeout = const Duration(seconds: SECONDS); |
| 62 |
| 63 client.getUrl(uri) |
| 64 .then((req) =>req.close()) |
| 65 .then((response) => response.drain()) |
| 66 .then((_) => print('drained client request')); |
| 67 } |
| 68 |
| 69 void main(List<String> args) { |
| 70 if (args.length == 1) { |
| 71 runClientProcess(args.first); |
| 72 } else { |
| 73 runServerProcess(); |
| 74 } |
| 75 } |
OLD | NEW |