Skip to content

Commit

Permalink
added the failing test for #116
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Mar 16, 2023
1 parent a54b568 commit 13ab4e9
Showing 1 changed file with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Text;
using System.Threading.Tasks;

using NUnit.Framework;

namespace DryIoc.IssuesTests
{
// [TestFixture]
public class GHIssue116_ReOpened_DryIoc_Resolve_with_decorators_goes_wrong_for_parallel_execution : ITest
{
public int Run()
{
DryIoc_Resolve_parallel_execution_on_repeat(10).GetAwaiter().GetResult();
return 1;
}

interface IQuery<T> { }
class Query<T> : IQuery<T> { };
class QueryDecorator<T> : IQuery<T>
{
public QueryDecorator(IQuery<T> decoratee) { }
}

public async Task DryIoc_Resolve_parallel_execution_on_repeat(int repeatCount)
{
for (var i = 0; i < repeatCount; i++)
await DryIoc_Resolve_parallel_execution();
}

// [Test, Repeat(10)]
public async Task DryIoc_Resolve_parallel_execution()
{
var container = new Container();

container.Register(typeof(IQuery<string>), typeof(Query<string>));
container.Register(typeof(IQuery<string>), typeof(QueryDecorator<string>), setup: Setup.Decorator);

IQuery<string> ResolveInScope()
{
using (var scope = container.OpenScope())
{
return scope.Resolve<IQuery<string>>();
}
}

const int tasksCount = 32;

var tasks = new Task<IQuery<string>>[tasksCount];
for (var i = 0; i < tasks.Length; i++)
tasks[i] = Task.Run(() => ResolveInScope());

await Task.WhenAll(tasks);

var failed = false;
var sb = new StringBuilder(tasks.Length);
for (var i = 0; i < tasks.Length; i++)
{
var success = tasks[i].Result is QueryDecorator<string>;
if (!success) failed = true;
sb.Append(success ? '_' : 'F');
}

Assert.IsFalse(failed, $"Some tasks are failed [{sb}]");
}
}
}

0 comments on commit 13ab4e9

Please sign in to comment.