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

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

Issue 185313002: restructure client api to use streams (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fix mock stream to more closely match websocket Created 6 years, 9 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 test.channel; 5 library test.channel;
6 6
7 import 'dart:async';
8
9 import 'mocks.dart';
Bob Nystrom 2014/03/04 01:03:45 Nit, but we tend to put local imports after "packa
danrubel 2014/03/05 15:32:04 Done.
10
11 import 'package:analysis_server/src/channel.dart';
7 import 'package:analysis_server/src/protocol.dart'; 12 import 'package:analysis_server/src/protocol.dart';
8 import 'package:unittest/matcher.dart'; 13 import 'package:unittest/matcher.dart';
9 import 'package:unittest/unittest.dart'; 14 import 'package:unittest/unittest.dart';
10 import 'package:analysis_server/src/channel.dart';
11 import 'mocks.dart';
12 15
13 main() { 16 main() {
14 group('Channel', () { 17 group('Channel', () {
18 setUp(ChannelTest.setUp);
15 test('invalidJsonToClient', ChannelTest.invalidJsonToClient); 19 test('invalidJsonToClient', ChannelTest.invalidJsonToClient);
16 test('invalidJsonToServer', ChannelTest.invalidJsonToServer); 20 test('invalidJsonToServer', ChannelTest.invalidJsonToServer);
17 test('notification', ChannelTest.notification); 21 test('notification', ChannelTest.notification);
22 test('notificationAndResponse', ChannelTest.notificationAndResponse);
18 test('request', ChannelTest.request); 23 test('request', ChannelTest.request);
24 test('requestResponse', ChannelTest.requestResponse);
19 test('response', ChannelTest.response); 25 test('response', ChannelTest.response);
20 }); 26 });
21 } 27 }
22 28
23 class ChannelTest { 29 class ChannelTest {
Bob Nystrom 2014/03/04 01:03:45 Why make a class for this?
danrubel 2014/03/05 15:32:04 It groups the tests. We will have more tests for t
nweiz 2014/03/05 20:16:39 You're already using [group] for this. If you need
danrubel 2014/03/11 19:06:09 Good point.
30 static MockSocket socket;
31 static WebSocketClientChannel client;
32 static WebSocketServerChannel server;
24 33
25 static void invalidJsonToClient() { 34 static List requestsReceived;
26 InvalidJsonMockSocket mockSocket = new InvalidJsonMockSocket(); 35 static List responsesReceived;
27 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); 36 static List notificationsReceived;
28 var responsesReceived = new List();
29 var notificationsReceived = new List();
30 client.listen((Response response) => responsesReceived.add(response),
31 (Notification notification) => notificationsReceived.add(notification));
32 37
33 mockSocket.addInvalid('"blat"'); 38 static void setUp() {
34 mockSocket.addInvalid('{foo:bar}'); 39 socket = new MockSocket.pair();
40 client = new WebSocketClientChannel(socket);
41 server = new WebSocketServerChannel(socket.twin);
35 42
36 expect(responsesReceived.length, equals(0)); 43 requestsReceived = new List();
37 expect(notificationsReceived.length, equals(0)); 44 responsesReceived = new List();
38 expect(mockSocket.responseCount, equals(0)); 45 notificationsReceived = new List();
Bob Nystrom 2014/03/04 01:03:45 new List() => []
danrubel 2014/03/05 15:32:04 Done.
46
47 // Allow multiple listeners on server side for testing
Bob Nystrom 2014/03/04 01:03:45 ".".
danrubel 2014/03/05 15:32:04 Done.
48 socket.twin.allowMultipleListeners();
49
50 server.listen((data) => requestsReceived.add(data));
Bob Nystrom 2014/03/04 01:03:45 server.listen(requestsReceived.add) Ditto below.
danrubel 2014/03/05 15:32:04 Nice! Done.
51 client.responseStream.listen((data) => responsesReceived.add(data));
52 client.notificationStream.listen((data) => notificationsReceived.add(data));
39 } 53 }
40 54
41 static void invalidJsonToServer() { 55 static Future invalidJsonToClient() {
42 InvalidJsonMockSocket mockSocket = new InvalidJsonMockSocket(); 56 socket.twin.add('{"foo":"bar"}');
43 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); 57 server.sendResponse(new Response('myId'));
44 var received = new List(); 58 return client.responseStream
45 server.listen((Request request) => received.add(request)); 59 .first
46 60 .timeout(new Duration(seconds: 1))
47 mockSocket.addInvalid('"blat"'); 61 .then((Response response) {
48 mockSocket.addInvalid('{foo:bar}'); 62 expect(response.id, equals('myId'));
49 63 expectMsgCount(0, 1, 0);
50 expect(received.length, equals(0)); 64 });
Bob Nystrom 2014/03/04 01:03:45 This might do the right thing because responseStre
danrubel 2014/03/05 15:32:04 The modified indentation makes it less obvious tha
nweiz 2014/03/05 20:16:39 With broadcast streams, it's important that you su
danrubel 2014/03/11 19:06:09 Good point. https://codereview.chromium.org/195463
51 expect(mockSocket.responseCount, equals(2));
52 } 65 }
53 66
54 static void notification() { 67 static Future invalidJsonToServer() {
55 MockSocket mockSocket = new MockSocket(); 68 socket.add('"blat"');
56 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); 69 return client.responseStream
57 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); 70 .first
58 var responsesReceived = new List(); 71 .timeout(new Duration(seconds: 1))
59 var notificationsReceived = new List(); 72 .then((Response response) {
60 client.listen((Response response) => responsesReceived.add(response), 73 expect(response.id, equals(''));
61 (Notification notification) => notificationsReceived.add(notification)); 74 expect(response.error, isNotNull);
75 expectMsgCount(0, 1, 0);
76 });
77 }
62 78
79 static Future notification() {
63 server.sendNotification(new Notification('myEvent')); 80 server.sendNotification(new Notification('myEvent'));
81 return client.notificationStream
82 .first
83 .timeout(new Duration(seconds: 1))
84 .then((Notification notification) {
85 expect(notification.event, equals('myEvent'));
86 expectMsgCount(0, 0, 1);
64 87
65 expect(responsesReceived.length, equals(0)); 88 expect(notificationsReceived.first, equals(notification));
66 expect(notificationsReceived.length, equals(1)); 89 });
67 expect(notificationsReceived.first.runtimeType, equals(Notification)); 90 }
68 Notification actual = notificationsReceived.first; 91
69 expect(actual.event, equals('myEvent')); 92 static Future notificationAndResponse() {
93 server
94 ..sendNotification(new Notification('myEvent'))
95 ..sendResponse(new Response('myId'));
96 return Future
97 .wait([
98 client.notificationStream.first,
99 client.responseStream.first])
100 .timeout(new Duration(seconds: 1))
101 .then((_) {expectMsgCount(0, 1, 1);});
Bob Nystrom 2014/03/04 01:03:45 Use => here or make it a multi-line method if you
danrubel 2014/03/05 15:32:04 Good point. Done.
70 } 102 }
71 103
72 static void request() { 104 static void request() {
73 MockSocket mockSocket = new MockSocket(); 105 client.sendRequest(new Request('myId', 'myMth'));
74 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); 106 server.listen((Request request) {
75 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); 107 expect(request.id, equals('myId'));
76 var requestsReceived = new List(); 108 expect(request.method, equals('myMth'));
77 server.listen((Request request) => requestsReceived.add(request)); 109 expectMsgCount(1, 0, 0);
78 110 });
79 client.sendRequest(new Request('myId', 'aMethod'));
80
81 expect(requestsReceived.length, equals(1));
82 expect(requestsReceived.first.runtimeType, equals(Request));
83 Request actual = requestsReceived.first;
84 expect(actual.id, equals('myId'));
85 expect(actual.method, equals('aMethod'));
86 } 111 }
87 112
88 static void response() { 113 static Future requestResponse() {
89 MockSocket mockSocket = new MockSocket(); 114 // Simulate server sending a response by echoing the request
90 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); 115 server.listen((Request request) =>
91 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); 116 server.sendResponse(new Response(request.id)));
92 var responsesReceived = new List(); 117 return client.sendRequest(new Request('myId', 'myMth'))
93 var notificationsReceived = new List(); 118 .timeout(new Duration(seconds: 1))
94 client.listen((Response response) => responsesReceived.add(response), 119 .then((Response response) {
95 (Notification notification) => notificationsReceived.add(notification)); 120 expect(response.id, equals('myId'));
121 expectMsgCount(1, 1, 0);
96 122
123 expect(requestsReceived.first.runtimeType, equals(Request));
Bob Nystrom 2014/03/04 01:03:45 Do: requestsReceived.first, new InstanceOf<Reques
danrubel 2014/03/05 15:32:04 Good point. Done. Sure would be nice if the match
Bob Nystrom 2014/03/05 21:01:03 It used to, but expect has optional named paramete
124 Request request = requestsReceived.first;
125 expect(request.id, equals('myId'));
126 expect(request.method, equals('myMth'));
127 expect(responsesReceived.first, equals(response));
128 });
129 }
130
131 static Future response() {
97 server.sendResponse(new Response('myId')); 132 server.sendResponse(new Response('myId'));
133 return client.responseStream
134 .first
135 .timeout(new Duration(seconds: 1))
136 .then((Response response) {
137 expect(response.id, equals('myId'));
138 expectMsgCount(0, 1, 0);
139 });
140 }
98 141
99 expect(responsesReceived.length, equals(1)); 142 static void expectMsgCount(requestCount, responseCount, notificationCount) {
Bob Nystrom 2014/03/04 01:03:45 I think the calls to this would be easier to read
danrubel 2014/03/05 15:32:04 I like it. Done.
100 expect(notificationsReceived.length, equals(0)); 143 expect(requestsReceived, hasLength(requestCount));
101 expect(responsesReceived.first.runtimeType, equals(Response)); 144 expect(responsesReceived, hasLength(responseCount));
102 Response actual = responsesReceived.first; 145 expect(notificationsReceived, hasLength(notificationCount));
103 expect(actual.id, equals('myId'));
104 } 146 }
105 } 147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698