public void ReadSmallerBuffers() { using (var stream = new BlockingMemoryStream()) { stream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4); stream.Write(new byte[] { 5, 6, 7, 8, 9 }, 0, 5); byte[] buffer = new byte[3]; int count = stream.Read(buffer, 0, buffer.Length); Assert.Equal(3, count); Assert.Equal(1, buffer[0]); Assert.Equal(2, buffer[1]); Assert.Equal(3, buffer[2]); count = stream.Read(buffer, 0, buffer.Length); Assert.Equal(1, count); Assert.Equal(4, buffer[0]); count = stream.Read(buffer, 0, buffer.Length); Assert.Equal(3, count); Assert.Equal(5, buffer[0]); Assert.Equal(6, buffer[1]); Assert.Equal(7, buffer[2]); count = stream.Read(buffer, 0, buffer.Length); Assert.Equal(2, count); Assert.Equal(8, buffer[0]); Assert.Equal(9, buffer[1]); } }
public CommandResult Execute() { TextWriter originalConsoleOut = _environment.GetConsoleOut(); TextWriter originalConsoleError = _environment.GetConsoleError(); string originalWorkingDirectory = _environment.GetWorkingDirectory(); try { // redirecting the standard out and error so we can forward // the output to the caller using (BlockingMemoryStream outStream = new BlockingMemoryStream()) using (BlockingMemoryStream errorStream = new BlockingMemoryStream()) { _environment.SetConsoleOut(new StreamWriter(outStream) { AutoFlush = true }); _environment.SetConsoleError(new StreamWriter(errorStream) { AutoFlush = true }); // Reset the Reporters to the new Console Out and Error. Reporter.Reset(); if (!string.IsNullOrEmpty(_workingDirectory)) { _environment.SetWorkingDirectory(_workingDirectory); } var taskOut = _stdOut.BeginRead(new StreamReader(outStream)); var taskErr = _stdErr.BeginRead(new StreamReader(errorStream)); int exitCode = _builtInCommand(_commandArgs.ToArray()); outStream.DoneWriting(); errorStream.DoneWriting(); Task.WaitAll(taskOut, taskErr); // fake out a ProcessStartInfo using the Muxer command name, since this is a built-in command ProcessStartInfo startInfo = new ProcessStartInfo(new Muxer().MuxerPath, $"{CommandName} {CommandArgs}"); return(new CommandResult(startInfo, exitCode, null, null)); } } finally { _environment.SetConsoleOut(originalConsoleOut); _environment.SetConsoleError(originalConsoleError); _environment.SetWorkingDirectory(originalWorkingDirectory); Reporter.Reset(); } }
public void ReadBiggerBuffer() { using (var stream = new BlockingMemoryStream()) { stream.Write(new byte[] { 1, 2, 3 }, 0, 3); byte[] buffer = new byte[10]; int count = stream.Read(buffer, 0, buffer.Length); Assert.Equal(3, count); Assert.Equal(1, buffer[0]); Assert.Equal(2, buffer[1]); Assert.Equal(3, buffer[2]); } }
public CommandResult Execute() { TextWriter originalConsoleOut = _environment.GetConsoleOut(); TextWriter originalConsoleError = _environment.GetConsoleError(); string originalWorkingDirectory = _environment.GetWorkingDirectory(); try { // redirecting the standard out and error so we can forward // the output to the caller using (BlockingMemoryStream outStream = new BlockingMemoryStream()) using (BlockingMemoryStream errorStream = new BlockingMemoryStream()) { _environment.SetConsoleOut(new StreamWriter(outStream) { AutoFlush = true }); _environment.SetConsoleError(new StreamWriter(errorStream) { AutoFlush = true }); // Reset the Reporters to the new Console Out and Error. Reporter.Reset(); if (!string.IsNullOrEmpty(_workingDirectory)) { _environment.SetWorkingDirectory(_workingDirectory); } var taskOut = _stdOut.BeginRead(new StreamReader(outStream)); var taskErr = _stdErr.BeginRead(new StreamReader(errorStream)); int exitCode = _builtInCommand(_commandArgs.ToArray()); outStream.DoneWriting(); errorStream.DoneWriting(); Task.WaitAll(taskOut, taskErr); // fake out a ProcessStartInfo using the Muxer command name, since this is a built-in command ProcessStartInfo startInfo = new ProcessStartInfo(new Muxer().MuxerPath, $"{CommandName} {CommandArgs}"); return new CommandResult(startInfo, exitCode, null, null); } } finally { _environment.SetConsoleOut(originalConsoleOut); _environment.SetConsoleError(originalConsoleError); _environment.SetWorkingDirectory(originalWorkingDirectory); Reporter.Reset(); } }
public void TestReadBlocksUntilWrite() { using (var stream = new BlockingMemoryStream()) { ManualResetEvent readerThreadExecuting = new ManualResetEvent(false); bool readerThreadSuccessful = false; Thread readerThread = new Thread(() => { byte[] buffer = new byte[10]; readerThreadExecuting.Set(); int count = stream.Read(buffer, 0, buffer.Length); Assert.Equal(3, count); Assert.Equal(1, buffer[0]); Assert.Equal(2, buffer[1]); Assert.Equal(3, buffer[2]); readerThreadSuccessful = true; }); readerThread.IsBackground = true; readerThread.Start(); // ensure the thread is executing readerThreadExecuting.WaitOne(); Assert.True(readerThread.IsAlive); // give it a little while to ensure it is blocking Thread.Sleep(10); Assert.True(readerThread.IsAlive); stream.Write(new byte[] { 1, 2, 3 }, 0, 3); Assert.True(readerThread.Join(1000)); Assert.True(readerThreadSuccessful); } }