OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 import 'dart:async'; |
| 6 import 'dart:isolate'; |
| 7 |
| 8 import 'package:analyzer_plugin/protocol/generated_protocol.dart'; |
| 9 import 'package:analyzer_plugin/protocol/protocol.dart'; |
| 10 import 'package:analyzer_plugin/src/channel/isolate_channel.dart'; |
| 11 import 'package:test/test.dart'; |
| 12 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 13 |
| 14 void main() { |
| 15 defineReflectiveTests(IsolateChannelTest); |
| 16 } |
| 17 |
| 18 @reflectiveTest |
| 19 class IsolateChannelTest { |
| 20 TestSendPort sendPort; |
| 21 IsolateChannel channel; |
| 22 |
| 23 void setUp() { |
| 24 sendPort = new TestSendPort(); |
| 25 channel = new IsolateChannel(sendPort); |
| 26 } |
| 27 |
| 28 void tearDown() { |
| 29 // If the test doesn't listen to the channel, then close will not cancel the |
| 30 // subscription and the process will not terminate. |
| 31 try { |
| 32 channel.listen((request) {}); |
| 33 } catch (exception) { |
| 34 // Ignore the exception if the test has already registered a listener. |
| 35 } |
| 36 channel.close(); |
| 37 } |
| 38 |
| 39 @failingTest |
| 40 Future<Null> test_close() async { |
| 41 bool done = false; |
| 42 channel.listen((Request request) {}, onDone: () { |
| 43 done = true; |
| 44 }); |
| 45 channel.close(); |
| 46 // TODO(brianwilkerson) Figure out how to wait until the handler has been |
| 47 // called. |
| 48 await _pumpEventQueue(); |
| 49 expect(done, isTrue); |
| 50 } |
| 51 |
| 52 Future<Null> test_listen() async { |
| 53 Request sentRequest = new PluginShutdownParams().toRequest('5'); |
| 54 Request receivedRequest; |
| 55 channel.listen((Request request) { |
| 56 receivedRequest = request; |
| 57 }); |
| 58 sendPort.receivePort.send(sentRequest.toJson()); |
| 59 await _pumpEventQueue(1); |
| 60 expect(receivedRequest, sentRequest); |
| 61 } |
| 62 |
| 63 void test_sendNotification() { |
| 64 Notification notification = |
| 65 new PluginErrorParams(false, '', '').toNotification(); |
| 66 channel.sendNotification(notification); |
| 67 expect(sendPort.sentMessages, hasLength(1)); |
| 68 expect(sendPort.sentMessages[0], notification.toJson()); |
| 69 } |
| 70 |
| 71 void test_sendResponse() { |
| 72 Response response = new PluginShutdownResult().toResponse('3'); |
| 73 channel.sendResponse(response); |
| 74 expect(sendPort.sentMessages, hasLength(1)); |
| 75 expect(sendPort.sentMessages[0], response.toJson()); |
| 76 } |
| 77 |
| 78 /** |
| 79 * Returns a [Future] that completes after pumping the event queue [times] |
| 80 * times. By default, this should pump the event queue enough times to allow |
| 81 * any code to run, as long as it's not waiting on some external event. |
| 82 */ |
| 83 Future<Null> _pumpEventQueue([int times = 5000]) { |
| 84 if (times == 0) return new Future.value(); |
| 85 // We use a delayed future to allow microtask events to finish. The |
| 86 // Future.value or Future() constructors use scheduleMicrotask themselves an
d |
| 87 // would therefore not wait for microtask callbacks that are scheduled after |
| 88 // invoking this method. |
| 89 return new Future.delayed(Duration.ZERO, () => _pumpEventQueue(times - 1)); |
| 90 } |
| 91 } |
| 92 |
| 93 /** |
| 94 * A send port used in tests. |
| 95 */ |
| 96 class TestSendPort implements SendPort { |
| 97 /** |
| 98 * The receive port used to receive messages from the server. |
| 99 */ |
| 100 SendPort receivePort; |
| 101 |
| 102 /** |
| 103 * The messages sent to the server. |
| 104 */ |
| 105 List<Object> sentMessages = <Object>[]; |
| 106 |
| 107 @override |
| 108 void send(message) { |
| 109 if (receivePort == null) { |
| 110 if (message is SendPort) { |
| 111 receivePort = message; |
| 112 } else { |
| 113 fail('Did not receive a receive port as the first communication.'); |
| 114 } |
| 115 } else { |
| 116 sentMessages.add(message); |
| 117 } |
| 118 } |
| 119 } |
OLD | NEW |