| OLD | NEW |
| 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:mojo_core'; | 5 import 'dart:mojo_core'; |
| 6 import 'dart:typed_data'; | 6 import 'dart:typed_data'; |
| 7 | 7 |
| 8 import 'package:mojo/dart/testing/expect.dart'; | 8 import 'package:mojo/dart/testing/expect.dart'; |
| 9 | 9 |
| 10 | 10 |
| 11 invalidHandleTest() { | 11 invalidHandleTest() { |
| 12 RawMojoHandle invalidHandle = new RawMojoHandle(RawMojoHandle.INVALID); | 12 RawMojoHandle invalidHandle = new RawMojoHandle(RawMojoHandle.INVALID); |
| 13 | 13 |
| 14 // Close. | 14 // Close. |
| 15 MojoResult result = invalidHandle.close(); | 15 MojoResult result = invalidHandle.close(); |
| 16 Expect.isTrue(result.isInvalidArgument); | 16 Expect.isTrue(result.isInvalidArgument); |
| 17 | 17 |
| 18 // Wait. | 18 // Wait. |
| 19 result = invalidHandle.wait(MojoHandleSignals.READWRITE, 1000000); | 19 MojoWaitResult mwr = invalidHandle.wait(MojoHandleSignals.READWRITE, 1000000); |
| 20 Expect.isTrue(result.isInvalidArgument); | 20 Expect.isTrue(mwr.result.isInvalidArgument); |
| 21 | 21 |
| 22 int res = RawMojoHandle.waitMany([invalidHandle.h], | 22 MojoWaitManyResult mwmr = RawMojoHandle.waitMany( |
| 23 [MojoHandleSignals.READWRITE], | 23 [invalidHandle.h], |
| 24 RawMojoHandle.DEADLINE_INDEFINITE); | 24 [MojoHandleSignals.READWRITE], |
| 25 Expect.equals(res, MojoResult.kInvalidArgument); | 25 RawMojoHandle.DEADLINE_INDEFINITE); |
| 26 Expect.isTrue(mwmr.result.isInvalidArgument); |
| 26 | 27 |
| 27 // Message pipe. | 28 // Message pipe. |
| 28 MojoMessagePipe pipe = new MojoMessagePipe(); | 29 MojoMessagePipe pipe = new MojoMessagePipe(); |
| 29 Expect.isNotNull(pipe); | 30 Expect.isNotNull(pipe); |
| 30 ByteData bd = new ByteData(10); | 31 ByteData bd = new ByteData(10); |
| 31 pipe.endpoints[0].handle.close(); | 32 pipe.endpoints[0].handle.close(); |
| 32 pipe.endpoints[1].handle.close(); | 33 pipe.endpoints[1].handle.close(); |
| 33 result = pipe.endpoints[0].write(bd); | 34 result = pipe.endpoints[0].write(bd); |
| 34 Expect.isTrue(result.isInvalidArgument); | 35 Expect.isTrue(result.isInvalidArgument); |
| 35 | 36 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 Expect.isNotNull(pipe); | 81 Expect.isNotNull(pipe); |
| 81 Expect.isTrue(pipe.status.isOk); | 82 Expect.isTrue(pipe.status.isOk); |
| 82 Expect.isNotNull(pipe.endpoints); | 83 Expect.isNotNull(pipe.endpoints); |
| 83 | 84 |
| 84 MojoMessagePipeEndpoint end0 = pipe.endpoints[0]; | 85 MojoMessagePipeEndpoint end0 = pipe.endpoints[0]; |
| 85 MojoMessagePipeEndpoint end1 = pipe.endpoints[1]; | 86 MojoMessagePipeEndpoint end1 = pipe.endpoints[1]; |
| 86 Expect.isTrue(end0.handle.isValid); | 87 Expect.isTrue(end0.handle.isValid); |
| 87 Expect.isTrue(end1.handle.isValid); | 88 Expect.isTrue(end1.handle.isValid); |
| 88 | 89 |
| 89 // Not readable, yet. | 90 // Not readable, yet. |
| 90 MojoResult result = end0.handle.wait(MojoHandleSignals.READABLE, 0); | 91 MojoWaitResult mwr = end0.handle.wait(MojoHandleSignals.READABLE, 0); |
| 91 Expect.isTrue(result.isDeadlineExceeded); | 92 Expect.isTrue(mwr.result.isDeadlineExceeded); |
| 92 | 93 |
| 93 // Should be writable. | 94 // Should be writable. |
| 94 result = end0.handle.wait(MojoHandleSignals.WRITABLE, 0); | 95 mwr = end0.handle.wait(MojoHandleSignals.WRITABLE, 0); |
| 95 Expect.isTrue(result.isOk); | 96 Expect.isTrue(mwr.result.isOk); |
| 96 | 97 |
| 97 // Try to read. | 98 // Try to read. |
| 98 ByteData data = new ByteData(10); | 99 ByteData data = new ByteData(10); |
| 99 end0.read(data); | 100 end0.read(data); |
| 100 Expect.isTrue(end0.status.isShouldWait); | 101 Expect.isTrue(end0.status.isShouldWait); |
| 101 | 102 |
| 102 // Write end1. | 103 // Write end1. |
| 103 String hello = "hello"; | 104 String hello = "hello"; |
| 104 ByteData helloData = | 105 ByteData helloData = |
| 105 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); | 106 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); |
| 106 result = end1.write(helloData); | 107 MojoResult result = end1.write(helloData); |
| 107 Expect.isTrue(result.isOk); | 108 Expect.isTrue(result.isOk); |
| 108 | 109 |
| 109 // end0 should now be readable. | 110 // end0 should now be readable. |
| 110 int res = RawMojoHandle.waitMany([end0.handle.h], | 111 MojoWaitManyResult mwmr = RawMojoHandle.waitMany([end0.handle.h], |
| 111 [MojoHandleSignals.READABLE], | 112 [MojoHandleSignals.READABLE], |
| 112 RawMojoHandle.DEADLINE_INDEFINITE); | 113 RawMojoHandle.DEADLINE_INDEFINITE); |
| 113 Expect.equals(res, MojoResult.kOk); | 114 Expect.isTrue(mwmr.result.isOk); |
| 114 | 115 |
| 115 // Read from end0. | 116 // Read from end0. |
| 116 MojoMessagePipeReadResult readResult = end0.read(data); | 117 MojoMessagePipeReadResult readResult = end0.read(data); |
| 117 Expect.isNotNull(readResult); | 118 Expect.isNotNull(readResult); |
| 118 Expect.isTrue(readResult.status.isOk); | 119 Expect.isTrue(readResult.status.isOk); |
| 119 Expect.equals(readResult.bytesRead, helloData.lengthInBytes); | 120 Expect.equals(readResult.bytesRead, helloData.lengthInBytes); |
| 120 Expect.equals(readResult.handlesRead, 0); | 121 Expect.equals(readResult.handlesRead, 0); |
| 121 | 122 |
| 122 String hello_result = new String.fromCharCodes( | 123 String hello_result = new String.fromCharCodes( |
| 123 data.buffer.asUint8List().sublist(0, readResult.bytesRead).toList()); | 124 data.buffer.asUint8List().sublist(0, readResult.bytesRead).toList()); |
| 124 Expect.equals(hello_result, "hello"); | 125 Expect.equals(hello_result, "hello"); |
| 125 | 126 |
| 126 // end0 should no longer be readable. | 127 // end0 should no longer be readable. |
| 127 result = end0.handle.wait(MojoHandleSignals.READABLE, 10); | 128 mwr = end0.handle.wait(MojoHandleSignals.READABLE, 10); |
| 128 Expect.isTrue(result.isDeadlineExceeded); | 129 Expect.isTrue(mwr.result.isDeadlineExceeded); |
| 129 | 130 |
| 130 // Close end0's handle. | 131 // Close end0's handle. |
| 131 result = end0.handle.close(); | 132 result = end0.handle.close(); |
| 132 Expect.isTrue(result.isOk); | 133 Expect.isTrue(result.isOk); |
| 133 | 134 |
| 134 // end1 should no longer be readable or writable. | 135 // end1 should no longer be readable or writable. |
| 135 result = end1.handle.wait(MojoHandleSignals.READWRITE, 1000); | 136 mwr = end1.handle.wait(MojoHandleSignals.READWRITE, 1000); |
| 136 Expect.isTrue(result.isFailedPrecondition); | 137 Expect.isTrue(mwr.result.isFailedPrecondition); |
| 137 | 138 |
| 138 result = end1.handle.close(); | 139 result = end1.handle.close(); |
| 139 Expect.isTrue(result.isOk); | 140 Expect.isTrue(result.isOk); |
| 140 } | 141 } |
| 141 | 142 |
| 142 | 143 |
| 143 basicDataPipeTest() { | 144 basicDataPipeTest() { |
| 144 MojoDataPipe pipe = new MojoDataPipe(); | 145 MojoDataPipe pipe = new MojoDataPipe(); |
| 145 Expect.isNotNull(pipe); | 146 Expect.isNotNull(pipe); |
| 146 Expect.isTrue(pipe.status.isOk); | 147 Expect.isTrue(pipe.status.isOk); |
| 147 Expect.isTrue(pipe.consumer.handle.isValid); | 148 Expect.isTrue(pipe.consumer.handle.isValid); |
| 148 Expect.isTrue(pipe.producer.handle.isValid); | 149 Expect.isTrue(pipe.producer.handle.isValid); |
| 149 | 150 |
| 150 MojoDataPipeProducer producer = pipe.producer; | 151 MojoDataPipeProducer producer = pipe.producer; |
| 151 MojoDataPipeConsumer consumer = pipe.consumer; | 152 MojoDataPipeConsumer consumer = pipe.consumer; |
| 152 Expect.isTrue(producer.handle.isValid); | 153 Expect.isTrue(producer.handle.isValid); |
| 153 Expect.isTrue(consumer.handle.isValid); | 154 Expect.isTrue(consumer.handle.isValid); |
| 154 | 155 |
| 155 // Consumer should not be readable. | 156 // Consumer should not be readable. |
| 156 MojoResult result = consumer.handle.wait(MojoHandleSignals.READABLE, 0); | 157 MojoWaitResult mwr = consumer.handle.wait(MojoHandleSignals.READABLE, 0); |
| 157 Expect.isTrue(result.isDeadlineExceeded); | 158 Expect.isTrue(mwr.result.isDeadlineExceeded); |
| 158 | 159 |
| 159 // Producer should be writable. | 160 // Producer should be writable. |
| 160 result = producer.handle.wait(MojoHandleSignals.WRITABLE, 0); | 161 mwr = producer.handle.wait(MojoHandleSignals.WRITABLE, 0); |
| 161 Expect.isTrue(result.isOk); | 162 Expect.isTrue(mwr.result.isOk); |
| 162 | 163 |
| 163 // Try to read from consumer. | 164 // Try to read from consumer. |
| 164 ByteData buffer = new ByteData(20); | 165 ByteData buffer = new ByteData(20); |
| 165 consumer.read(buffer, buffer.lengthInBytes, MojoDataPipeConsumer.FLAG_NONE); | 166 consumer.read(buffer, buffer.lengthInBytes, MojoDataPipeConsumer.FLAG_NONE); |
| 166 Expect.isTrue(consumer.status.isShouldWait); | 167 Expect.isTrue(consumer.status.isShouldWait); |
| 167 | 168 |
| 168 // Try to begin a two-phase read from consumer. | 169 // Try to begin a two-phase read from consumer. |
| 169 ByteData b = consumer.beginRead(20, MojoDataPipeConsumer.FLAG_NONE); | 170 ByteData b = consumer.beginRead(20, MojoDataPipeConsumer.FLAG_NONE); |
| 170 Expect.isNull(b); | 171 Expect.isNull(b); |
| 171 Expect.isTrue(consumer.status.isShouldWait); | 172 Expect.isTrue(consumer.status.isShouldWait); |
| 172 | 173 |
| 173 // Write to producer. | 174 // Write to producer. |
| 174 String hello = "hello "; | 175 String hello = "hello "; |
| 175 ByteData helloData = | 176 ByteData helloData = |
| 176 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); | 177 new ByteData.view((new Uint8List.fromList(hello.codeUnits)).buffer); |
| 177 int written = producer.write( | 178 int written = producer.write( |
| 178 helloData, helloData.lengthInBytes, MojoDataPipeProducer.FLAG_NONE); | 179 helloData, helloData.lengthInBytes, MojoDataPipeProducer.FLAG_NONE); |
| 179 Expect.isTrue(producer.status.isOk); | 180 Expect.isTrue(producer.status.isOk); |
| 180 Expect.equals(written, helloData.lengthInBytes); | 181 Expect.equals(written, helloData.lengthInBytes); |
| 181 | 182 |
| 182 // Now that we have written, the consumer should be readable. | 183 // Now that we have written, the consumer should be readable. |
| 183 int res = RawMojoHandle.waitMany([consumer.handle.h], | 184 MojoWaitManyResult mwmr = RawMojoHandle.waitMany( |
| 184 [MojoHandleSignals.READABLE], | 185 [consumer.handle.h], |
| 185 RawMojoHandle.DEADLINE_INDEFINITE); | 186 [MojoHandleSignals.READABLE], |
| 186 Expect.equals(res, MojoResult.kOk); | 187 RawMojoHandle.DEADLINE_INDEFINITE); |
| 188 Expect.isTrue(mwr.result.isOk); |
| 187 | 189 |
| 188 // Do a two-phase write to the producer. | 190 // Do a two-phase write to the producer. |
| 189 ByteData twoPhaseWrite = producer.beginWrite( | 191 ByteData twoPhaseWrite = producer.beginWrite( |
| 190 20, MojoDataPipeProducer.FLAG_NONE); | 192 20, MojoDataPipeProducer.FLAG_NONE); |
| 191 Expect.isTrue(producer.status.isOk); | 193 Expect.isTrue(producer.status.isOk); |
| 192 Expect.isNotNull(twoPhaseWrite); | 194 Expect.isNotNull(twoPhaseWrite); |
| 193 Expect.isTrue(twoPhaseWrite.lengthInBytes >= 20); | 195 Expect.isTrue(twoPhaseWrite.lengthInBytes >= 20); |
| 194 | 196 |
| 195 String world = "world"; | 197 String world = "world"; |
| 196 twoPhaseWrite.buffer.asUint8List().setAll(0, world.codeUnits); | 198 twoPhaseWrite.buffer.asUint8List().setAll(0, world.codeUnits); |
| 197 producer.endWrite(Uint8List.BYTES_PER_ELEMENT * world.codeUnits.length); | 199 producer.endWrite(Uint8List.BYTES_PER_ELEMENT * world.codeUnits.length); |
| 198 Expect.isTrue(producer.status.isOk); | 200 Expect.isTrue(producer.status.isOk); |
| 199 | 201 |
| 200 // Read one character from consumer. | 202 // Read one character from consumer. |
| 201 int read = consumer.read(buffer, 1, MojoDataPipeConsumer.FLAG_NONE); | 203 int read = consumer.read(buffer, 1, MojoDataPipeConsumer.FLAG_NONE); |
| 202 Expect.isTrue(consumer.status.isOk); | 204 Expect.isTrue(consumer.status.isOk); |
| 203 Expect.equals(read, 1); | 205 Expect.equals(read, 1); |
| 204 | 206 |
| 205 // Close the producer. | 207 // Close the producer. |
| 206 result = producer.handle.close(); | 208 MojoResult result = producer.handle.close(); |
| 207 Expect.isTrue(result.isOk); | 209 Expect.isTrue(result.isOk); |
| 208 | 210 |
| 209 // Consumer should still be readable. | 211 // Consumer should still be readable. |
| 210 result = consumer.handle.wait(MojoHandleSignals.READABLE, 0); | 212 mwr = consumer.handle.wait(MojoHandleSignals.READABLE, 0); |
| 211 Expect.isTrue(result.isOk); | 213 Expect.isTrue(mwr.result.isOk); |
| 212 | 214 |
| 213 // Get the number of remaining bytes. | 215 // Get the number of remaining bytes. |
| 214 int remaining = consumer.read( | 216 int remaining = consumer.read( |
| 215 null, 0, MojoDataPipeConsumer.FLAG_QUERY); | 217 null, 0, MojoDataPipeConsumer.FLAG_QUERY); |
| 216 Expect.isTrue(consumer.status.isOk); | 218 Expect.isTrue(consumer.status.isOk); |
| 217 Expect.equals(remaining, "hello world".length - 1); | 219 Expect.equals(remaining, "hello world".length - 1); |
| 218 | 220 |
| 219 // Do a two-phase read. | 221 // Do a two-phase read. |
| 220 ByteData twoPhaseRead = consumer.beginRead( | 222 ByteData twoPhaseRead = consumer.beginRead( |
| 221 remaining, MojoDataPipeConsumer.FLAG_NONE); | 223 remaining, MojoDataPipeConsumer.FLAG_NONE); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 duplicate = null; | 300 duplicate = null; |
| 299 } | 301 } |
| 300 | 302 |
| 301 | 303 |
| 302 main() { | 304 main() { |
| 303 invalidHandleTest(); | 305 invalidHandleTest(); |
| 304 basicMessagePipeTest(); | 306 basicMessagePipeTest(); |
| 305 basicDataPipeTest(); | 307 basicDataPipeTest(); |
| 306 basicSharedBufferTest(); | 308 basicSharedBufferTest(); |
| 307 } | 309 } |
| OLD | NEW |