-
Notifications
You must be signed in to change notification settings - Fork 121
Мелкие исправления потокобезопасности #1770
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
Open
sfaqer
wants to merge
1
commit into
EvilBeaver:develop
Choose a base branch
from
sfaqer:bugfix/small-thread-safety
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−32
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /*---------------------------------------------------------- | ||
| This Source Code Form is subject to the terms of the | ||
| Mozilla Public License, v.2.0. If a copy of the MPL | ||
| was not distributed with this file, You can obtain one | ||
| at http://mozilla.org/MPL/2.0/. | ||
| ----------------------------------------------------------*/ | ||
|
|
||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using FluentAssertions; | ||
| using Moq; | ||
| using OneScript.StandardLibrary.Binary; | ||
| using ScriptEngine.HostedScript; | ||
| using Xunit; | ||
|
|
||
| namespace OneScript.Core.Tests | ||
| { | ||
| public class TemplateStorageTests | ||
| { | ||
| [Fact] | ||
| public void TemplatesRegisteredFromManyThreadsAreKept() | ||
| { | ||
| const int threadsCount = 8; | ||
| const int templatesPerThread = 500; | ||
| var storage = new TemplateStorage(Mock.Of<ITemplateFactory>()); | ||
| var errors = new ConcurrentQueue<Exception>(); | ||
|
|
||
| // Так регистрируют макеты библиотеки, загружаемые из разных заданий | ||
| var threads = Enumerable.Range(0, threadsCount).Select(number => new Thread(() => | ||
| { | ||
| try | ||
| { | ||
| for (var i = 0; i < templatesPerThread; i++) | ||
| { | ||
| storage.RegisterTemplate($"Макет{number}_{i}", new StubTemplate()); | ||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| errors.Enqueue(e); | ||
| } | ||
| })).ToArray(); | ||
|
|
||
| foreach (var thread in threads) | ||
| { | ||
| thread.Start(); | ||
| } | ||
| foreach (var thread in threads) | ||
| { | ||
| thread.Join(); | ||
| } | ||
|
|
||
| errors.Should().BeEmpty(); | ||
| storage.GetTemplates().Should().HaveCount(threadsCount * templatesPerThread); | ||
| } | ||
|
|
||
| private class StubTemplate : ITemplate | ||
| { | ||
| public string GetFilename() => ""; | ||
|
|
||
| public BinaryDataContext GetBinaryData() => throw new NotSupportedException(); | ||
|
|
||
| public TemplateKind Kind => TemplateKind.File; | ||
|
|
||
| public void Dispose() | ||
| { | ||
| } | ||
| } | ||
| } | ||
| } |
101 changes: 101 additions & 0 deletions
101
src/Tests/OneScript.DebugProtocol.Test/BreakpointManagerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /*---------------------------------------------------------- | ||
| This Source Code Form is subject to the terms of the | ||
| Mozilla Public License, v.2.0. If a copy of the MPL | ||
| was not distributed with this file, You can obtain one | ||
| at http://mozilla.org/MPL/2.0/. | ||
| ----------------------------------------------------------*/ | ||
|
|
||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using FluentAssertions; | ||
| using OneScript.DebugServices; | ||
| using Xunit; | ||
|
|
||
| namespace OneScript.DebugProtocol.Test | ||
| { | ||
| public class BreakpointManagerTests | ||
| { | ||
| [Fact] | ||
| public void ConditionOfRemovedBreakpointIsNull() | ||
| { | ||
| var manager = new DefaultBreakpointManager(); | ||
| manager.SetBreakpoints("module.os", new[] { (10, "А = 1") }); | ||
| manager.FindBreakpoint("module.os", 10).Should().BeTrue(); | ||
|
|
||
| // Отладчик снял точку между проверкой строки и чтением условия | ||
| manager.Clear(); | ||
|
|
||
| manager.GetCondition("module.os", 10).Should().BeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void BreakpointsCanBeCheckedWhileDebuggerChangesThem() | ||
| { | ||
| const string module = "module.os"; | ||
| var manager = new DefaultBreakpointManager(); | ||
| var errors = new ConcurrentQueue<Exception>(); | ||
| var stop = false; | ||
| const int scriptThreadsCount = 4; | ||
| using var readersRunning = new CountdownEvent(scriptThreadsCount); | ||
|
|
||
| // Потоки скриптов проверяют точки на каждой строке | ||
| var scriptThreads = Enumerable.Range(0, scriptThreadsCount).Select(_ => new Thread(() => | ||
| { | ||
| var signaled = false; | ||
| try | ||
| { | ||
| while (!Volatile.Read(ref stop)) | ||
| { | ||
| for (var line = 1; line <= 20; line++) | ||
| { | ||
| if (manager.FindBreakpoint(module, line)) | ||
| manager.GetCondition(module, line); | ||
| } | ||
| manager.StopOnAnyException("ошибка"); | ||
|
|
||
| if (!signaled) | ||
| { | ||
| signaled = true; | ||
| readersRunning.Signal(); | ||
| } | ||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| errors.Enqueue(e); | ||
| } | ||
| finally | ||
| { | ||
| if (!signaled) | ||
| readersRunning.Signal(); | ||
| } | ||
| }) { IsBackground = true }).ToArray(); | ||
|
|
||
| foreach (var thread in scriptThreads) | ||
| { | ||
| thread.Start(); | ||
| } | ||
|
|
||
| // Поток отладчика меняет точки, когда все потоки скриптов уже проверяют их | ||
| var readersStarted = readersRunning.Wait(TimeSpan.FromSeconds(10)); | ||
| var breakpoints = Enumerable.Range(1, 20).Select(line => (line, "")).ToArray(); | ||
| for (var i = 0; i < 20000; i++) | ||
| { | ||
| manager.SetBreakpoints(module, breakpoints); | ||
| manager.SetExceptionBreakpoints(new[] { ("all", "ошибка") }); | ||
| manager.Clear(); | ||
| } | ||
|
|
||
| Volatile.Write(ref stop, true); | ||
| foreach (var thread in scriptThreads) | ||
| { | ||
| thread.Join(); | ||
| } | ||
|
|
||
| readersStarted.Should().BeTrue(); | ||
| errors.Should().BeEmpty(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Что тут хотел сказать Сонар? Вроде бы это как раз и есть требуемое поведение