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