Skip to content
Open
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
23 changes: 23 additions & 0 deletions Homework3.Tests/Homework3.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Homework3\Homework3.csproj" />
</ItemGroup>
</Project>
132 changes: 132 additions & 0 deletions Homework3.Tests/MyTreadPoolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
namespace Homework3.Tests;

class Tests
{
readonly int amountOfThreads = 5;
MyThreadPool threadPool;
Comment on lines +5 to +6
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо модификаторы видимости

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не поправлено


[SetUp]
public void SetUp()
{
threadPool = new(amountOfThreads);
}

[Test]
public void MultiThreadThreadPoolRequest()
{
var threads = new Thread[amountOfThreads];
var tasks = new IMyTask<int>[amountOfThreads];
ManualResetEvent manualResetEvent = new(false);
for (int i = 0; i < amountOfThreads; i++)
{
var localI = i;
threads[i] = new Thread(() =>
{
tasks[localI] = threadPool.Submit(() =>
{
manualResetEvent.WaitOne();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нам было бы интереснее попробовать Submit делать одновременно, а не задачи, уже поставленные на пул, одновременно стартовать

Thread.Sleep(1000);
return 2 * 2;
});
}
);
}

foreach (var thread in threads)
{
thread.Start();
}

manualResetEvent.Set();

foreach (var thread in threads)
{
thread.Join();
}

for (int i = 0; i < amountOfThreads; i++)
{
Assert.That(tasks[i].Result(), Is.EqualTo(4));
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Много потоков, делающих Submit, проверяется, но единственный ли это источник потенциальных гонок в этой задаче?


[Test]
public void ShutdownTest()
{
var myTask1 = threadPool.Submit(() => 2 * 2)!.ContinueWith(x => x.ToString());
Thread.Sleep(1000);
threadPool.Shutdown();
Thread.Sleep(100);
Assert.Throws<InvalidOperationException>(() => threadPool.Submit(() => 2 * 2)!.ContinueWith(x => x.ToString()));
}

[Test]
public void SubmitTest()
{
var myTask = threadPool.Submit(() => 2 * 2)!;
Thread.Sleep(1000);
Assert.Multiple(() =>
{
Assert.That(myTask.IsCompleted(), Is.EqualTo(true));
Assert.That(myTask.Result(), Is.EqualTo(4));
});
}

[Test]
public void SubmitPlusContinueWithTest()
{
var myTask = threadPool.Submit(() => 2 * 2)!.ContinueWith(x => x.ToString());
Thread.Sleep(1000);
Assert.Multiple(() =>
{
Assert.That(myTask.IsCompleted(), Is.EqualTo(true));
Assert.That(myTask.Result(), Is.EqualTo("4"));
});
}

[Test]
public void ExceptionTest()
{
var myTask = threadPool.Submit(object () =>
{
throw new InvalidOperationException();
});
Assert.Throws<AggregateException>(() => myTask!.Result());
}

[Test]
public void ThreeContinueWithTest()
{
var myTask = threadPool.Submit(() => 2 * 2)!.ContinueWith(x => x.ToString()).ContinueWith(y => y + " ");
Thread.Sleep(1000);
Assert.Multiple(() =>
{
Assert.That(myTask.IsCompleted(), Is.EqualTo(true));
Assert.That(myTask.Result(), Is.EqualTo("4 "));
});
}

[Test]
public void ThereNThreadsInMyThreadPoolTest()
{
var threadsId = new int[amountOfThreads];
for (int i = 0; i < amountOfThreads; i++)
{
int localI = i;
threadPool.Submit(() =>
{
threadsId[localI] = Environment.CurrentManagedThreadId;
Thread.Sleep(1000);
return 2 * 2;
});
}
Dictionary<int, int> dict = new();
for (int i = 0; i < amountOfThreads; i++)
{
dict.Add(i, threadsId[i]);
}
var values = dict.Select(x => x.Value).ToArray();

Assert.That(values.Count, Is.EqualTo(amountOfThreads));
}
}

This comment was marked as resolved.

1 change: 1 addition & 0 deletions Homework3.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
16 changes: 16 additions & 0 deletions Homework3/EternalTaskException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

/// <summary>
/// Thrown if the task hasn't been completed for some exact time after Shutdown.
/// </summary>
public class EternalTaskException : Exception
{
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
/// <param name="message">Error message.</param>
public EternalTaskException(string message)
: base(message)
{
}
}

9 changes: 9 additions & 0 deletions Homework3/Homework3.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
31 changes: 31 additions & 0 deletions Homework3/Homework3.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1706.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Homework3", "Homework3.csproj", "{6CD95AEA-46EF-4C7C-B3E3-C4B948912D59}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Homework3.Tests", "..\Homework3.Tests\Homework3.Tests.csproj", "{19B0F252-AE39-4460-A8C3-ACD58D24B58B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6CD95AEA-46EF-4C7C-B3E3-C4B948912D59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6CD95AEA-46EF-4C7C-B3E3-C4B948912D59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CD95AEA-46EF-4C7C-B3E3-C4B948912D59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CD95AEA-46EF-4C7C-B3E3-C4B948912D59}.Release|Any CPU.Build.0 = Release|Any CPU
{19B0F252-AE39-4460-A8C3-ACD58D24B58B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{19B0F252-AE39-4460-A8C3-ACD58D24B58B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{19B0F252-AE39-4460-A8C3-ACD58D24B58B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{19B0F252-AE39-4460-A8C3-ACD58D24B58B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {44148E15-051F-4366-A340-D677BCF6B98B}
EndGlobalSection
EndGlobal
29 changes: 29 additions & 0 deletions Homework3/IMyTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Homework3;

using static Homework3.MyThreadPool;

/// <summary>
/// interface for MyTask class.
/// </summary>
public interface IMyTask<T>

This comment was marked as resolved.

{
/// <summary>
/// Returns true, if task is completed.
/// </summary>
/// <returns></returns>
public bool IsCompleted();

/// <summary>
/// Return result of the task.
/// </summary>
/// <returns></returns>
public T? Result();

/// <summary>
/// Return new task which operates with the previous result.
/// </summary>
/// <typeparam name="T2"></typeparam>
/// <param name="continueFunction"></param>
/// <returns></returns>
Comment on lines +25 to +27
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пустые тэги не нужны

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не поправлено

public MyTask<T2> ContinueWith<T2>(Func<T?, T2> continueFunction);
}
Loading