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 library scheduled_server; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:collection'; |
| 9 import 'dart:io'; |
| 10 import 'dart:uri'; |
| 11 |
| 12 import 'scheduled_test.dart'; |
| 13 import 'src/utils.dart'; |
| 14 import 'src/scheduled_server/handler.dart'; |
| 15 import 'src/scheduled_server/safe_http_server.dart'; |
| 16 |
| 17 typedef Future ScheduledHandler(HttpRequest request); |
| 18 |
| 19 /// A class representing an [HttpServer] that's scheduled to run in the course |
| 20 /// of the test. This class allows the server's request handling to be scheduled |
| 21 /// synchronously. |
| 22 /// |
| 23 /// The server expects requests to be received in the order [handle] is called, |
| 24 /// and expects that no additional requests will be received. |
| 25 class ScheduledServer { |
| 26 /// The description of the server. |
| 27 final String description; |
| 28 |
| 29 /// The wrapped server. |
| 30 final Future<HttpServer> _server; |
| 31 |
| 32 /// The queue of handlers to run for upcoming requests. Each [Future] will |
| 33 /// complete once the schedule reaches the point where that handler was |
| 34 /// scheduled. |
| 35 final _handlers = new Queue<Handler>(); |
| 36 |
| 37 /// The number of servers created. Used for auto-generating descriptions; |
| 38 static var _count = 0; |
| 39 |
| 40 ScheduledServer._(this._server, this.description); |
| 41 |
| 42 /// Creates a new server listening on an automatically-allocated port on |
| 43 /// localhost. [description] is used to refer to the server in debugging |
| 44 /// messages. |
| 45 factory ScheduledServer([String description]) { |
| 46 var id = _count++; |
| 47 if (description == null) description = 'scheduled server $id'; |
| 48 |
| 49 var scheduledServer; |
| 50 scheduledServer = new ScheduledServer._(schedule(() { |
| 51 return SafeHttpServer.bind("127.0.0.1", 0).then((server) { |
| 52 server.listen(scheduledServer._handleRequest, |
| 53 onError: (e) => currentSchedule.signalError(e)); |
| 54 currentSchedule.onComplete.schedule(server.close); |
| 55 return server; |
| 56 }); |
| 57 }, "starting '$description'"), description); |
| 58 return scheduledServer; |
| 59 } |
| 60 |
| 61 /// The port on which the server is listening. |
| 62 Future<int> get port => _server.then((s) => s.port); |
| 63 |
| 64 /// The base URL of the server, including its port. |
| 65 Future<Uri> get url => port.then((p) => Uri.parse("http://localhost:$p")); |
| 66 |
| 67 /// Schedules [handler] to handle a request to the server with [method] and |
| 68 /// [path]. The schedule will wait until an HTTP request is received. If that |
| 69 /// request doesn't have the expected [method] and [path], it will fail. |
| 70 /// Otherwise, it will run [fn]. If [fn] returns a [Future], the schedule will |
| 71 /// wait until that [Future] completes. |
| 72 /// |
| 73 /// The request must be received at the point in the schedule at which |
| 74 /// [handle] was called, or in the task immediately prior (to allow for |
| 75 /// non-deterministic asynchronicity). Otherwise, an error will be thrown. |
| 76 void handle(String method, String path, ScheduledHandler fn) { |
| 77 var handler = new Handler(this, method, path, fn); |
| 78 _handlers.add(handler); |
| 79 schedule(() { |
| 80 handler.ready = true; |
| 81 return handler.result.catchError((e) { |
| 82 // Close the server so that we don't leave a dangling request. |
| 83 _server.then((s) => s.close()); |
| 84 throw e; |
| 85 }); |
| 86 }, "'$description' waiting for $method $path"); |
| 87 } |
| 88 |
| 89 /// The handler for incoming [HttpRequest]s to this server. This dispatches |
| 90 /// the request to the first handler in the queue. It's that handler's |
| 91 /// responsibility to check that the method/path are correct and that it's |
| 92 /// being run at the correct time. |
| 93 void _handleRequest(HttpRequest request) { |
| 94 wrapFuture(new Future.of(() { |
| 95 if (_handlers.isEmpty) { |
| 96 throw "'$description' received ${request.method} ${request.uri.path} " |
| 97 "when no more requests were expected."; |
| 98 } |
| 99 return _handlers.removeFirst().fn(request); |
| 100 }).catchError((e) { |
| 101 // Close the server so that we don't leave a dangling request. |
| 102 _server.then((s) => s.close()); |
| 103 throw e; |
| 104 }), 'receiving ${request.method} ${request.uri}'); |
| 105 } |
| 106 } |
OLD | NEW |