| 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'; |
| 12 |
| 11 import 'scheduled_test.dart'; | 13 import 'scheduled_test.dart'; |
| 12 import 'src/scheduled_server/handler.dart'; | 14 import 'src/scheduled_server/handler.dart'; |
| 13 import 'src/scheduled_server/safe_http_server.dart'; | 15 import 'src/scheduled_server/safe_http_server.dart'; |
| 16 import 'src/utils.dart'; |
| 14 | 17 |
| 15 typedef Future ScheduledHandler(HttpRequest request); | 18 typedef Future ScheduledHandler(HttpRequest request); |
| 16 | 19 |
| 17 /// A class representing an [HttpServer] that's scheduled to run in the course | 20 /// A class representing an [HttpServer] that's scheduled to run in the course |
| 18 /// of the test. This class allows the server's request handling to be scheduled | 21 /// of the test. This class allows the server's request handling to be scheduled |
| 19 /// synchronously. | 22 /// synchronously. |
| 20 /// | 23 /// |
| 21 /// The server expects requests to be received in the order [handle] is called, | 24 /// The server expects requests to be received in the order [handle] is called, |
| 22 /// and expects that no additional requests will be received. | 25 /// and expects that no additional requests will be received. |
| 23 class ScheduledServer { | 26 class ScheduledServer { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 39 | 42 |
| 40 /// Creates a new server listening on an automatically-allocated port on | 43 /// Creates a new server listening on an automatically-allocated port on |
| 41 /// 127.0.0.1. [description] is used to refer to the server in debugging | 44 /// 127.0.0.1. [description] is used to refer to the server in debugging |
| 42 /// messages. | 45 /// messages. |
| 43 factory ScheduledServer([String description]) { | 46 factory ScheduledServer([String description]) { |
| 44 var id = _count++; | 47 var id = _count++; |
| 45 if (description == null) description = 'scheduled server $id'; | 48 if (description == null) description = 'scheduled server $id'; |
| 46 | 49 |
| 47 var scheduledServer; | 50 var scheduledServer; |
| 48 scheduledServer = new ScheduledServer._(schedule(() { | 51 scheduledServer = new ScheduledServer._(schedule(() { |
| 49 return SafeHttpServer.bind("127.0.0.1", 0).then((server) { | 52 return Chain.track(SafeHttpServer.bind("127.0.0.1", 0)).then((server) { |
| 50 server.listen(scheduledServer._handleRequest, | 53 Chain.track(server).listen(scheduledServer._handleRequest, |
| 51 onError: currentSchedule.signalError); | 54 onError: currentSchedule.signalError); |
| 52 currentSchedule.onComplete.schedule(server.close); | 55 currentSchedule.onComplete.schedule(server.close); |
| 53 return server; | 56 return server; |
| 54 }); | 57 }); |
| 55 }, "starting '$description'"), description); | 58 }, "starting '$description'"), description); |
| 56 return scheduledServer; | 59 return scheduledServer; |
| 57 } | 60 } |
| 58 | 61 |
| 59 /// The port on which the server is listening. | 62 /// The port on which the server is listening. |
| 60 Future<int> get port => _server.then((s) => s.port); | 63 Future<int> get port => _server.then((s) => s.port); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 82 throw e; | 85 throw e; |
| 83 }); | 86 }); |
| 84 }, "'$description' waiting for $method $path"); | 87 }, "'$description' waiting for $method $path"); |
| 85 } | 88 } |
| 86 | 89 |
| 87 /// The handler for incoming [HttpRequest]s to this server. This dispatches | 90 /// The handler for incoming [HttpRequest]s to this server. This dispatches |
| 88 /// the request to the first handler in the queue. It's that handler's | 91 /// the request to the first handler in the queue. It's that handler's |
| 89 /// responsibility to check that the method/path are correct and that it's | 92 /// responsibility to check that the method/path are correct and that it's |
| 90 /// being run at the correct time. | 93 /// being run at the correct time. |
| 91 void _handleRequest(HttpRequest request) { | 94 void _handleRequest(HttpRequest request) { |
| 92 wrapFuture(new Future.sync(() { | 95 wrapFuture(syncFuture(() { |
| 93 if (_handlers.isEmpty) { | 96 if (_handlers.isEmpty) { |
| 94 fail("'$description' received ${request.method} ${request.uri.path} " | 97 fail("'$description' received ${request.method} ${request.uri.path} " |
| 95 "when no more requests were expected."); | 98 "when no more requests were expected."); |
| 96 } | 99 } |
| 97 return _handlers.removeFirst().fn(request); | 100 return _handlers.removeFirst().fn(request); |
| 98 }).catchError((e) { | 101 }).catchError((e) { |
| 99 // Close the server so that we don't leave a dangling request. | 102 // Close the server so that we don't leave a dangling request. |
| 100 _server.then((s) => s.close(force: true)); | 103 _server.then((s) => s.close(force: true)); |
| 101 throw e; | 104 throw e; |
| 102 }), 'receiving ${request.method} ${request.uri}'); | 105 }), 'receiving ${request.method} ${request.uri}'); |
| 103 } | 106 } |
| 104 } | 107 } |
| OLD | NEW |