Skip to content

Commit

Permalink
test: Add test code for Process
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Sep 29, 2024
1 parent 06c33a3 commit 516bfd5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ public static void Main(string[] args)
{
var delay = args.Length > 0 ? int.Parse(args[0]) : 1000;
var dateTimeOffset = DateTimeOffset.UtcNow;
while (DateTimeOffset.UtcNow < dateTimeOffset.AddMilliseconds(delay))
var endDateTimeOffset = dateTimeOffset.AddMilliseconds(delay);
while ((dateTimeOffset = DateTimeOffset.UtcNow) < endDateTimeOffset)
{
System.Threading.Thread.Sleep(100);
System.Console.Out.Write("out: ");
System.Console.Out.WriteLine(dateTimeOffset);
System.Console.Error.Write("error: ");
System.Console.Error.WriteLine(dateTimeOffset);
}

System.Console.Out.Write(dateTimeOffset.ToString("o"));
}
}
43 changes: 42 additions & 1 deletion test/LibplanetConsole.Console.Tests/ProcessTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using Xunit.Extensions.AssemblyFixture;

namespace LibplanetConsole.Console.Tests;
Expand Down Expand Up @@ -94,7 +95,9 @@ public void Run_DotnetHelp_Test()
{
var dotnetPath = ProcessEnvironment.DotnetPath;
var process = new TestProcess(dotnetPath, "--help");
using var outputCollector = new ProcessOutputCollector(process);
process.Run();
Assert.NotEmpty(outputCollector.Output);
Assert.False(process.IsRunning);
Assert.Equal(-1, process.Id);
}
Expand All @@ -104,7 +107,9 @@ public async Task RunAsync_DotnetHelp_TestAsync()
{
var dotnetPath = ProcessEnvironment.DotnetPath;
var process = new TestProcess(dotnetPath, "--help");
using var outputCollector = new ProcessOutputCollector(process);
await process.RunAsync(default);
Assert.NotEmpty(outputCollector.Output);
Assert.False(process.IsRunning);
Assert.Equal(-1, process.Id);
}
Expand All @@ -114,25 +119,37 @@ public void Run_DotnetHelp_Twice_Test()
{
var dotnetPath = ProcessEnvironment.DotnetPath;
var process = new TestProcess(dotnetPath, "--help");
var outputCollector1 = new ProcessOutputCollector(process);
process.Run();
outputCollector1.Dispose();
Assert.False(process.IsRunning);
Assert.Equal(-1, process.Id);
var outputCollector2 = new ProcessOutputCollector(process);
process.Run();
outputCollector2.Dispose();
Assert.False(process.IsRunning);
Assert.Equal(-1, process.Id);

Assert.Equal(outputCollector1.Output, outputCollector2.Output);
}

[Fact]
public async Task RunAsync_DotnetHelp_Twice_TestAsync()
{
var dotnetPath = ProcessEnvironment.DotnetPath;
var process = new TestProcess(dotnetPath, "--help");
var outputCollector1 = new ProcessOutputCollector(process);
await process.RunAsync(default);
outputCollector1.Dispose();
Assert.False(process.IsRunning);
Assert.Equal(-1, process.Id);
var outputCollector2 = new ProcessOutputCollector(process);
await process.RunAsync(default);
outputCollector2.Dispose();
Assert.False(process.IsRunning);
Assert.Equal(-1, process.Id);

Assert.Equal(outputCollector1.Output, outputCollector2.Output);
}

[Fact]
Expand All @@ -141,7 +158,14 @@ public void Run_Runtime_Test()
var dotnetPath = ProcessEnvironment.DotnetPath;
var executionPath = consoleApplicationFixture.ExecutionPath;
var process = new TestProcess(dotnetPath, executionPath);
using var outputCollector = new ProcessOutputCollector(process);
var expectedTimestamp = DateTimeOffset.UtcNow + TimeSpan.FromMilliseconds(1000);
process.Run();
var output = outputCollector.Output;
var lines = output.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var actualTimestamp = DateTimeOffset.Parse(lines.Last(), CultureInfo.CurrentCulture);

Assert.True(actualTimestamp >= expectedTimestamp);
Assert.False(process.IsRunning);
Assert.Equal(-1, process.Id);
}
Expand All @@ -152,7 +176,14 @@ public async Task RunAsync_Runtime_TestAsync()
var dotnetPath = ProcessEnvironment.DotnetPath;
var executionPath = consoleApplicationFixture.ExecutionPath;
var process = new TestProcess(dotnetPath, executionPath);
using var outputCollector = new ProcessOutputCollector(process);
var expectedTimestamp = DateTimeOffset.UtcNow + TimeSpan.FromMilliseconds(1000);
await process.RunAsync(default);
var output = outputCollector.Output;
var lines = output.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var actualTimestamp = DateTimeOffset.Parse(lines.Last(), CultureInfo.CurrentCulture);

Assert.True(actualTimestamp >= expectedTimestamp);
Assert.False(process.IsRunning);
Assert.Equal(-1, process.Id);
}
Expand Down Expand Up @@ -213,11 +244,21 @@ public async Task RunAsync_TestId_TestAsync()
{
var dotnetPath = ProcessEnvironment.DotnetPath;
var executionPath = consoleApplicationFixture.ExecutionPath;
var process = new TestProcess(dotnetPath, executionPath, "1000");
var millisecondsDelay = Random.Shared.Next(1000, 2000);
var process = new TestProcess(dotnetPath, executionPath, $"{millisecondsDelay}");
using var outputCollector = new ProcessOutputCollector(process);
var expectedTimestamp
= DateTimeOffset.UtcNow + TimeSpan.FromMilliseconds(millisecondsDelay);
var task = process.RunAsync(default);

Assert.True(process.IsRunning);
Assert.NotEqual(-1, process.Id);
await task;

var output = outputCollector.Output;
var lines = output.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var actualTimestamp = DateTimeOffset.Parse(lines.Last(), CultureInfo.CurrentCulture);
Assert.True(actualTimestamp >= expectedTimestamp);
Assert.False(process.IsRunning);
Assert.Equal(-1, process.Id);
}
Expand Down

0 comments on commit 516bfd5

Please sign in to comment.