| 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; | 9 import 'package:shelf/shelf.dart' as shelf; |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 Handler(this.server, this.method, this.path, shelf.Handler fn) { | 46 Handler(this.server, this.method, this.path, shelf.Handler fn) { |
| 47 _fn = (request) { | 47 _fn = (request) { |
| 48 return _waitForTask().then((_) { | 48 return _waitForTask().then((_) { |
| 49 if (!ready) { | 49 if (!ready) { |
| 50 fail("'${server.description}' received $method $path earlier than " | 50 fail("'${server.description}' received $method $path earlier than " |
| 51 "expected."); | 51 "expected."); |
| 52 } | 52 } |
| 53 | 53 |
| 54 var description = "'${server.description}' handling ${request.method} " | 54 var description = "'${server.description}' handling ${request.method} " |
| 55 "${request.pathInfo}"; | 55 "${request.url.path}"; |
| 56 // 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 |
| 57 // 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 |
| 58 // while executing a handler. | 58 // while executing a handler. |
| 59 chainToCompleter(schedule(() { | 59 chainToCompleter(schedule(() { |
| 60 return syncFuture(() { | 60 return syncFuture(() { |
| 61 if (request.method != method || request.pathInfo != path) { | 61 if (request.method != method || request.url.path != path) { |
| 62 fail("'${server.description}' expected $method $path, " | 62 fail("'${server.description}' expected $method $path, " |
| 63 "but got ${request.method} ${request.pathInfo}."); | 63 "but got ${request.method} ${request.url.path}."); |
| 64 } | 64 } |
| 65 | 65 |
| 66 return fn(request); | 66 return fn(request); |
| 67 }); | 67 }); |
| 68 }, description), _resultCompleter); | 68 }, description), _resultCompleter); |
| 69 | 69 |
| 70 return _resultCompleter.future; | 70 return _resultCompleter.future; |
| 71 }); | 71 }); |
| 72 }; | 72 }; |
| 73 } | 73 } |
| 74 | 74 |
| 75 /// 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 |
| 76 /// completing. Otherwise, completes immediately. | 76 /// completing. Otherwise, completes immediately. |
| 77 Future _waitForTask() { | 77 Future _waitForTask() { |
| 78 return pumpEventQueue().then((_) { | 78 return pumpEventQueue().then((_) { |
| 79 if (currentSchedule.currentTask != _taskBefore) return null; | 79 if (currentSchedule.currentTask != _taskBefore) return null; |
| 80 // 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 |
| 81 // 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 |
| 82 // set. | 82 // set. |
| 83 return _taskBefore.result.then((_) => pumpEventQueue()); | 83 return _taskBefore.result.then((_) => pumpEventQueue()); |
| 84 }); | 84 }); |
| 85 } | 85 } |
| 86 } | 86 } |
| OLD | NEW |