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

Side by Side Diff: mojo/dart/test/handle_watcher_test.dart

Issue 800523004: Dart: Simplifies the handle watcher. Various cleanups and bugfixes. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 import 'dart:async'; 5 import 'dart:async';
6 import 'dart:isolate'; 6 import 'dart:isolate';
7 import 'dart:mojo_core'; 7 import 'dart:mojo_core';
8 import 'dart:typed_data'; 8 import 'dart:typed_data';
9 9
10 import 'package:mojo/dart/testing/expect.dart'; 10 import 'package:mojo/dart/testing/expect.dart';
11 11
12 void simpleTest() { 12 void simpleTest() {
13 var pipe = new MojoMessagePipe(); 13 var pipe = new MojoMessagePipe();
14 Expect.isNotNull(pipe); 14 Expect.isNotNull(pipe);
15 15
16 var endpoint = pipe.endpoints[0]; 16 var endpoint = pipe.endpoints[0];
17 Expect.isTrue(endpoint.handle.isValid); 17 Expect.isTrue(endpoint.handle.isValid);
18 18
19 var handle = new MojoHandle(endpoint.handle); 19 var handle = new MojoHandle(endpoint.handle);
20 var completer = new Completer(); 20 var completer = new Completer();
21 int numEvents = 0; 21 int numEvents = 0;
22 22
23 handle.enableWriteEvents();
24 handle.listen((_) { 23 handle.listen((_) {
25 numEvents++; 24 numEvents++;
26 handle.close(); 25 handle.close();
27 }, onDone: () { 26 }, onDone: () {
28 completer.complete(numEvents); 27 completer.complete(numEvents);
29 }); 28 });
29 handle.enableWriteEvents();
siva 2014/12/29 23:20:44 Why does this have to be moved down?
zra 2014/12/30 16:29:33 It doesn't have to be. enableWriteEvents() can go
30 30
31 completer.future.then((int numEvents) { 31 completer.future.then((int numEvents) {
32 Expect.equals(1, numEvents); 32 Expect.equals(1, numEvents);
33 }); 33 });
34 } 34 }
35 35
36 36
37 Future simpleAsyncAwaitTest() async { 37 Future simpleAsyncAwaitTest() async {
38 var pipe = new MojoMessagePipe(); 38 var pipe = new MojoMessagePipe();
39 Expect.isNotNull(pipe); 39 Expect.isNotNull(pipe);
40 40
41 var endpoint = pipe.endpoints[0]; 41 var endpoint = pipe.endpoints[0];
42 Expect.isTrue(endpoint.handle.isValid); 42 Expect.isTrue(endpoint.handle.isValid);
43 43
44 var handle = new MojoHandle(endpoint.handle); 44 var handle = new MojoHandle(endpoint.handle, MojoHandleSignals.READWRITE);
45 45
46 int numEvents = 0; 46 int numEvents = 0;
47 handle.enableWriteEvents(); 47 await for (List<int> event in handle) {
48 await for (var signal in handle) {
49 numEvents++; 48 numEvents++;
50 handle.close(); 49 handle.close();
51 } 50 }
52 Expect.equals(1, numEvents); 51 Expect.equals(1, numEvents);
53 } 52 }
54 53
55 54
56 ByteData byteDataOfString(String s) { 55 ByteData byteDataOfString(String s) {
57 return new ByteData.view((new Uint8List.fromList(s.codeUnits)).buffer); 56 return new ByteData.view((new Uint8List.fromList(s.codeUnits)).buffer);
58 } 57 }
(...skipping 21 matching lines...) Expand all
80 // Convert to a string and check. 79 // Convert to a string and check.
81 String msg = stringOfByteData(bytes); 80 String msg = stringOfByteData(bytes);
82 Expect.equals(expected, msg); 81 Expect.equals(expected, msg);
83 } 82 }
84 83
85 84
86 Future pingPongIsolate(MojoMessagePipeEndpoint endpoint) async { 85 Future pingPongIsolate(MojoMessagePipeEndpoint endpoint) async {
87 int pings = 0; 86 int pings = 0;
88 int pongs = 0; 87 int pongs = 0;
89 var handle = new MojoHandle(endpoint.handle); 88 var handle = new MojoHandle(endpoint.handle);
90 await for (var signal in handle) { 89 await for (List<int> event in handle) {
91 if (MojoHandleSignals.isReadWrite(signal)) { 90 var mojoSignals = new MojoHandleSignals(event[1]);
91 if (mojoSignals.isReadWrite) {
92 // We are either sending or receiving. 92 // We are either sending or receiving.
93 throw new Exception("Unexpected signal"); 93 throw new Exception("Unexpected event");
94 } else if (MojoHandleSignals.isReadable(signal)) { 94 } else if (mojoSignals.isReadable) {
95 expectStringFromEndpoint("Ping", endpoint); 95 expectStringFromEndpoint("Ping", endpoint);
96 pings++; 96 pings++;
97 handle.enableWriteEvents(); 97 handle.enableWriteEvents();
98 } else if (MojoHandleSignals.isWritable(signal)) { 98 } else if (mojoSignals.isWritable) {
99 endpoint.write(byteDataOfString("Pong")); 99 endpoint.write(byteDataOfString("Pong"));
100 pongs++; 100 pongs++;
101 handle.disableWriteEvents(); 101 handle.enableReadEvents();
102 } 102 }
103 } 103 }
104 handle.close(); 104 handle.close();
105 Expect.equals(10, pings); 105 Expect.equals(10, pings);
106 Expect.equals(10, pongs); 106 Expect.equals(10, pongs);
107 } 107 }
108 108
109 109
110 Future pingPongTest() async { 110 Future pingPongTest() async {
111 var pipe = new MojoMessagePipe(); 111 var pipe = new MojoMessagePipe();
112 var isolate = await Isolate.spawn(pingPongIsolate, pipe.endpoints[0]); 112 var isolate = await Isolate.spawn(pingPongIsolate, pipe.endpoints[0]);
113 var endpoint = pipe.endpoints[1]; 113 var endpoint = pipe.endpoints[1];
114 var handle = new MojoHandle(endpoint.handle); 114 var handle = new MojoHandle(endpoint.handle, MojoHandleSignals.READWRITE);
115 115
116 int pings = 0; 116 int pings = 0;
117 int pongs = 0; 117 int pongs = 0;
118 handle.enableWriteEvents(); // This side will send first. 118 await for (List<int> event in handle) {
119 await for (var signal in handle) { 119 var mojoSignals = new MojoHandleSignals(event[1]);
120 if (MojoHandleSignals.isReadWrite(signal)) { 120 if (mojoSignals.isReadWrite) {
121 // We are either sending or receiving. 121 // We are either sending or receiving.
122 throw new Exception("Unexpected signal"); 122 throw new Exception("Unexpected event");
123 } else if (MojoHandleSignals.isReadable(signal)) { 123 } else if (mojoSignals.isReadable) {
124 expectStringFromEndpoint("Pong", endpoint); 124 expectStringFromEndpoint("Pong", endpoint);
125 pongs++; 125 pongs++;
126 if (pongs == 10) { 126 if (pongs == 10) {
127 handle.close(); 127 handle.close();
128 } 128 }
129 handle.enableWriteEvents(); // Now it is our turn to send. 129 handle.enableWriteEvents(); // Now it is our turn to send.
130 } else if (MojoHandleSignals.isWritable(signal)) { 130 } else if (mojoSignals.isWritable) {
131 if (pings < 10) { 131 if (pings < 10) {
132 endpoint.write(byteDataOfString("Ping")); 132 endpoint.write(byteDataOfString("Ping"));
133 pings++; 133 pings++;
134 } 134 }
135 handle.disableWriteEvents(); // Don't send while waiting for reply. 135 handle.enableReadEvents(); // Don't send while waiting for reply.
136 } 136 }
137 } 137 }
138 Expect.equals(10, pings); 138 Expect.equals(10, pings);
139 Expect.equals(10, pongs); 139 Expect.equals(10, pongs);
140 } 140 }
141 141
142 142
143 main() async { 143 main() async {
144 simpleTest(); 144 simpleTest();
145 await simpleAsyncAwaitTest(); 145 await simpleAsyncAwaitTest();
146 await pingPongTest(); 146 await pingPongTest();
147 } 147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698