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