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

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: 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';
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);
18 test('request', ChannelTest.request); 22 test('request', ChannelTest.request);
23 test('requestResponse', ChannelTest.requestResponse);
19 test('response', ChannelTest.response); 24 test('response', ChannelTest.response);
20 }); 25 });
21 } 26 }
22 27
23 class ChannelTest { 28 class ChannelTest {
29 static MockSocket socket;
30 static WebSocketClientChannel client;
31 static WebSocketServerChannel server;
24 32
25 static void invalidJsonToClient() { 33 static List requestsReceived;
26 InvalidJsonMockSocket mockSocket = new InvalidJsonMockSocket(); 34 static List responsesReceived;
27 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); 35 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 36
33 mockSocket.addInvalid('"blat"'); 37 static void setUp() {
34 mockSocket.addInvalid('{foo:bar}'); 38 socket = new MockSocket.pair();
39 client = new WebSocketClientChannel(socket);
40 server = new WebSocketServerChannel(socket.twin);
35 41
36 expect(responsesReceived.length, equals(0)); 42 requestsReceived = new List();
37 expect(notificationsReceived.length, equals(0)); 43 responsesReceived = new List();
38 expect(mockSocket.responseCount, equals(0)); 44 notificationsReceived = new List();
45
46 server.listen((data) => requestsReceived.add(data));
47 client.responseStream.listen((data) => responsesReceived.add(data));
48 client.notificationStream.listen((data) => notificationsReceived.add(data));
39 } 49 }
40 50
41 static void invalidJsonToServer() { 51 static Future invalidJsonToClient() {
42 InvalidJsonMockSocket mockSocket = new InvalidJsonMockSocket(); 52 socket.twin.add('{"foo":"bar"}');
43 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); 53 server.sendResponse(new Response('myId'));
44 var received = new List(); 54 return client.responseStream
45 server.listen((Request request) => received.add(request)); 55 .first
46 56 .timeout(new Duration(seconds: 1))
47 mockSocket.addInvalid('"blat"'); 57 .then((Response response) {
48 mockSocket.addInvalid('{foo:bar}'); 58 expect(response.id, equals('myId'));
49 59 expectMsgCount(0, 1, 0);
50 expect(received.length, equals(0)); 60 });
51 expect(mockSocket.responseCount, equals(2));
52 } 61 }
53 62
54 static void notification() { 63 static Future invalidJsonToServer() {
55 MockSocket mockSocket = new MockSocket(); 64 socket.add('"blat"');
56 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); 65 return client.responseStream
57 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); 66 .first
58 var responsesReceived = new List(); 67 .timeout(new Duration(seconds: 1))
59 var notificationsReceived = new List(); 68 .then((Response response) {
60 client.listen((Response response) => responsesReceived.add(response), 69 expect(response.id, equals(''));
61 (Notification notification) => notificationsReceived.add(notification)); 70 expect(response.error, isNotNull);
71 expectMsgCount(0, 1, 0);
72 });
73 }
62 74
75 static Future notification() {
63 server.sendNotification(new Notification('myEvent')); 76 server.sendNotification(new Notification('myEvent'));
77 return client.notificationStream
78 .first
79 .timeout(new Duration(seconds: 1))
80 .then((Notification notification) {
81 expect(notification.event, equals('myEvent'));
82 expectMsgCount(0, 0, 1);
64 83
65 expect(responsesReceived.length, equals(0)); 84 expect(notificationsReceived.first, equals(notification));
66 expect(notificationsReceived.length, equals(1)); 85 });
67 expect(notificationsReceived.first.runtimeType, equals(Notification));
68 Notification actual = notificationsReceived.first;
69 expect(actual.event, equals('myEvent'));
70 } 86 }
71 87
72 static void request() { 88 static void request() {
73 MockSocket mockSocket = new MockSocket(); 89 client.sendRequest(new Request('myId', 'myMth'));
74 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); 90 server.listen((Request request) {
75 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); 91 expect(request.id, equals('myId'));
76 var requestsReceived = new List(); 92 expect(request.method, equals('myMth'));
77 server.listen((Request request) => requestsReceived.add(request)); 93 expectMsgCount(1, 0, 0);
78 94 });
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 } 95 }
87 96
88 static void response() { 97 static Future requestResponse() {
89 MockSocket mockSocket = new MockSocket(); 98 // Simulate server sending a response by echoing the request
90 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); 99 server.listen((Request request) =>
91 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); 100 server.sendResponse(new Response(request.id)));
92 var responsesReceived = new List(); 101 return client.sendRequest(new Request('myId', 'myMth'))
93 var notificationsReceived = new List(); 102 .timeout(new Duration(seconds: 1))
94 client.listen((Response response) => responsesReceived.add(response), 103 .then((Response response) {
95 (Notification notification) => notificationsReceived.add(notification)); 104 expect(response.id, equals('myId'));
105 expectMsgCount(1, 1, 0);
96 106
107 expect(requestsReceived.first.runtimeType, equals(Request));
108 Request request = requestsReceived.first;
109 expect(request.id, equals('myId'));
110 expect(request.method, equals('myMth'));
111 expect(responsesReceived.first, equals(response));
112 });
113 }
114
115 static Future response() {
97 server.sendResponse(new Response('myId')); 116 server.sendResponse(new Response('myId'));
117 return client.responseStream
118 .first
119 .timeout(new Duration(seconds: 1))
120 .then((Response response) {
121 expect(response.id, equals('myId'));
122 expectMsgCount(0, 1, 0);
123 });
124 }
98 125
99 expect(responsesReceived.length, equals(1)); 126 static void expectMsgCount(requestCount, responseCount, notificationCount) {
100 expect(notificationsReceived.length, equals(0)); 127 expect(requestsReceived, hasLength(requestCount));
101 expect(responsesReceived.first.runtimeType, equals(Response)); 128 expect(responsesReceived, hasLength(responseCount));
102 Response actual = responsesReceived.first; 129 expect(notificationsReceived, hasLength(notificationCount));
103 expect(actual.id, equals('myId'));
104 } 130 }
105 } 131 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698