protected override async Task <StreamPair> CreateConnectedStreamsAsync() { string pipeName = FileSystemTest.GetNamedPipeServerStreamName(); string pipePath = Path.GetFullPath($@"\\.\pipe\{pipeName}"); var server = new NamedPipeServerStream(pipeName, PipeDirection.In); var clienStream = new FileStream(pipePath, FileMode.Open, FileAccess.Write, FileShare.None); await server.WaitForConnectionAsync(); var serverStrean = new FileStream(new SafeFileHandle(server.SafePipeHandle.DangerousGetHandle(), true), FileAccess.Read); server.SafePipeHandle.SetHandleAsInvalid(); return(serverStrean, clienStream); }
protected override async Task <StreamPair> CreateConnectedStreamsAsync() { string name = FileSystemTest.GetNamedPipeServerStreamName(); var server = new NamedPipeServerStream(name, PipeDirection.In); var client = new NamedPipeClientStream(".", name, PipeDirection.Out); await WhenAllOrAnyFailed(server.WaitForConnectionAsync(), client.ConnectAsync()); var fs1 = new FileStream(new SafeFileHandle(server.SafePipeHandle.DangerousGetHandle(), true), FileAccess.Read); var fs2 = new FileStream(new SafeFileHandle(client.SafePipeHandle.DangerousGetHandle(), true), FileAccess.Write); server.SafePipeHandle.SetHandleAsInvalid(); client.SafePipeHandle.SetHandleAsInvalid(); return(fs1, fs2); }
public async Task ReadAllBytes_NonSeekableFileStream_InWindows() { string pipeName = FileSystemTest.GetNamedPipeServerStreamName(); string pipePath = Path.GetFullPath($@"\\.\pipe\{pipeName}"); var namedPipeWriterStream = new NamedPipeServerStream(pipeName, PipeDirection.Out); var contentBytes = new byte[] { 1, 2, 3 }; using (var cts = new CancellationTokenSource()) { Task writingServerTask = WaitConnectionAndWritePipeStreamAsync(namedPipeWriterStream, contentBytes, cts.Token); Task <byte[]> readTask = Task.Run(() => File.ReadAllBytes(pipePath), cts.Token); cts.CancelAfter(TimeSpan.FromSeconds(3)); await writingServerTask; byte[] readBytes = await readTask; Assert.Equal <byte>(contentBytes, readBytes); }