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.handler; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import '../../scheduled_server.dart'; |
| 11 import '../../scheduled_test.dart'; |
| 12 import '../utils.dart'; |
| 13 |
| 14 /// A handler for a single request to a [ScheduledServer]. |
| 15 class Handler { |
| 16 /// The server for which this handler will handle a request. |
| 17 final ScheduledServer server; |
| 18 |
| 19 /// The expected method of the request to be handled. |
| 20 final String method; |
| 21 |
| 22 /// The expected path of the request to be handled. |
| 23 final String path; |
| 24 |
| 25 /// The function to run to handle the request. |
| 26 ScheduledHandler get fn => _fn; |
| 27 ScheduledHandler _fn; |
| 28 |
| 29 /// The scheduled task immediately prior to this handler. If this task is |
| 30 /// running when this handler receives a request, it should wait until the |
| 31 /// task has completed. |
| 32 /// |
| 33 /// The last task in the queue will be the prior task because a Handler is |
| 34 /// created before its associated [schedule] call. |
| 35 final Task _taskBefore = currentSchedule.tasks.contents.last; |
| 36 |
| 37 /// The result of running this handler. If an error occurs while running the |
| 38 /// handler, that will be piped through this [Future]. |
| 39 Future get result => _resultCompleter.future; |
| 40 final _resultCompleter = new Completer(); |
| 41 |
| 42 /// Whether it's time for the handler to receive its request. |
| 43 var ready = false; |
| 44 |
| 45 Handler(this.server, this.method, this.path, ScheduledHandler fn) { |
| 46 _fn = (request) { |
| 47 return _waitForTask().then((_) { |
| 48 if (!ready) { |
| 49 throw "'${server.description}' received $method $path earlier than " |
| 50 "expected."; |
| 51 } |
| 52 |
| 53 // Use a nested call to [schedule] to help the user tell the difference |
| 54 // between a test failing while waiting for a handler and a test failing |
| 55 // while executing a handler. |
| 56 chainToCompleter(schedule(() { |
| 57 return new Future.of(() { |
| 58 if (request.method != method || request.uri.path != path) { |
| 59 throw "'${server.description}' expected $method $path, " |
| 60 "but got ${request.method} ${request.uri.path}."; |
| 61 } |
| 62 |
| 63 return fn(request); |
| 64 }); |
| 65 }, "'${server.description}' handling ${request.method} ${request.uri}"), |
| 66 _resultCompleter); |
| 67 }); |
| 68 }; |
| 69 } |
| 70 |
| 71 /// If the current task is [_taskBefore], waits for it to finish before |
| 72 /// completing. Otherwise, completes immediately. |
| 73 Future _waitForTask() { |
| 74 return pumpEventQueue().then((_) { |
| 75 if (currentSchedule.currentTask != _taskBefore) return; |
| 76 // If we're one task before the handler was scheduled, wait for that |
| 77 // task to complete and pump the event queue so that [ready] will be |
| 78 // set. |
| 79 return _taskBefore.result.then((_) => pumpEventQueue()); |
| 80 }); |
| 81 } |
| 82 } |
OLD | NEW |