Skip to content
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

[DO NOT MERGE] Poor main attempt to battle thread safety #31

Closed
wants to merge 3 commits into from
Closed
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
66 changes: 66 additions & 0 deletions BoDi.Tests/ParallelBoDiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoDi.Tests
{
[TestFixture]
public class ParallelBoDiTests
{
private interface IBodiFactory
{
IObjectContainer Create(IObjectContainer parent = null);

}
private class BoDiSimpleFactory : IBodiFactory
{
public IObjectContainer Create(IObjectContainer parent = null) => new ObjectContainer(parent);
}

private static IObjectContainer[] Create(IBodiFactory factory)
{
var parent = factory.Create();
parent.RegisterTypeAs<object, object>().InstancePerContext();

IEnumerable<IObjectContainer> CreateChilds(int count)
{
for (var i = 1; i < count; i ++)
{
yield return factory.Create(parent);
}
}

return CreateChilds(50).ToArray();
}

[Test]
public async Task ShouldWorkWhenCreateParallely()
{
var containers = Create(new BoDiSimpleFactory());

var result = await Task.WhenAll(containers.Select(c => Task.Run(() => c.Resolve<object>())).ToArray());
AssertResultsAreSame(result);
}

private static void AssertResultsAreSame(object[] result)
{
var first = result.First();
foreach (var any in result)
{
Assert.AreSame(first, any);
}
}


[Test]
public void ShouldWorkWhenCreateSequentally()
{
var containers = Create(new BoDiSimpleFactory());
var result = containers.Select(c => c.Resolve<object>()).ToArray();
AssertResultsAreSame(result);
}
}
}
26 changes: 24 additions & 2 deletions BoDi/BoDi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,15 @@ private object ChangeType(string name, Type keyType)

private bool isDisposed = false;
private readonly ObjectContainer baseContainer;
protected object SyncRootForChilds = new object();

private readonly Dictionary<RegistrationKey, IRegistration> registrations = new Dictionary<RegistrationKey, IRegistration>();
private readonly List<RegistrationKey> resolvedKeys = new List<RegistrationKey>();
private readonly Dictionary<RegistrationKey, object> objectPool = new Dictionary<RegistrationKey, object>();

public event Action<object> ObjectCreated;
public IObjectContainer BaseContainer => baseContainer;


public ObjectContainer(IObjectContainer baseContainer = null)
{
Expand Down Expand Up @@ -778,7 +781,12 @@ private object Resolve(Type typeToResolve, ResolutionList resolutionPath, string
}

if (baseContainer != null)
return baseContainer.GetRegistrationResult(keyToResolve);
{
lock (baseContainer.SyncRootForChilds)
{
return baseContainer.GetRegistrationResult(keyToResolve);
}
}

if (IsSpecialNamedInstanceDictionaryKey(keyToResolve))
{
Expand Down Expand Up @@ -845,7 +853,21 @@ private object ResolveObject(RegistrationKey keyToResolve, ResolutionList resolu

var resolutionPathForResolve = registrationToUse.Key == this ?
resolutionPath : new ResolutionList();
var result = registrationToUse.Value.Resolve(registrationToUse.Key, keyToResolve, resolutionPathForResolve);

object result;

if (registrationToUse.Key == this)
{
result = registrationToUse.Value.Resolve(registrationToUse.Key, keyToResolve, resolutionPathForResolve);
}
else
{
lock (registrationToUse.Key.SyncRootForChilds)
{
result = registrationToUse.Value.Resolve(registrationToUse.Key, keyToResolve, resolutionPathForResolve);
}
}

if (isImplicitTypeRegistration) // upon successful implicit registration, we register the rule, so that sub context can also get the same resolved value
AddRegistration(keyToResolve, registrationToUse.Value);
return result;
Expand Down