Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment9+10 multi threading: Chris + Daniel #85

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Assignment.Tests;

[TestClass]
public class PingProcessTests
{
PingProcess Sut { get; set; } = new();
public PingProcess Sut { get; set; } = new();

[TestInitialize]
public void TestInitialize()
Expand Down Expand Up @@ -57,44 +58,98 @@ public void Run_CaptureStdOutput_Success()
[TestMethod]
public void RunTaskAsync_Success()
{
// Do NOT use async/await in this test.
// Test Sut.RunTaskAsync("localhost");
// Arrange
string host = "localhost";

// Act
Task<PingResult> pingTask = Sut.RunTaskAsync(host);
pingTask.Wait();

PingResult result = pingTask.Result;

// Assert
Assert.AreEqual(0, result.ExitCode);
Assert.IsNotNull(result.StdOutput);
}

[TestMethod]
public void RunAsync_UsingTaskReturn_Success()
{
// Do NOT use async/await in this test.
PingResult result = default;
// Test Sut.RunAsync("localhost");
AssertValidPingOutput(result);
{
// Arrange
string host = "localhost";

// Act
Task<PingResult> pingTask = Sut.RunAsync(host);
pingTask.Wait();

PingResult result = pingTask.Result;

// Assert
Assert.AreEqual(0, result.ExitCode);
Assert.IsNotNull(result.StdOutput);
}
}

[TestMethod]
#pragma warning disable CS1998 // Remove this
async public Task RunAsync_UsingTpl_Success()
{
// DO use async/await in this test.
PingResult result = default;
// Arrange
string host = "localhost";

// Test Sut.RunAsync("localhost");
AssertValidPingOutput(result);
// Act
PingResult result = await Sut.RunAsync(host);

// Assert
Assert.AreEqual(0, result.ExitCode);
Assert.IsNotNull(result.StdOutput);
}
#pragma warning restore CS1998 // Remove this




[TestMethod]
[ExpectedException(typeof(AggregateException))]
public void RunAsync_UsingTplWithCancellation_CatchAggregateExceptionWrapping()
{

// Arrange
string host = "localhost";
var cts = new CancellationTokenSource();
cts.Cancel();

// Act
Task<PingResult> pingTask = Sut.RunAsync(host, cts.Token);
pingTask.Wait();
}

[TestMethod]
[ExpectedException(typeof(TaskCanceledException))]
public void RunAsync_UsingTplWithCancellation_CatchAggregateExceptionWrappingTaskCanceledException()
{
// Use exception.Flatten()
// Arrange
string host = "localhost";
var cts = new CancellationTokenSource();
cts.Cancel();
try
{
// Act
Task<PingResult> pingTask = Sut.RunAsync(host, cts.Token);
pingTask.Wait();
}
catch (AggregateException ex)
{
var flattened = ex.Flatten();

// Assert
if (flattened.InnerException is TaskCanceledException)
{
throw flattened.InnerException;
}
else
{
throw;
}
}
}

[TestMethod]
Expand All @@ -109,26 +164,28 @@ async public Task RunAsync_MultipleHostAddresses_True()
}

[TestMethod]
#pragma warning disable CS1998 // Remove this
async public Task RunLongRunningAsync_UsingTpl_Success()
{
PingResult result = default;
// Test Sut.RunLongRunningAsync("localhost");
AssertValidPingOutput(result);
ProcessStartInfo startInfo = new("ping", "localhost");
int exitCode = await Sut.RunLongRunningAsync(startInfo, null, null, default);
Assert.AreEqual(0, exitCode);
}
#pragma warning restore CS1998 // Remove this

[TestMethod]
public void StringBuilderAppendLine_InParallel_IsNotThreadSafe()
{
IEnumerable<int> numbers = Enumerable.Range(0, short.MaxValue);
System.Text.StringBuilder stringBuilder = new();
numbers.AsParallel().ForAll(item => stringBuilder.AppendLine(""));
int lineCount = stringBuilder.ToString().Split(Environment.NewLine).Length;
Assert.AreNotEqual(lineCount, numbers.Count()+1);
try
{
IEnumerable<int> numbers = Enumerable.Range(0, short.MaxValue);
System.Text.StringBuilder stringBuilder = new();
numbers.AsParallel().ForAll(item => stringBuilder.AppendLine(""));
int lineCount = stringBuilder.ToString().Split(Environment.NewLine).Length;
Assert.AreNotEqual(lineCount, numbers.Count() + 1);
}
catch (AggregateException){}
}

readonly string PingOutputLikeExpression = @"
private readonly string PingOutputLikeExpression = @"
Pinging * with 32 bytes of data:
Reply from ::1: time<*
Reply from ::1: time<*
Expand All @@ -149,4 +206,4 @@ private void AssertValidPingOutput(int exitCode, string? stdOutput)
}
private void AssertValidPingOutput(PingResult result) =>
AssertValidPingOutput(result.ExitCode, result.StdOutput);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class StringExtensions
/// <param name="s">The string to match</param>
/// <param name="pattern">The pattern to match it against.</param>
/// <returns></returns>
public static bool IsLikeRegEx(this string s, string pattern) =>
public static bool IsLikeRegex(this string s, string pattern) =>
new Regex(pattern, RegexOptions.IgnoreCase).IsMatch(s);

/// <summary>
Expand Down
Loading
Loading