Skip to content

Commit

Permalink
Код отформатирован. (#8)
Browse files Browse the repository at this point in the history
Испрален баг при создании Processor через IoC, из-за которого создавалось два процессора, работающего с одной очередью вместо одного. с созданием двух процессоров, которые читают из одной очереди.

В итоге тест, в некоторых случаях не проходил, так как два процессора читали сообщения из одной очереди. При этом тест успешно завершался, только если первый созданный процессор получал сообщение HardStopCommand, а при работе двух процессоров иногда HardStopCommand уходило в другой процессор.

Co-authored-by: etyumemntcev <[email protected]>
  • Loading branch information
etyumentcev and etyumemntcev authored Sep 5, 2023
1 parent 8894f3e commit d362af9
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 23 deletions.
4 changes: 2 additions & 2 deletions appserver/core.tests/IocTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public void IocShouldUpdateResolveDependencyStrategy()

Ioc.Resolve<ICommand>(
"Update Ioc Resolve Dependency Strategy",
(Func<string, object[], object> args) =>
(Func<string, object[], object> args) =>
{
wasCalled = true;
return args;
}
).Execute();

Assert.True( wasCalled );
Assert.True(wasCalled);
}

[Fact]
Expand Down
10 changes: 4 additions & 6 deletions appserver/core/Ioc.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using System;

namespace AppServer
namespace AppServer
{
/// <summary>
/// Контейнер инверсии зависимотей (Расширяемая фабрика).
/// </summary>
public class Ioc
{
internal static Func<string, object[], object> _strategy =
(string dependency, object[]args) =>
(string dependency, object[] args) =>
{
if ("Update Ioc Resolve Dependency Strategy" == dependency)
{
return new AppServer.Impl.UpdateIocResolveDependencyStrategyCommand(
(Func < Func<string, object[], object>, Func<string, object[], object> >) args[0]
(Func<Func<string, object[], object>, Func<string, object[], object>>)args[0]
);
}
else
Expand Down Expand Up @@ -45,7 +43,7 @@ public class Ioc
/// </returns>
public static T Resolve<T>(string dependency, params object[] args)
{
return (T) _strategy(dependency, args);
return (T)_strategy(dependency, args);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ internal class UpdateIocResolveDependencyStrategyCommand : ICommand

public UpdateIocResolveDependencyStrategyCommand(
Func<Func<string, object[], object>, Func<string, object[], object>> updater
) => _updateIoCStrategy = updater;
)
{
_updateIoCStrategy = updater;
}

public void Execute()
{
Expand Down
8 changes: 4 additions & 4 deletions appserver/scopes.tests/ScopesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ namespace AppServer.Scopes
{
public class Tests : IDisposable
{
public Tests()
{
public Tests()
{
new InitCommand().Execute();
var iocScope = Ioc.Resolve<object>("IoC.Scope.Create");
Ioc.Resolve<ICommand>("IoC.Scope.Current.Set", iocScope).Execute();
Expand All @@ -12,7 +12,7 @@ public Tests()
[Fact]
public void Ioc_Should_Resolve_Registered_Dependency_In_CurrentScope()
{
Ioc.Resolve<ICommand>("IoC.Register", "someDependency", (object[] args) => (object) 1).Execute();
Ioc.Resolve<ICommand>("IoC.Register", "someDependency", (object[] args) => (object)1).Execute();

Assert.Equal(1, Ioc.Resolve<int>("someDependency"));
}
Expand All @@ -26,7 +26,7 @@ public void Ioc_Should_Throw_Exception_On_Unregistered_Dependency_In_CurrentScop
[Fact]
public void Ioc_Should_Use_Parent_Scope_If_Resolving_Dependency_Is_Not_Defined_In_Current_Scope()
{
Ioc.Resolve<ICommand>("IoC.Register", "someDependency", (object[] args) => (object) 1).Execute();
Ioc.Resolve<ICommand>("IoC.Register", "someDependency", (object[] args) => (object)1).Execute();

var parentIoCScope = Ioc.Resolve<object>("IoC.Scope.Current");

Expand Down
7 changes: 5 additions & 2 deletions appserver/scopes/DependencyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ public class DependencyResolver : IDependencyResolver
{
IDictionary<string, Func<object[], object>> _dependencies;

public DependencyResolver(object scope) => _dependencies = (IDictionary<string, Func<object[], object>>)scope;
public DependencyResolver(object scope)
{
_dependencies = (IDictionary<string, Func<object[], object>>)scope;
}

public object Resolve(string dependency, object[] args)
{
Expand All @@ -19,7 +22,7 @@ public object Resolve(string dependency, object[] args)
}
else
{
dependencies = (IDictionary<string, Func<object[], object>>)(dependencies["IoC.Scope.Parent"](args));
dependencies = (IDictionary<string, Func<object[], object>>)dependencies["IoC.Scope.Parent"](args);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions appserver/scopes/InitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace AppServer.Scopes
{
public class InitCommand : ICommand
{
internal static ThreadLocal<object> currentScopes =
internal static ThreadLocal<object> currentScopes =
new ThreadLocal<object>(true);

static ConcurrentDictionary<string, Func<object[], object>> rootScope =
static ConcurrentDictionary<string, Func<object[], object>> rootScope =
new ConcurrentDictionary<string, Func<object[], object>>();

static bool _alreadyExecutesScuccessfully = false;
public void Execute()
public void Execute()
{
if (_alreadyExecutesScuccessfully)
return;
Expand Down Expand Up @@ -49,7 +49,7 @@ public void Execute()
{
var creatingScope = Ioc.Resolve<IDictionary<string, Func<object[], object>>>("IoC.Scope.Create.Empty");

if(args.Length > 0)
if (args.Length > 0)
{
var parentScope = args[0];
creatingScope.Add("IoC.Scope.Parent", (object[] args) => parentScope);
Expand Down
6 changes: 5 additions & 1 deletion appserver/scopes/SetCurrentScopeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ public class SetCurrentScopeCommand : ICommand
{
object _scope;

public SetCurrentScopeCommand(object scope) => _scope = scope;
public SetCurrentScopeCommand(object scope)
{
_scope = scope;
}

public void Execute()
{
InitCommand.currentScopes.Value = _scope;
Expand Down
1 change: 0 additions & 1 deletion appserver/server.tests/ProcessorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Moq;
using AppServer.Scopes;
using System.Collections.Concurrent;

namespace AppServer.Commands.Tests
Expand Down
1 change: 0 additions & 1 deletion appserver/server/RegisterProcessorCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public void Execute()
IDictionary<string, object> context = new Dictionary<string, object>();
new InitProcessorContextCommand(context).Execute();
context.Add("processor", new Processor(new Processable(context)));
new Processor(new Processable(context));
return context;
}).Execute();
}
Expand Down

0 comments on commit d362af9

Please sign in to comment.