OLD | NEW |
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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
| 7 import 'package:stream_channel/stream_channel.dart'; |
7 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
| 9 |
8 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; | 10 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; |
9 | 11 |
10 import 'utils.dart'; | 12 import 'utils.dart'; |
11 | 13 |
12 void main() { | 14 void main() { |
| 15 var responseController; |
| 16 var requestController; |
| 17 var client; |
| 18 setUp(() { |
| 19 responseController = new StreamController(); |
| 20 requestController = new StreamController(); |
| 21 client = new json_rpc.Client.withoutJson( |
| 22 new StreamChannel(responseController.stream, requestController.sink)); |
| 23 }); |
| 24 |
13 test(".withoutJson supports decoded stream and sink", () { | 25 test(".withoutJson supports decoded stream and sink", () { |
14 var responseController = new StreamController(); | |
15 var requestController = new StreamController(); | |
16 var client = new json_rpc.Client.withoutJson( | |
17 responseController.stream, requestController.sink); | |
18 client.listen(); | 26 client.listen(); |
19 | 27 |
20 expect(requestController.stream.first.then((request) { | 28 expect(requestController.stream.first.then((request) { |
21 expect(request, allOf([ | 29 expect(request, allOf([ |
22 containsPair('jsonrpc', '2.0'), | 30 containsPair('jsonrpc', '2.0'), |
23 containsPair('method', 'foo') | 31 containsPair('method', 'foo') |
24 ])); | 32 ])); |
25 | 33 |
26 responseController.add({ | 34 responseController.add({ |
27 'jsonrpc': '2.0', | 35 'jsonrpc': '2.0', |
28 'result': 'bar', | 36 'result': 'bar', |
29 'id': request['id'] | 37 'id': request['id'] |
30 }); | 38 }); |
31 }), completes); | 39 }), completes); |
32 | 40 |
33 client.sendRequest('foo'); | 41 client.sendRequest('foo'); |
34 }); | 42 }); |
35 | 43 |
36 test(".listen returns when the controller is closed", () { | 44 test(".listen returns when the controller is closed", () { |
37 var responseController = new StreamController(); | |
38 var requestController = new StreamController(); | |
39 var client = new json_rpc.Client.withoutJson( | |
40 responseController.stream, requestController.sink); | |
41 | |
42 var hasListenCompeted = false; | 45 var hasListenCompeted = false; |
43 expect(client.listen().then((_) => hasListenCompeted = true), completes); | 46 expect(client.listen().then((_) => hasListenCompeted = true), completes); |
44 | 47 |
45 return pumpEventQueue().then((_) { | 48 return pumpEventQueue().then((_) { |
46 expect(hasListenCompeted, isFalse); | 49 expect(hasListenCompeted, isFalse); |
47 | 50 |
48 // This should cause listen to complete. | 51 // This should cause listen to complete. |
49 return responseController.close(); | 52 return responseController.close(); |
50 }); | 53 }); |
51 }); | 54 }); |
52 | 55 |
53 test(".listen returns a stream error", () { | 56 test(".listen returns a stream error", () { |
54 var responseController = new StreamController(); | |
55 var requestController = new StreamController(); | |
56 var client = new json_rpc.Client( | |
57 responseController.stream, requestController.sink); | |
58 | |
59 expect(client.listen(), throwsA('oh no')); | 57 expect(client.listen(), throwsA('oh no')); |
60 responseController.addError('oh no'); | 58 responseController.addError('oh no'); |
61 }); | 59 }); |
62 | 60 |
63 test(".listen can't be called twice", () { | 61 test(".listen can't be called twice", () { |
64 var responseController = new StreamController(); | |
65 var requestController = new StreamController(); | |
66 var client = new json_rpc.Client( | |
67 responseController.stream, requestController.sink); | |
68 client.listen(); | 62 client.listen(); |
69 | |
70 expect(() => client.listen(), throwsStateError); | 63 expect(() => client.listen(), throwsStateError); |
71 }); | 64 }); |
72 | 65 |
73 test(".close cancels the stream subscription and closes the sink", () { | 66 test(".close cancels the stream subscription and closes the sink", () { |
74 var responseController = new StreamController(); | 67 // Work around sdk#19095. |
75 var requestController = new StreamController(); | 68 requestController.stream.listen(null); |
76 var client = new json_rpc.Client( | |
77 responseController.stream, requestController.sink); | |
78 | 69 |
79 expect(client.listen(), completes); | 70 expect(client.listen(), completes); |
80 | 71 |
81 expect(client.isClosed, isFalse); | 72 expect(client.isClosed, isFalse); |
82 expect(client.close(), completes); | 73 expect(client.close(), completes); |
83 expect(client.isClosed, isTrue); | 74 expect(client.isClosed, isTrue); |
84 | 75 |
85 expect(() => responseController.stream.listen((_) {}), throwsStateError); | 76 expect(() => responseController.stream.listen((_) {}), throwsStateError); |
86 expect(requestController.isClosed, isTrue); | 77 expect(requestController.isClosed, isTrue); |
87 }); | 78 }); |
88 | |
89 test(".close can't be called before .listen", () { | |
90 var responseController = new StreamController(); | |
91 var requestController = new StreamController(); | |
92 var client = new json_rpc.Client( | |
93 responseController.stream, requestController.sink); | |
94 | |
95 expect(() => client.close(), throwsStateError); | |
96 }); | |
97 } | 79 } |
OLD | NEW |