Пример #1
0
        public void CancelledTokenFastPath()
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.Cancel();
            CancellationToken cancelledToken = cts.Token;

            using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
            {
                FSAssert.IsCancelled(fs.ReadAsync(new byte[1], 0, 1, cancelledToken), cancelledToken);
            }

            using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write))
            {
                // before write only check
                FSAssert.IsCancelled(fs.ReadAsync(new byte[1], 0, 1, cancelledToken), cancelledToken);

                fs.Dispose();
                // before disposed check
                FSAssert.IsCancelled(fs.ReadAsync(new byte[1], 0, 1, cancelledToken), cancelledToken);

                // out of bounds checking happens first
                Assert.Throws <ArgumentException>(null, () =>
                                                  FSAssert.CompletesSynchronously(fs.ReadAsync(new byte[2], 1, 2, cancelledToken)));

                // count is checked prior
                Assert.Throws <ArgumentOutOfRangeException>("count", () =>
                                                            FSAssert.CompletesSynchronously(fs.ReadAsync(new byte[1], 0, -1, cancelledToken)));

                // offset is checked prior
                Assert.Throws <ArgumentOutOfRangeException>("offset", () =>
                                                            FSAssert.CompletesSynchronously(fs.ReadAsync(new byte[1], -1, -1, cancelledToken)));

                // buffer is checked first
                Assert.Throws <ArgumentNullException>("buffer", () =>
                                                      FSAssert.CompletesSynchronously(fs.ReadAsync(null, -1, -1, cancelledToken)));
            }
        }
Пример #2
0
        public async Task ReadAsyncBufferedCompletesSynchronously()
        {
            // It doesn't make sense to spin up a background thread just to do a memcpy.
            string fileName = GetTestFilePath();

            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {
                fs.Write(TestBuffer, 0, TestBuffer.Length);
                fs.Write(TestBuffer, 0, TestBuffer.Length);
            }

            // This isn't working now for useAsync:true since we always have a usercallback
            // that get's run on the threadpool (see Win32FileStream.EndReadTask)

            // This isn't working now for useAsync:false since we always call
            // Stream.ReadAsync that queues Read on a background thread
            foreach (bool useAsync in new[] { true, false })
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, TestBuffer.Length * 2, useAsync))
                {
                    byte[] buffer = new byte[TestBuffer.Length];

                    // Existing issue: FileStreamAsyncResult doesn't set CompletedSynchronously correctly.

                    // prime the internal buffer
                    Assert.Equal(TestBuffer.Length, await fs.ReadAsync(buffer, 0, buffer.Length));
                    Assert.Equal(TestBuffer, buffer);

                    Array.Clear(buffer, 0, buffer.Length);

                    // read should now complete synchronously since it is serviced by the read buffer filled in the first request
                    Assert.Equal(TestBuffer.Length,
                                 FSAssert.CompletesSynchronously(fs.ReadAsync(buffer, 0, buffer.Length)));
                    Assert.Equal(TestBuffer, buffer);
                }
            }
        }
Пример #3
0
        public void ThrowWhenHandlePositionIsChanged()
        {
            string fileName = GetTestFilePath();

            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {
                // write some data to move the position, flush to ensure OS position is updated
                fs.Write(TestBuffer, 0, TestBuffer.Length);
                fs.Flush();

                if (fs.SafeFileHandle.IsInvalid)
                {
                    // nothing to test
                    return;
                }

                using (FileStream fsr = new FileStream(fs.SafeFileHandle, FileAccess.Read, TestBuffer.Length))
                {
                    Assert.Equal(TestBuffer.Length, fs.Position);
                    Assert.Equal(TestBuffer.Length, fsr.Position);

                    // Operations on original filestream will fail if data is in buffer and position changes.

                    // Put data in FS write buffer and update position from FSR
                    fs.WriteByte(0);
                    fsr.Position = 0;
                    Assert.Throws <IOException>(() => fs.Position);

                    fs.WriteByte(0);
                    fsr.Position++;
                    Assert.Throws <IOException>(() => fs.Read(new byte[1], 0, 1));

                    fs.WriteByte(0);
                    fsr.Position++;
                    if (Interop.IsWindows) // TODO: [ActiveIssue(812)]: Remove guard when async I/O is properly implemented on Unix
                    {
                        Assert.Throws <IOException>(() => FSAssert.CompletesSynchronously(fs.ReadAsync(new byte[1], 0, 1)));
                    }
                    else
                    {
                        Assert.ThrowsAsync <IOException>(() => fs.ReadAsync(new byte[1], 0, 1)).Wait();
                    }

                    fs.WriteByte(0);
                    fsr.Position++;
                    Assert.Throws <IOException>(() => fs.ReadByte());

                    fs.WriteByte(0);
                    fsr.Position++;
                    Assert.Throws <IOException>(() => fs.Seek(0, SeekOrigin.End));

                    fs.WriteByte(0);
                    fsr.Position++;
                    Assert.Throws <IOException>(() => fs.SetLength(2));

                    fs.WriteByte(0);
                    fsr.Position++;
                    Assert.Throws <IOException>(() => fs.Flush());

                    fs.WriteByte(0);
                    fsr.Position++;
                    Assert.Throws <IOException>(() => fs.Dispose());
                }
            }
        }