Exemplo n.º 1
0
        public static void AsInputStream_Equal()
        {
            Stream stream = TestStreamProvider.CreateReadOnlyStream();

            using (IInputStream insOne = stream.AsInputStream())
                using (IInputStream insTwo = stream.AsInputStream())
                {
                    Assert.Equal(insOne, insTwo);
                }
        }
Exemplo n.º 2
0
        public static void AsInputStream_RoundtripUnwrap()
        {
            // NetFx Stream -> IInputStream -> NetFx Stream -> roundtrip reference equality is preserved
            Stream managedStream = TestStreamProvider.CreateReadOnlyStream();

            using (IInputStream ins = managedStream.AsInputStream())
            {
                Assert.Same(managedStream, ins.AsStreamForRead());
            }
        }
Exemplo n.º 3
0
        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());
            }
        }
Exemplo n.º 4
0
        public static void AsInputStream_WrapsToSameInstance()
        {
            Stream managedStream = TestStreamProvider.CreateReadOnlyStream();

            using (IInputStream ins = managedStream.AsInputStream())
            {
                Assert.NotNull(ins);
                Assert.Same(ins, managedStream.AsInputStream());
            }
        }
Exemplo n.º 5
0
        public static void AsOutputStream_WrapsToSameInstance()
        {
            Stream managedStream = TestStreamProvider.CreateWriteOnlyStream();

            using (IOutputStream outs = managedStream.AsOutputStream())
            {
                Assert.NotNull(outs);
                Assert.Same(outs, managedStream.AsOutputStream());
            }
        }
Exemplo n.º 6
0
        public static void AsOutputStream_Equal()
        {
            Stream stream = TestStreamProvider.CreateWriteOnlyStream();

            using (IOutputStream outsOne = stream.AsOutputStream())
                using (IOutputStream outsTwo = stream.AsOutputStream())
                {
                    Assert.Equal(outsOne, outsOne);
                }
        }
Exemplo n.º 7
0
        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(); });
            }
        }
Exemplo n.º 8
0
        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);
                }
        }
Exemplo n.º 9
0
        public static void AsInputStream_NotEqual()
        {
            Stream streamOne = TestStreamProvider.CreateReadOnlyStream();
            Stream streamTwo = TestStreamProvider.CreateReadOnlyStream();

            Assert.NotEqual(streamOne, streamTwo);

            using (IInputStream insOne = streamOne.AsInputStream())
                using (IInputStream insTwo = streamTwo.AsInputStream())
                {
                    Assert.NotEqual(insOne, insTwo);
                }
        }
