137 lines
3.8 KiB
C#
137 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Xunit;
|
|
using FluentAssertions;
|
|
using MontoyaTech.Parallel.Net;
|
|
|
|
namespace MontoyaTech.Parallel.Net.Tests
|
|
{
|
|
public class ParallelTests
|
|
{
|
|
[Fact]
|
|
public void Parallel_NoMaxTasks_Should_Work()
|
|
{
|
|
var inputs = new List<int>() { 0, 1, 2, 3, 4 };
|
|
|
|
var results = inputs.Parallel(input => input * 5);
|
|
|
|
results.Count.Should().Be(inputs.Count);
|
|
|
|
results.Should().Contain(0);
|
|
results.Should().Contain(5);
|
|
results.Should().Contain(10);
|
|
results.Should().Contain(15);
|
|
results.Should().Contain(20);
|
|
}
|
|
|
|
[Fact]
|
|
public void Parallel_MaxTasks_Should_Work()
|
|
{
|
|
var inputs = new List<int>() { 0, 1, 2, 3, 4 };
|
|
|
|
var results = inputs.Parallel(input => input * 5, maxTasks: 2);
|
|
|
|
results.Count.Should().Be(inputs.Count);
|
|
|
|
results.Should().Contain(0);
|
|
results.Should().Contain(5);
|
|
results.Should().Contain(10);
|
|
results.Should().Contain(15);
|
|
results.Should().Contain(20);
|
|
}
|
|
|
|
[Fact]
|
|
public void Parallel_MaxTasks_Zero_Ignore_Should_Work()
|
|
{
|
|
var inputs = new List<int>() { 0, 1, 2, 3, 4 };
|
|
|
|
var results = inputs.Parallel(input => input * 5, maxTasks: 0);
|
|
|
|
results.Count.Should().Be(inputs.Count);
|
|
|
|
results.Should().Contain(0);
|
|
results.Should().Contain(5);
|
|
results.Should().Contain(10);
|
|
results.Should().Contain(15);
|
|
results.Should().Contain(20);
|
|
}
|
|
|
|
[Fact]
|
|
public void Parallel_Timeout_Should_Work()
|
|
{
|
|
var inputs = new List<int>() { 0, 1, 2, 3, 4 };
|
|
|
|
var action = new Action(() => inputs.Parallel(input => input * 5, timeout: TimeSpan.FromSeconds(0)));
|
|
|
|
action.Should().Throw<Exception>();
|
|
}
|
|
|
|
[Fact]
|
|
public void Parallel_Timeout_NoException_Should_Work()
|
|
{
|
|
var inputs = new List<int>() { 0, 1, 2, 3, 4 };
|
|
|
|
var action = new Action(() => inputs.Parallel(input => input * 5, timeout: TimeSpan.FromSeconds(0), throwOnTimeout: false));
|
|
|
|
action.Should().NotThrow<Exception>();
|
|
}
|
|
|
|
[Fact]
|
|
public void Parallel_NoTimeout_Exception_Should_Work()
|
|
{
|
|
var inputs = new List<int>() { 0, 1, 2, 3, 4 };
|
|
|
|
var action = new Action(() => inputs.Parallel(input => input * 5));
|
|
|
|
action.Should().NotThrow<Exception>();
|
|
}
|
|
|
|
[Fact]
|
|
public void Parallel_Exception_Should_Bubble()
|
|
{
|
|
var inputs = new List<int>() { 0, 1, 2, 3, 4 };
|
|
|
|
var results = new List<int>();
|
|
|
|
var action = new Action(() => results = inputs.Parallel(input => {
|
|
if (input == 0)
|
|
throw new Exception("Testing");
|
|
|
|
return input * 5;
|
|
}));
|
|
|
|
action.Should().Throw<Exception>();
|
|
|
|
results.Count.Should().Be(0);
|
|
|
|
inputs.RemoveAt(0);
|
|
|
|
action.Should().NotThrow<Exception>();
|
|
|
|
results.Count.Should().BeGreaterThan(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void Parallel_Exception_NoBubble_Should_Work()
|
|
{
|
|
var inputs = new List<int>() { 0, 1, 2, 3, 4 };
|
|
|
|
var results = new List<int>();
|
|
|
|
var action = new Action(() => results = inputs.Parallel(input => {
|
|
if (input == 0)
|
|
throw new Exception("Testing");
|
|
|
|
return input * 5;
|
|
}, bubbleExceptions: false));
|
|
|
|
action.Should().NotThrow<Exception>();
|
|
|
|
results.Count.Should().BeGreaterThan(0);
|
|
}
|
|
}
|
|
}
|