public static void AsOutputStream_WrapsToSameInstance() { Stream managedStream = TestStreamProvider.CreateWriteOnlyStream(); using (IOutputStream outs = managedStream.AsOutputStream()) { Assert.NotNull(outs); Assert.Same(outs, managedStream.AsOutputStream()); } }
public static void AsOutputStream_Equal() { Stream stream = TestStreamProvider.CreateWriteOnlyStream(); using (IOutputStream outsOne = stream.AsOutputStream()) using (IOutputStream outsTwo = stream.AsOutputStream()) { Assert.Equal(outsOne, outsOne); } }
public static void AsOutputStream_RoundtripUnwrap() { // NetFx Stream -> IOutputStream -> NetFx Stream -> roundtrip reference equality is preserved Stream managedStream = TestStreamProvider.CreateWriteOnlyStream(); using (IOutputStream outs = managedStream.AsOutputStream()) { Assert.Same(managedStream, outs.AsStreamForWrite()); } }
public static void AsOutputStream_FromWriteOnlyStream() { Stream managedStream = TestStreamProvider.CreateWriteOnlyStream(); using (IOutputStream outs = managedStream.AsOutputStream()) { Assert.NotNull(outs); // Adapting a write-only managed Stream to IInputStream must throw a NotSupportedException Assert.Throws <NotSupportedException>(() => { IInputStream ins = managedStream.AsInputStream(); }); } }
public static void AsOutputStream_NotEqual() { Stream streamOne = TestStreamProvider.CreateWriteOnlyStream(); Stream streamTwo = TestStreamProvider.CreateWriteOnlyStream(); Assert.NotEqual(streamOne, streamTwo); using (IOutputStream outsOne = streamOne.AsOutputStream()) using (IOutputStream outsTwo = streamTwo.AsOutputStream()) { Assert.NotEqual(outsOne, outsTwo); } }
private static void DoTestWrite(Func <Stream> createStreamFunc, bool mustInvokeProgressHandler) { Stream backingStream = createStreamFunc(); using (IOutputStream stream = backingStream.AsOutputStream()) { // Create test data Random rnd = new Random(20100720); // Must be a different seed than used for TestStreamProvider.ModelStreamContents byte[] modelWriteData = new byte[0xA000]; rnd.NextBytes(modelWriteData); // Start test IBuffer buffer = modelWriteData.AsBuffer(); // ibuffer.Length for IBuffer created by Array.ToBuffer(void) must equal to array.Length Assert.Equal((uint)modelWriteData.Length, buffer.Length); // ibuffer.Capacity for IBuffer created by Array.ToBuffer(void) must equal to array.Length Assert.Equal((uint)modelWriteData.Length, buffer.Capacity); IAsyncOperationWithProgress <uint, uint> writeOp = stream.WriteAsync(buffer); // Note the race. By the tie we get here, the status of the op may be started or already completed. AsyncStatus writeOpStatus = writeOp.Status; Assert.True(writeOpStatus == AsyncStatus.Completed || writeOpStatus == AsyncStatus.Started, "New writeOp must have Status = Started or Completed (race)"); uint writeOpId = writeOp.Id; bool progressCallbackInvoked = false; bool completedCallbackInvoked = false; uint resultBytesWritten = 0; EventWaitHandle waitHandle = new ManualResetEvent(false); writeOp.Progress = (asyncWriteOp, bytesCompleted) => { progressCallbackInvoked = true; // asyncWriteOp.Id in a progress callback must match the ID of the asyncWriteOp to which the callback was assigned Assert.Equal(writeOpId, asyncWriteOp.Id); // asyncWriteOp.Status must be 'Started' for an asyncWriteOp in progress Assert.Equal(AsyncStatus.Started, asyncWriteOp.Status); // bytesCompleted must be in range [0, maxBytesToWrite] asyncWriteOp in progress Assert.InRange(bytesCompleted, 0u, (uint)TestStreamProvider.ModelStreamLength); }; writeOp.Completed = (asyncWriteOp, passedStatus) => { try { completedCallbackInvoked = true; // asyncWriteOp.Id in a completion callback must match the ID of the asyncWriteOp to which the callback was assigned Assert.Equal(writeOpId, asyncWriteOp.Id); // asyncWriteOp.Status must match passedStatus for a completed asyncWriteOp Assert.Equal(passedStatus, asyncWriteOp.Status); // asyncWriteOp.Status must be 'Completed' for a completed asyncWriteOp Assert.Equal(AsyncStatus.Completed, asyncWriteOp.Status); uint bytesWritten = asyncWriteOp.GetResults(); // asyncWriteOp.GetResults() must return that all required bytes were written for a completed asyncWriteOp Assert.Equal((uint)modelWriteData.Length, bytesWritten); resultBytesWritten = bytesWritten; } finally { waitHandle.Set(); } }; // Now, let's block until the write op is complete. // We speculate that it will complete within 3500 msec, although under high load it may not be. // If the test fails we should use a better way to determine if callback is really not invoked, or if it's just too slow. waitHandle.WaitOne(500); waitHandle.WaitOne(1000); waitHandle.WaitOne(2000); if (mustInvokeProgressHandler) { Assert.True(progressCallbackInvoked, "Progress callback specified to WriteAsync callback must be invoked when reading from this kind of stream"); } Assert.True(completedCallbackInvoked, "Completion callback specified to WriteAsync callback must be invoked"); // writeOp.Status must be 'Completed' for a completed async writeOp Assert.Equal(AsyncStatus.Completed, writeOp.Status); // writeOp.GetResults() must return that all required bytes were written for a completed async writeOp Assert.Equal((uint)modelWriteData.Length, resultBytesWritten); // Check contents backingStream.Seek(0, SeekOrigin.Begin); byte[] verifyBuff = new byte[modelWriteData.Length + 1024]; int r = backingStream.Read(verifyBuff, 0, verifyBuff.Length); for (int i = 0; i < modelWriteData.Length; i++) { Assert.Equal(modelWriteData[i], verifyBuff[i]); } } }