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

Side by Side Diff: pkg/analysis_server/test/mocks.dart

Issue 243703008: Eliminate nondeterminism from analysis server unit tests. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 months 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
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 mocks; 5 library mocks;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:io'; 8 import 'dart:io';
9 9
10 import 'package:analyzer/src/generated/engine.dart'; 10 import 'package:analyzer/src/generated/engine.dart';
(...skipping 13 matching lines...) Expand all
24 // Verify the directory exists 24 // Verify the directory exists
25 Directory sdkDir = new Directory.fromUri(sdkUri); 25 Directory sdkDir = new Directory.fromUri(sdkUri);
26 if (!sdkDir.existsSync()) { 26 if (!sdkDir.existsSync()) {
27 throw 'Specified Dart SDK does not exist: $sdkDir'; 27 throw 'Specified Dart SDK does not exist: $sdkDir';
28 } 28 }
29 29
30 return sdkDir.path; 30 return sdkDir.path;
31 } 31 }
32 32
33 /** 33 /**
34 * Returns a [Future] that completes after pumping the event queue [times]
35 * times. By default, this should pump the event queue enough times to allow
36 * any code to run, as long as it's not waiting on some external event.
37 */
38 Future pumpEventQueue([int times = 20]) {
39 if (times == 0) return new Future.value();
40 // We use a delayed future to allow microtask events to finish. The
41 // Future.value or Future() constructors use scheduleMicrotask themselves and
42 // would therefore not wait for microtask callbacks that are scheduled after
43 // invoking this method.
44 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1));
45 }
46
47 /**
34 * A mock [WebSocket] for testing. 48 * A mock [WebSocket] for testing.
35 */ 49 */
36 class MockSocket<T> implements WebSocket { 50 class MockSocket<T> implements WebSocket {
37 StreamController controller = new StreamController(); 51 StreamController controller = new StreamController();
38 MockSocket twin; 52 MockSocket twin;
39 Stream stream; 53 Stream stream;
40 54
41 factory MockSocket.pair() { 55 factory MockSocket.pair() {
42 MockSocket socket1 = new MockSocket(); 56 MockSocket socket1 = new MockSocket();
43 MockSocket socket2 = new MockSocket(); 57 MockSocket socket2 = new MockSocket();
(...skipping 18 matching lines...) Expand all
62 StreamSubscription<T> listen(void onData(T event), 76 StreamSubscription<T> listen(void onData(T event),
63 { Function onError, void onDone(), bool cancelOnError}) => 77 { Function onError, void onDone(), bool cancelOnError}) =>
64 stream.listen(onData, onError: onError, onDone: onDone, 78 stream.listen(onData, onError: onError, onDone: onDone,
65 cancelOnError: cancelOnError); 79 cancelOnError: cancelOnError);
66 80
67 Stream<T> where(bool test(T)) => stream.where(test); 81 Stream<T> where(bool test(T)) => stream.where(test);
68 82
69 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); 83 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
70 } 84 }
71 85
86 class NoResponseException implements Exception {
87 /** The request that was not responded to. */
Brian Wilkerson 2014/04/21 14:18:05 I don't know for sure, but I suspect that this sho
88 final Request request;
89
90 NoResponseException(this.request);
91
92 String toString() {
93 return "NoResponseException after request ${request.toJson()}";
94 }
95 }
96
72 /** 97 /**
73 * A mock [ServerCommunicationChannel] for testing [AnalysisServer]. 98 * A mock [ServerCommunicationChannel] for testing [AnalysisServer].
74 */ 99 */
75 class MockServerChannel implements ServerCommunicationChannel { 100 class MockServerChannel implements ServerCommunicationChannel {
76 StreamController<Request> requestController = new StreamController<Request>(); 101 StreamController<Request> requestController = new StreamController<Request>();
77 StreamController<Response> responseController = new StreamController<Response> (); 102 StreamController<Response> responseController = new StreamController<Response> ();
78 StreamController<Notification> notificationController = new StreamController<N otification>(); 103 StreamController<Notification> notificationController = new StreamController<N otification>();
79 104
80 List<Response> responsesReceived = []; 105 List<Response> responsesReceived = [];
81 List<Notification> notificationsReceived = []; 106 List<Notification> notificationsReceived = [];
(...skipping 11 matching lines...) Expand all
93 notificationsReceived.add(notification); 118 notificationsReceived.add(notification);
94 // Wrap send notification in future to simulate websocket 119 // Wrap send notification in future to simulate websocket
95 new Future(() => notificationController.add(notification)); 120 new Future(() => notificationController.add(notification));
96 } 121 }
97 122
98 /// Simulate request/response pair 123 /// Simulate request/response pair
99 Future<Response> sendRequest(Request request) { 124 Future<Response> sendRequest(Request request) {
100 var id = request.id; 125 var id = request.id;
101 // Wrap send request in future to simulate websocket 126 // Wrap send request in future to simulate websocket
102 new Future(() => requestController.add(request)); 127 new Future(() => requestController.add(request));
103 return responseController.stream.firstWhere((response) => response.id == id) ; 128 pumpEventQueue().then((_) => responseController.addError(
129 new NoResponseException(request)));
130 return responseController.stream.firstWhere((response) => response.id == id
131 );
104 } 132 }
105 133
106 @override 134 @override
107 void sendResponse(Response response) { 135 void sendResponse(Response response) {
108 responsesReceived.add(response); 136 responsesReceived.add(response);
109 // Wrap send response in future to simulate websocket 137 // Wrap send response in future to simulate websocket
110 new Future(() => responseController.add(response)); 138 new Future(() => responseController.add(response));
111 } 139 }
112 140
113 void expectMsgCount({responseCount: 0, notificationCount: 0}) { 141 void expectMsgCount({responseCount: 0, notificationCount: 0}) {
114 expect(responsesReceived, hasLength(responseCount)); 142 expect(responsesReceived, hasLength(responseCount));
115 expect(notificationsReceived, hasLength(notificationCount)); 143 expect(notificationsReceived, hasLength(notificationCount));
116 } 144 }
117 } 145 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698