Exemplo n.º 10
0
        private static void DoTestRead(Func <IInputStream> createStreamFunc, InputStreamOptions inputStreamOptions, bool mustInvokeProgressHandler, bool completesSynchronously)
        {
            IInputStream stream = createStreamFunc();
            IBuffer      buffer = WindowsRuntimeBuffer.Create(TestStreamProvider.ModelStreamLength);

            IAsyncOperationWithProgress <IBuffer, uint> readOp = stream.ReadAsync(buffer, (uint)TestStreamProvider.ModelStreamLength, inputStreamOptions);

            if (completesSynchronously)
            {
                // New readOp for a stream where we know that reading is sycnhronous must have Status = Completed
                Assert.Equal(AsyncStatus.Completed, readOp.Status);
            }
            else
            {
                // Note the race. By the tie we get here, the status of the op may be started or already completed.
                AsyncStatus readOpStatus = readOp.Status;
                Assert.True(readOpStatus == AsyncStatus.Completed || readOpStatus == AsyncStatus.Started, "New readOp must have Status = Started or Completed (race)");
            }

            bool progressCallbackInvoked  = false;
            bool completedCallbackInvoked = false;

            uint            readOpId   = readOp.Id;
            EventWaitHandle waitHandle = new ManualResetEvent(false);

            readOp.Progress = (asyncReadOp, bytesCompleted) =>
            {
                progressCallbackInvoked = true;

                // asyncReadOp.Id in a progress callback must match the ID of the asyncReadOp to which the callback was assigned
                Assert.Equal(readOpId, asyncReadOp.Id);

                // asyncReadOp.Status must be 'Started' for an asyncReadOp in progress
                Assert.Equal(AsyncStatus.Started, asyncReadOp.Status);

                // bytesCompleted must be in range [0, maxBytesToRead] asyncReadOp in progress
                Assert.InRange(bytesCompleted, 0u, (uint)TestStreamProvider.ModelStreamLength);
            };

            readOp.Completed = (asyncReadOp, passedStatus) =>
            {
                try
                {
                    completedCallbackInvoked = true;

                    // asyncReadOp.Id in a completion callback must match the ID of the asyncReadOp to which the callback was assigned
                    Assert.Equal(readOpId, asyncReadOp.Id);

                    // asyncReadOp.Status must match passedStatus for a completed asyncReadOp
                    Assert.Equal(passedStatus, asyncReadOp.Status);

                    // asyncReadOp.Status must be 'Completed' for a completed asyncReadOp
                    Assert.Equal(AsyncStatus.Completed, asyncReadOp.Status);

                    IBuffer resultBuffer = asyncReadOp.GetResults();

                    // asyncReadOp.GetResults() must not return null for a completed asyncReadOp
                    Assert.NotNull(resultBuffer);

                    AssertExtensions.GreaterThan(resultBuffer.Capacity, 0u, "resultBuffer.Capacity should be more than zero in completed callback");
                    AssertExtensions.GreaterThan(resultBuffer.Length, 0u, "resultBuffer.Length should be more than zero in completed callback");
                    AssertExtensions.LessThanOrEqualTo(resultBuffer.Length, resultBuffer.Capacity, "resultBuffer.Length should be <= Capacity in completed callback");

                    if (inputStreamOptions == InputStreamOptions.None)
                    {
                        // resultBuffer.Length must be equal to requested number of bytes when an asyncReadOp with
                        // InputStreamOptions.None completes successfully
                        Assert.Equal(resultBuffer.Length, (uint)TestStreamProvider.ModelStreamLength);
                    }

                    if (inputStreamOptions == InputStreamOptions.Partial)
                    {
                        AssertExtensions.LessThanOrEqualTo(resultBuffer.Length, (uint)TestStreamProvider.ModelStreamLength,
                                                           "resultBuffer.Length must be <= requested number of bytes with InputStreamOptions.Partial in completed callback");
                    }
                    buffer = resultBuffer;
                }
                finally
                {
                    waitHandle.Set();
                }
            };

            // Now, let's block until the read 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 ReadAsync callback must be invoked when reading from this kind of stream");
            }

            Assert.True(completedCallbackInvoked,
                        "Completion callback specified to ReadAsync callback must be invoked");

            // readOp.Status must be 'Completed' for a completed async readOp
            Assert.Equal(AsyncStatus.Completed, readOp.Status);

            AssertExtensions.GreaterThan(buffer.Capacity, 0u, "buffer.Capacity should be greater than zero bytes");
            AssertExtensions.GreaterThan(buffer.Length, 0u, "buffer.Length should be greater than zero bytes");
            AssertExtensions.LessThanOrEqualTo(buffer.Length, buffer.Capacity, "buffer.Length <= buffer.Capacity is required for a completed async readOp");

            if (inputStreamOptions == InputStreamOptions.None)
            {
                // buffer.Length must be equal to requested number of bytes when an async readOp with
                //  InputStreamOptions.None completes successfully
                Assert.Equal((uint)TestStreamProvider.ModelStreamLength, buffer.Length);
            }

            if (inputStreamOptions == InputStreamOptions.Partial)
            {
                AssertExtensions.LessThanOrEqualTo(buffer.Length, (uint)TestStreamProvider.ModelStreamLength,
                                                   "resultBuffer.Length must be <= requested number of bytes with InputStreamOptions.Partial");
            }

            byte[] results = new byte[buffer.Length];
            buffer.CopyTo(0, results, 0, (int)buffer.Length);

            Assert.True(TestStreamProvider.CheckContent(results, 0, (int)buffer.Length),
                        "Result data returned from AsyncRead must be the same as expected from the test data source");
        }