-
Notifications
You must be signed in to change notification settings - Fork 0
Добавила решение 3 домашней работы #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
ba47c8f
a020537
66cc5da
1a2d04d
2e62337
2788e4c
ed1d3b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> |
| 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; | ||
|
|
||
| [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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Sorry, something went wrong. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| global using NUnit.Framework; |
| 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) | ||
| { | ||
| } | ||
| } | ||
|
|
| 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> |
| 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 |
| 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.
Sorry, something went wrong. |
||
| { | ||
| /// <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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Пустые тэги не нужны There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не поправлено |
||
| public MyTask<T2> ContinueWith<T2>(Func<T?, T2> continueFunction); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Надо модификаторы видимости
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не поправлено