Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: pkg/scheduled_test/lib/src/scheduled_server/handler.dart

Issue 812253002: Delete a bunch of packages that are now on GitHub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Un-delete http Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
9 import 'package:shelf/shelf.dart' as shelf;
10
11 import '../../scheduled_server.dart';
12 import '../../scheduled_test.dart';
13 import '../utils.dart';
14
15 /// A handler for a single request to a [ScheduledServer].
16 class Handler {
17 /// The server for which this handler will handle a request.
18 final ScheduledServer server;
19
20 /// The expected method of the request to be handled.
21 final String method;
22
23 /// The expected path of the request to be handled.
24 final String path;
25
26 /// The function to run to handle the request.
27 shelf.Handler get fn => _fn;
28 shelf.Handler _fn;
29
30 /// The scheduled task immediately prior to this handler. If this task is
31 /// running when this handler receives a request, it should wait until the
32 /// task has completed.
33 ///
34 /// The last task in the queue will be the prior task because a Handler is
35 /// created before its associated [schedule] call.
36 final Task _taskBefore = currentSchedule.tasks.contents.last;
37
38 /// The result of running this handler. If an error occurs while running the
39 /// handler, that will be piped through this [Future].
40 Future get result => _resultCompleter.future;
41 final _resultCompleter = new Completer();
42
43 /// Whether it's time for the handler to receive its request.
44 var ready = false;
45
46 Handler(this.server, this.method, this.path, shelf.Handler fn) {
47 _fn = (request) {
48 return _waitForTask().then((_) {
49 if (!ready) {
50 fail("'${server.description}' received $method $path earlier than "
51 "expected.");
52 }
53
54 var description = "'${server.description}' handling ${request.method} "
55 "${request.url.path}";
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
58 // while executing a handler.
59 chainToCompleter(schedule(() {
60 return syncFuture(() {
61 if (request.method != method || request.url.path != path) {
62 fail("'${server.description}' expected $method $path, "
63 "but got ${request.method} ${request.url.path}.");
64 }
65
66 return fn(request);
67 });
68 }, description), _resultCompleter);
69
70 return _resultCompleter.future;
71 });
72 };
73 }
74
75 /// If the current task is [_taskBefore], waits for it to finish before
76 /// completing. Otherwise, completes immediately.
77 Future _waitForTask() {
78 return pumpEventQueue().then((_) {
79 if (currentSchedule.currentTask != _taskBefore) return null;
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
82 // set.
83 return _taskBefore.result.then((_) => pumpEventQueue());
84 });
85 }
86 }
OLDNEW
« no previous file with comments | « pkg/scheduled_test/lib/src/scheduled_future_matchers.dart ('k') | pkg/scheduled_test/lib/src/stream_matcher.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698