OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library scheduled_test.scheduled_server; | 5 library scheduled_test.scheduled_server; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
11 import 'package:stack_trace/stack_trace.dart'; | 11 import 'package:stack_trace/stack_trace.dart'; |
12 | 12 |
13 import 'scheduled_test.dart'; | 13 import 'scheduled_test.dart'; |
14 import 'src/scheduled_server/handler.dart'; | 14 import 'src/scheduled_server/handler.dart'; |
15 import 'src/scheduled_server/safe_http_server.dart'; | |
16 import 'src/utils.dart'; | 15 import 'src/utils.dart'; |
17 | 16 |
18 typedef Future ScheduledHandler(HttpRequest request); | 17 typedef Future ScheduledHandler(HttpRequest request); |
19 | 18 |
20 /// A class representing an [HttpServer] that's scheduled to run in the course | 19 /// A class representing an [HttpServer] that's scheduled to run in the course |
21 /// of the test. This class allows the server's request handling to be scheduled | 20 /// of the test. This class allows the server's request handling to be scheduled |
22 /// synchronously. | 21 /// synchronously. |
23 /// | 22 /// |
24 /// The server expects requests to be received in the order [handle] is called, | 23 /// The server expects requests to be received in the order [handle] is called, |
25 /// and expects that no additional requests will be received. | 24 /// and expects that no additional requests will be received. |
(...skipping 16 matching lines...) Expand all Loading... |
42 | 41 |
43 /// Creates a new server listening on an automatically-allocated port on | 42 /// Creates a new server listening on an automatically-allocated port on |
44 /// 127.0.0.1. [description] is used to refer to the server in debugging | 43 /// 127.0.0.1. [description] is used to refer to the server in debugging |
45 /// messages. | 44 /// messages. |
46 factory ScheduledServer([String description]) { | 45 factory ScheduledServer([String description]) { |
47 var id = _count++; | 46 var id = _count++; |
48 if (description == null) description = 'scheduled server $id'; | 47 if (description == null) description = 'scheduled server $id'; |
49 | 48 |
50 var scheduledServer; | 49 var scheduledServer; |
51 scheduledServer = new ScheduledServer._(schedule(() { | 50 scheduledServer = new ScheduledServer._(schedule(() { |
52 return Chain.track(SafeHttpServer.bind("127.0.0.1", 0)).then((server) { | 51 return Chain.track(HttpServer.bind("127.0.0.1", 0)).then((server) { |
53 Chain.track(server).listen(scheduledServer._handleRequest, | 52 Chain.track(server).listen(scheduledServer._handleRequest, |
54 onError: currentSchedule.signalError); | 53 onError: currentSchedule.signalError); |
55 currentSchedule.onComplete.schedule(server.close); | 54 currentSchedule.onComplete.schedule(server.close); |
56 return server; | 55 return server; |
57 }); | 56 }); |
58 }, "starting '$description'"), description); | 57 }, "starting '$description'"), description); |
59 return scheduledServer; | 58 return scheduledServer; |
60 } | 59 } |
61 | 60 |
62 /// The port on which the server is listening. | 61 /// The port on which the server is listening. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 "when no more requests were expected."); | 97 "when no more requests were expected."); |
99 } | 98 } |
100 return _handlers.removeFirst().fn(request); | 99 return _handlers.removeFirst().fn(request); |
101 }).catchError((e) { | 100 }).catchError((e) { |
102 // Close the server so that we don't leave a dangling request. | 101 // Close the server so that we don't leave a dangling request. |
103 _server.then((s) => s.close(force: true)); | 102 _server.then((s) => s.close(force: true)); |
104 throw e; | 103 throw e; |
105 }), 'receiving ${request.method} ${request.uri}'); | 104 }), 'receiving ${request.method} ${request.uri}'); |
106 } | 105 } |
107 } | 106 } |
OLD | NEW |