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 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import '../../scheduled_server.dart'; | 10 import '../../scheduled_server.dart'; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 return _waitForTask().then((_) { | 47 return _waitForTask().then((_) { |
48 if (!ready) { | 48 if (!ready) { |
49 throw "'${server.description}' received $method $path earlier than " | 49 throw "'${server.description}' received $method $path earlier than " |
50 "expected."; | 50 "expected."; |
51 } | 51 } |
52 | 52 |
53 // Use a nested call to [schedule] to help the user tell the difference | 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 | 54 // between a test failing while waiting for a handler and a test failing |
55 // while executing a handler. | 55 // while executing a handler. |
56 chainToCompleter(schedule(() { | 56 chainToCompleter(schedule(() { |
57 return new Future.of(() { | 57 return new Future.sync(() { |
58 if (request.method != method || request.uri.path != path) { | 58 if (request.method != method || request.uri.path != path) { |
59 throw "'${server.description}' expected $method $path, " | 59 throw "'${server.description}' expected $method $path, " |
60 "but got ${request.method} ${request.uri.path}."; | 60 "but got ${request.method} ${request.uri.path}."; |
61 } | 61 } |
62 | 62 |
63 return fn(request); | 63 return fn(request); |
64 }); | 64 }); |
65 }, "'${server.description}' handling ${request.method} ${request.uri}"), | 65 }, "'${server.description}' handling ${request.method} ${request.uri}"), |
66 _resultCompleter); | 66 _resultCompleter); |
67 }); | 67 }); |
68 }; | 68 }; |
69 } | 69 } |
70 | 70 |
71 /// 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 |
72 /// completing. Otherwise, completes immediately. | 72 /// completing. Otherwise, completes immediately. |
73 Future _waitForTask() { | 73 Future _waitForTask() { |
74 return pumpEventQueue().then((_) { | 74 return pumpEventQueue().then((_) { |
75 if (currentSchedule.currentTask != _taskBefore) return; | 75 if (currentSchedule.currentTask != _taskBefore) return; |
76 // 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 |
77 // 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 |
78 // set. | 78 // set. |
79 return _taskBefore.result.then((_) => pumpEventQueue()); | 79 return _taskBefore.result.then((_) => pumpEventQueue()); |
80 }); | 80 }); |
81 } | 81 } |
82 } | 82 } |
OLD | NEW |