| 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 '../../scheduled_server.dart'; | 9 import '../../scheduled_server.dart'; |
| 10 import '../../scheduled_test.dart'; | 10 import '../../scheduled_test.dart'; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 Future get result => _resultCompleter.future; | 38 Future get result => _resultCompleter.future; |
| 39 final _resultCompleter = new Completer(); | 39 final _resultCompleter = new Completer(); |
| 40 | 40 |
| 41 /// Whether it's time for the handler to receive its request. | 41 /// Whether it's time for the handler to receive its request. |
| 42 var ready = false; | 42 var ready = false; |
| 43 | 43 |
| 44 Handler(this.server, this.method, this.path, ScheduledHandler fn) { | 44 Handler(this.server, this.method, this.path, ScheduledHandler fn) { |
| 45 _fn = (request) { | 45 _fn = (request) { |
| 46 return _waitForTask().then((_) { | 46 return _waitForTask().then((_) { |
| 47 if (!ready) { | 47 if (!ready) { |
| 48 throw "'${server.description}' received $method $path earlier than " | 48 throw new ScheduledServerError("'${server.description}' received " |
| 49 "expected."; | 49 "$method $path earlier than expected.")
; |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Use a nested call to [schedule] to help the user tell the difference | 52 // 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 | 53 // between a test failing while waiting for a handler and a test failing |
| 54 // while executing a handler. | 54 // while executing a handler. |
| 55 chainToCompleter(schedule(() { | 55 chainToCompleter(schedule(() { |
| 56 return new Future.sync(() { | 56 return new Future.sync(() { |
| 57 if (request.method != method || request.uri.path != path) { | 57 if (request.method != method || request.uri.path != path) { |
| 58 throw "'${server.description}' expected $method $path, " | 58 throw new ScheduledServerError( |
| 59 "but got ${request.method} ${request.uri.path}."; | 59 "'${server.description}' expected $method $path, " |
| 60 "but got ${request.method} ${request.uri.path}."); |
| 60 } | 61 } |
| 61 | 62 |
| 62 return fn(request); | 63 return fn(request); |
| 63 }); | 64 }); |
| 64 }, "'${server.description}' handling ${request.method} ${request.uri}"), | 65 }, "'${server.description}' handling ${request.method} ${request.uri}"), |
| 65 _resultCompleter); | 66 _resultCompleter); |
| 66 }); | 67 }); |
| 67 }; | 68 }; |
| 68 } | 69 } |
| 69 | 70 |
| 70 /// If the current task is [_taskBefore], waits for it to finish before | 71 /// If the current task is [_taskBefore], waits for it to finish before |
| 71 /// completing. Otherwise, completes immediately. | 72 /// completing. Otherwise, completes immediately. |
| 72 Future _waitForTask() { | 73 Future _waitForTask() { |
| 73 return pumpEventQueue().then((_) { | 74 return pumpEventQueue().then((_) { |
| 74 if (currentSchedule.currentTask != _taskBefore) return; | 75 if (currentSchedule.currentTask != _taskBefore) return; |
| 75 // If we're one task before the handler was scheduled, wait for that | 76 // 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 | 77 // task to complete and pump the event queue so that [ready] will be |
| 77 // set. | 78 // set. |
| 78 return _taskBefore.result.then((_) => pumpEventQueue()); | 79 return _taskBefore.result.then((_) => pumpEventQueue()); |
| 79 }); | 80 }); |
| 80 } | 81 } |
| 81 } | 82 } |
| OLD | NEW |