-
Notifications
You must be signed in to change notification settings - Fork 121
Компиляция во время загрузки библиотеки в другом потоке #1775
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/global-symbols-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.
+247
−57
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
87 changes: 87 additions & 0 deletions
87
src/Tests/OneScript.Core.Tests/GlobalSymbolsThreadSafetyTests.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,87 @@ | ||
| /*---------------------------------------------------------- | ||
| 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.Contexts; | ||
| using ScriptEngine.Hosting; | ||
| using ScriptEngine.Machine; | ||
| using ScriptEngine.Machine.Contexts; | ||
| using Xunit; | ||
|
|
||
| namespace OneScript.Core.Tests | ||
| { | ||
| [GlobalContext(ManualRegistration = true)] | ||
| public class GlobalSymbolsTestContext : GlobalContextBase<GlobalSymbolsTestContext> | ||
| { | ||
| } | ||
|
|
||
| public class GlobalSymbolsThreadSafetyTests | ||
| { | ||
| [Fact] | ||
| public void ScriptsCompileWhileGlobalSymbolsAreAdded() | ||
| { | ||
| var engine = DefaultEngineBuilder.Create().SetDefaultOptions().Build(); | ||
| engine.Initialize(); | ||
| engine.Environment.InjectGlobalProperty(ValueFactory.Create("есть"), "ИзвестноеСвойство", true); | ||
|
|
||
| // Много обращений к глобальному свойству: каждое ищет имя в общей области глобальных свойств | ||
| var code = string.Concat(Enumerable.Repeat("А = ИзвестноеСвойство;\n", 200)); | ||
| var errors = new ConcurrentQueue<string>(); | ||
| var stop = false; | ||
| var compiled = 0; | ||
| const int compilersCount = 4; | ||
| var compilers = Enumerable.Range(0, compilersCount).Select(_ => new Thread(() => | ||
| { | ||
| while (!Volatile.Read(ref stop)) | ||
| { | ||
| try | ||
| { | ||
| var source = engine.Loader.FromString(code); | ||
| engine.GetCompilerService().Compile(source, engine.NewProcess()); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| errors.Enqueue($"{e.GetType().Name}: {e.Message}"); | ||
| } | ||
| Interlocked.Increment(ref compiled); | ||
| } | ||
| }) { IsBackground = true }).ToArray(); | ||
|
|
||
| foreach (var thread in compilers) | ||
| { | ||
| thread.Start(); | ||
| } | ||
|
|
||
| // Компиляторы уже работают, когда начинается регистрация | ||
| SpinWait.SpinUntil(() => Volatile.Read(ref compiled) >= compilersCount, 10000).Should().BeTrue(); | ||
| var compiledBeforeRegistration = Volatile.Read(ref compiled); | ||
|
|
||
| // Как загрузка библиотеки и ПодключитьВнешнююКомпоненту в другом потоке | ||
| for (var i = 0; i < 30000; i++) | ||
| { | ||
| engine.Environment.InjectGlobalProperty(ValueFactory.Create(i), "Свойство" + i, true); | ||
| if (i % 1000 == 0) | ||
| engine.Environment.InjectObject(new GlobalSymbolsTestContext()); | ||
| } | ||
|
|
||
| var compiledDuringRegistration = Volatile.Read(ref compiled) - compiledBeforeRegistration; | ||
|
|
||
| Volatile.Write(ref stop, true); | ||
| foreach (var thread in compilers) | ||
| { | ||
| thread.Join(); | ||
| } | ||
|
|
||
| compiledDuringRegistration.Should().BePositive("компиляции должны идти вперемежку с регистрацией"); | ||
| errors.Should().BeEmpty(); | ||
| } | ||
| } | ||
| } |
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,3 @@ | ||
| Функция Привет() Экспорт | ||
| Возврат "Привет"; | ||
| КонецФункции |
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,8 @@ | ||
| Процедура ПриЗагрузкеБиблиотеки(Знач Путь, СтандартнаяОбработка, Отказ) | ||
|
|
||
| // Загрузка долгая: пока она идет, библиотеку запрашивают из других потоков | ||
| Приостановить(300); | ||
| СтандартнаяОбработка = Ложь; | ||
| ДобавитьМодуль(ОбъединитьПути(Путь, "module.os"), "МедленныйМодуль"); | ||
|
|
||
| КонецПроцедуры |
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.
Uh oh!
There was an error while loading. Please reload this page.