OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 import 'dart:async'; | |
6 import 'dart:isolate'; | |
7 import 'dart:typed_data'; | |
8 import 'dart:mojo.bindings' as bindings; | |
9 import 'dart:mojo.core' as core; | |
10 | |
11 import 'package:mojo/dart/testing/expect.dart'; | |
12 | |
13 const int kEchoesCount = 100; | |
14 | |
15 class EchoString extends bindings.Struct { | |
16 static const int kStructSize = 16; | |
17 static const bindings.StructDataHeader kDefaultStructInfo = | |
18 const bindings.StructDataHeader(kStructSize, 1); | |
19 String a = null; | |
20 | |
21 EchoString() : super(kStructSize); | |
22 | |
23 static EchoString deserialize(bindings.Message message) { | |
24 return decode(new bindings.Decoder(message)); | |
25 } | |
26 | |
27 static EchoString decode(bindings.Decoder decoder0) { | |
28 if (decoder0 == null) { | |
29 return null; | |
30 } | |
31 EchoString result = new EchoString(); | |
32 var mainDataHeader = decoder0.decodeStructDataHeader(); | |
33 if (mainDataHeader.version > 0) { | |
34 result.a = decoder0.decodeString(8, false); | |
35 } | |
36 return result; | |
37 } | |
38 | |
39 void encode(bindings.Encoder encoder) { | |
40 var encoder0 = encoder.getStructEncoderAtOffset(kDefaultStructInfo); | |
41 encoder0.encodeString(a, 8, false); | |
42 } | |
43 } | |
44 | |
45 class EchoStringResponse extends bindings.Struct { | |
46 static const int kStructSize = 16; | |
47 static const bindings.StructDataHeader kDefaultStructInfo = | |
48 const bindings.StructDataHeader(kStructSize, 1); | |
49 String a = null; | |
50 | |
51 EchoStringResponse() : super(kStructSize); | |
52 | |
53 static EchoStringResponse deserialize(bindings.Message message) { | |
54 return decode(new bindings.Decoder(message)); | |
55 } | |
56 | |
57 static EchoStringResponse decode(bindings.Decoder decoder0) { | |
58 if (decoder0 == null) { | |
59 return null; | |
60 } | |
61 EchoStringResponse result = new EchoStringResponse(); | |
62 var mainDataHeader = decoder0.decodeStructDataHeader(); | |
63 if (mainDataHeader.version > 0) { | |
64 result.a = decoder0.decodeString(8, false); | |
65 } | |
66 return result; | |
67 } | |
68 | |
69 void encode(bindings.Encoder encoder) { | |
70 var encoder0 = encoder.getStructEncoderAtOffset(kDefaultStructInfo); | |
71 encoder0.encodeString(a, 8, false); | |
72 } | |
73 } | |
74 | |
75 const int kEchoString_name = 0; | |
76 const int kEchoStringResponse_name = 1; | |
77 | |
78 class EchoStub extends bindings.Stub { | |
79 EchoStub(core.MojoMessagePipeEndpoint endpoint) | |
80 : super.fromEndpoint(endpoint); | |
81 | |
82 Future<EchoStringResponse> echoString(EchoString es) { | |
83 var response = new EchoStringResponse(); | |
84 response.a = es.a; | |
85 return new Future.value(response); | |
86 } | |
87 | |
88 Future<bindings.Message> handleMessage(bindings.ServiceMessage message) { | |
89 switch (message.header.type) { | |
90 case kEchoString_name: | |
91 var es = EchoString.deserialize(message.payload); | |
92 return echoString(es).then((response) { | |
93 if (response != null) { | |
94 return buildResponseWithId(response, kEchoStringResponse_name, | |
95 message.header.requestId, | |
96 bindings.MessageHeader.kMessageIsResponse); | |
97 } | |
98 }); | |
99 break; | |
100 default: | |
101 throw new Exception("Unexpected case"); | |
102 break; | |
103 } | |
104 return null; | |
105 } | |
106 } | |
107 | |
108 class EchoProxy extends bindings.Proxy { | |
109 EchoProxy(core.MojoMessagePipeEndpoint endpoint) | |
110 : super.fromEndpoint(endpoint); | |
111 | |
112 Future<EchoStringResponse> echoString(String a) { | |
113 // compose message. | |
114 var es = new EchoString(); | |
115 es.a = a; | |
116 return sendMessageWithRequestId(es, kEchoString_name, -1, | |
117 bindings.MessageHeader.kMessageExpectsResponse); | |
118 } | |
119 | |
120 void handleResponse(bindings.ServiceMessage message) { | |
121 switch (message.header.type) { | |
122 case kEchoStringResponse_name: | |
123 var esr = EchoStringResponse.deserialize(message.payload); | |
124 Completer c = completerMap[message.header.requestId]; | |
125 completerMap[message.header.requestId] = null; | |
126 c.complete(esr); | |
127 break; | |
128 default: | |
129 throw new Exception("Unexpected case"); | |
130 break; | |
131 } | |
132 } | |
133 } | |
134 | |
135 void providerIsolate(core.MojoMessagePipeEndpoint endpoint) { | |
136 new EchoStub(endpoint); | |
137 } | |
138 | |
139 Future<int> runTest() async { | |
140 var testCompleter = new Completer(); | |
141 | |
142 var pipe = new core.MojoMessagePipe(); | |
143 var proxy = new EchoProxy(pipe.endpoints[0]); | |
144 await Isolate.spawn(providerIsolate, pipe.endpoints[1]); | |
145 | |
146 int n = kEchoesCount; | |
147 int count = 0; | |
148 for (int i = 0; i < n; i++) { | |
149 proxy.echoString("hello").then((response) { | |
150 Expect.equals("hello", response.a); | |
151 count++; | |
152 if (i == (n - 1)) { | |
153 proxy.close(); | |
154 testCompleter.complete(count); | |
155 } | |
156 }); | |
157 } | |
158 | |
159 return testCompleter.future; | |
160 } | |
161 | |
162 Future runAwaitTest() async { | |
163 var pipe = new core.MojoMessagePipe(); | |
164 var proxy = new EchoProxy(pipe.endpoints[0]); | |
165 await Isolate.spawn(providerIsolate, pipe.endpoints[1]); | |
166 | |
167 int n = kEchoesCount; | |
168 for (int i = 0; i < n; i++) { | |
169 var response = await proxy.echoString("Hello"); | |
170 Expect.equals("Hello", response.a); | |
171 } | |
172 proxy.close(); | |
173 } | |
174 | |
175 void closingProviderIsolate(core.MojoMessagePipeEndpoint endpoint) { | |
176 var provider = new EchoStub(endpoint); | |
177 provider.close(); | |
178 } | |
179 | |
180 Future<bool> runOnClosedTest() async { | |
181 var testCompleter = new Completer(); | |
182 | |
183 var pipe = new core.MojoMessagePipe(); | |
184 var proxy = new EchoProxy(pipe.endpoints[0]); | |
185 proxy.onError = () => testCompleter.complete(true); | |
186 await Isolate.spawn(closingProviderIsolate, pipe.endpoints[1]); | |
187 return testCompleter.future.timeout(new Duration(seconds: 1), | |
188 onTimeout: () => false); | |
189 } | |
190 | |
191 main() async { | |
192 Expect.equals(kEchoesCount, await runTest()); | |
193 await runAwaitTest(); | |
194 Expect.isTrue(await runOnClosedTest()); | |
195 } | |
OLD | NEW |