| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 return _waitForTask().then((_) { | 46 return _waitForTask().then((_) { |
| 47 if (!ready) { | 47 if (!ready) { |
| 48 fail("'${server.description}' received " | 48 fail("'${server.description}' received " |
| 49 "$method $path earlier than 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 syncFuture(() { |
| 57 if (request.method != method || request.uri.path != path) { | 57 if (request.method != method || request.uri.path != path) { |
| 58 fail("'${server.description}' expected $method $path, " | 58 fail("'${server.description}' expected $method $path, " |
| 59 "but got ${request.method} ${request.uri.path}."); | 59 "but got ${request.method} ${request.uri.path}."); |
| 60 } | 60 } |
| 61 | 61 |
| 62 return fn(request); | 62 return fn(request); |
| 63 }); | 63 }); |
| 64 }, "'${server.description}' handling ${request.method} ${request.uri}"), | 64 }, "'${server.description}' handling ${request.method} ${request.uri}"), |
| 65 _resultCompleter); | 65 _resultCompleter); |
| 66 }); | 66 }); |
| 67 }; | 67 }; |
| 68 } | 68 } |
| 69 | 69 |
| 70 /// If the current task is [_taskBefore], waits for it to finish before | 70 /// If the current task is [_taskBefore], waits for it to finish before |
| 71 /// completing. Otherwise, completes immediately. | 71 /// completing. Otherwise, completes immediately. |
| 72 Future _waitForTask() { | 72 Future _waitForTask() { |
| 73 return pumpEventQueue().then((_) { | 73 return pumpEventQueue().then((_) { |
| 74 if (currentSchedule.currentTask != _taskBefore) return; | 74 if (currentSchedule.currentTask != _taskBefore) return null; |
| 75 // If we're one task before the handler was scheduled, wait for that | 75 // 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 | 76 // task to complete and pump the event queue so that [ready] will be |
| 77 // set. | 77 // set. |
| 78 return _taskBefore.result.then((_) => pumpEventQueue()); | 78 return _taskBefore.result.then((_) => pumpEventQueue()); |
| 79 }); | 79 }); |
| 80 } | 80 } |
| 81 } | 81 } |
| OLD | NEW |