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

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

Issue 1545483003: Dart: Reorganize files (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Fix build file Created 4 years, 12 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
« no previous file with comments | « mojo/dart/test/isolate_test.dart ('k') | mojo/dart/test/shared_buffer_finalizer_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
9 import 'package:mojo/core.dart' as core;
10
11 ByteData byteDataOfString(String s) {
12 return new ByteData.view((new Uint8List.fromList(s.codeUnits)).buffer);
13 }
14
15 String stringOfByteData(ByteData bytes) {
16 return new String.fromCharCodes(bytes.buffer.asUint8List().toList());
17 }
18
19 void expectStringFromEndpoint(
20 String expected, core.MojoMessagePipeEndpoint endpoint) {
21 // Query how many bytes are available.
22 var result = endpoint.query();
23 assert(result != null);
24 int size = result.bytesRead;
25 assert(size > 0);
26
27 // Read the data.
28 ByteData bytes = new ByteData(size);
29 result = endpoint.read(bytes);
30 assert(result != null);
31 assert(size == result.bytesRead);
32
33 // Convert to a string and check.
34 String msg = stringOfByteData(bytes);
35 assert(expected == msg);
36 }
37
38 void pipeTestIsolate(core.MojoMessagePipeEndpoint endpoint) {
39 var eventSubscription = new core.MojoEventSubscription(endpoint.handle);
40 eventSubscription.subscribe((int mojoSignals) {
41 if (core.MojoHandleSignals.isReadWrite(mojoSignals)) {
42 throw 'We should only be reading or writing, not both.';
43 } else if (core.MojoHandleSignals.isReadable(mojoSignals)) {
44 expectStringFromEndpoint("Ping", endpoint);
45 eventSubscription.enableWriteEvents();
46 } else if (core.MojoHandleSignals.isWritable(mojoSignals)) {
47 endpoint.write(byteDataOfString("Pong"));
48 eventSubscription.enableReadEvents();
49 } else if (core.MojoHandleSignals.isPeerClosed(mojoSignals)) {
50 eventSubscription.close();
51 } else {
52 throw 'Unexpected event.';
53 }
54 });
55 }
56
57 main() {
58 var pipe = new core.MojoMessagePipe();
59 var endpoint = pipe.endpoints[0];
60 var eventSubscription = new core.MojoEventSubscription(endpoint.handle);
61 Isolate.spawn(pipeTestIsolate, pipe.endpoints[1]).then((_) {
62 eventSubscription.subscribe((int mojoSignals) {
63 if (core.MojoHandleSignals.isReadWrite(mojoSignals)) {
64 throw 'We should only be reading or writing, not both.';
65 } else if (core.MojoHandleSignals.isReadable(mojoSignals)) {
66 expectStringFromEndpoint("Pong", endpoint);
67 eventSubscription.close();
68 } else if (core.MojoHandleSignals.isWritable(mojoSignals)) {
69 endpoint.write(byteDataOfString("Ping"));
70 eventSubscription.enableReadEvents();
71 } else if (core.MojoHandleSignals.isPeerClosed(mojoSignals)) {
72 throw 'This end should close first.';
73 } else {
74 throw 'Unexpected event.';
75 }
76 });
77 eventSubscription.enableWriteEvents();
78 });
79 }
OLDNEW
« no previous file with comments | « mojo/dart/test/isolate_test.dart ('k') | mojo/dart/test/shared_buffer_finalizer_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698