[PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_Client_ReadWriteCancelledToken_Throws_OperationCanceledException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeClientStream client = pair.clientStream;
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                pair.Connect();
                if (client.CanRead)
                {
                    var  cts             = new CancellationTokenSource();
                    Task clientReadToken = client.ReadAsync(buffer, 0, buffer.Length, cts.Token);

                    Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientReadToken);
                }
                if (client.CanWrite)
                {
                    var  cts = new CancellationTokenSource();
                    Task clientWriteToken = client.WriteAsync(buffer, 0, buffer.Length, cts.Token);

                    Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientWriteToken);
                }
            }
        }
Ejemplo n.º 2
0
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_Client_ReadWriteCancelledToken_Throws_OperationCanceledException()
        {
            if ((Options & PipeOptions.Asynchronous) == 0)
            {
                // Test depends on PipeOptions.Asynchronous, as CancelIoEx is for overlapped I/O
                return;
            }

            using StreamPair streams = await CreateConnectedStreamsAsync();

            (NamedPipeServerStream server, NamedPipeClientStream client) = GetClientAndServer(streams);

            var buffer = new byte[4];

            if (client.CanRead)
            {
                var  cts             = new CancellationTokenSource();
                Task clientReadToken = client.ReadAsync(buffer, 0, buffer.Length, cts.Token);

                Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientReadToken);
            }

            if (client.CanWrite)
            {
                var  cts = new CancellationTokenSource();
                Task clientWriteToken = client.WriteAsync(buffer, 0, buffer.Length, cts.Token);

                Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientWriteToken);
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOff_Server_ReadWriteCancelledToken_Throws_OperationCanceledException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                byte[] buffer = new byte[] { 0, 0, 0, 0 };

                pair.Connect();

                if (server.CanRead)
                {
                    Task serverReadToken = server.ReadAsync(buffer, 0, buffer.Length, CancellationToken.None);

                    Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverReadToken);

                    Assert.True(serverReadToken.IsCanceled);
                }
                if (server.CanWrite)
                {
                    Task serverWriteToken = server.WriteAsync(buffer, 0, buffer.Length, CancellationToken.None);

                    Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverWriteToken);

                    Assert.True(serverWriteToken.IsCanceled);
                }
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOff_Client_ReadWriteCancelledToken_Throws_OperationCanceledException()
        {
            using StreamPair streams = await CreateConnectedStreamsAsync();

            (NamedPipeServerStream server, NamedPipeClientStream client) = GetClientAndServer(streams);

            var buffer = new byte[4];

            if (client.CanRead)
            {
                Task clientReadToken = client.ReadAsync(buffer, 0, buffer.Length, CancellationToken.None);

                Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientReadToken);

                Assert.True(clientReadToken.IsCanceled);
            }

            if (client.CanWrite)
            {
                Task clientWriteToken = client.WriteAsync(buffer, 0, buffer.Length, CancellationToken.None);

                Assert.True(InteropTest.CancelIoEx(client.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => clientWriteToken);

                Assert.True(clientWriteToken.IsCanceled);
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_Server_ReadWriteCancelledToken_Throws_OperationCanceledException()
        {
            using StreamPair streams = await CreateConnectedStreamsAsync();

            (NamedPipeServerStream server, NamedPipeClientStream client) = GetClientAndServer(streams);

            var buffer = new byte[4];

            if (server.CanRead)
            {
                var  cts             = new CancellationTokenSource();
                Task serverReadToken = server.ReadAsync(buffer, 0, buffer.Length, cts.Token);

                Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverReadToken);
            }
            if (server.CanWrite)
            {
                var  cts = new CancellationTokenSource();
                Task serverWriteToken = server.WriteAsync(buffer, 0, buffer.Length, cts.Token);

                Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => serverWriteToken);
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_IOException()
        {
            (NamedPipeServerStream server, NamedPipeClientStream client) = CreateServerAndClientStreams();
            using StreamPair streams = (server, client);

            var  cts = new CancellationTokenSource();
            Task waitForConnectionTask = server.WaitForConnectionAsync(cts.Token);

            Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
            await Assert.ThrowsAsync <IOException>(() => waitForConnectionTask);
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_IOException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                var cts = new CancellationTokenSource();
                NamedPipeServerStream server = pair.serverStream;
                Task waitForConnectionTask   = server.WaitForConnectionAsync(cts.Token);

                Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAsync <IOException>(() => waitForConnectionTask);
            }
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOff_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_OperationCanceledException()
        {
            (NamedPipeServerStream server, NamedPipeClientStream client) = CreateServerAndClientStreams();
            using StreamPair streams = (server, client);

            Task waitForConnectionTask = server.WaitForConnectionAsync(CancellationToken.None);

            Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => waitForConnectionTask);

            Assert.True(waitForConnectionTask.IsCanceled);
        }
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOff_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_OperationCanceledException()
        {
            using (NamedPipePair pair = CreateNamedPipePair())
            {
                NamedPipeServerStream server = pair.serverStream;
                Task waitForConnectionTask   = server.WaitForConnectionAsync(CancellationToken.None);

                Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => waitForConnectionTask);

                Assert.True(waitForConnectionTask.IsCanceled);
            }
        }
Ejemplo n.º 10
0
        [PlatformSpecific(TestPlatforms.Windows)] // P/Invoking to Win32 functions
        public async Task CancelTokenOn_ServerWaitForConnectionAsyncWithOuterCancellation_Throws_IOException()
        {
            if ((Options & PipeOptions.Asynchronous) == 0)
            {
                // Test depends on PipeOptions.Asynchronous, as CancelIoEx is for overlapped I/O
                return;
            }

            (NamedPipeServerStream server, NamedPipeClientStream client) = CreateServerAndClientStreams();
            using StreamPair streams = (server, client);

            var  cts = new CancellationTokenSource();
            Task waitForConnectionTask = server.WaitForConnectionAsync(cts.Token);

            Assert.True(InteropTest.CancelIoEx(server.SafePipeHandle), "Outer cancellation failed");
            await Assert.ThrowsAsync <IOException>(() => waitForConnectionTask);
        }