forked from castleproject/Windsor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test for castleproject#646: resolve from thread pool causes null refe…
…rence in scope
- Loading branch information
1 parent
557df01
commit 36db480
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/Castle.Windsor.Extensions.DependencyInjection.Tests/ResolveFromThreadpoolUnsafe.cs
This file contains 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,56 @@ | ||
using Castle.MicroKernel.Registration; | ||
using Castle.Windsor.Extensions.DependencyInjection.Tests.Components; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Castle.Windsor.Extensions.DependencyInjection.Tests { | ||
public class ResolveFromThreadpoolUnsafe { | ||
[Fact] | ||
public async Task Can_Resolve_From_ServiceProvider() { | ||
|
||
var serviceProvider = new ServiceCollection(); | ||
var container = new WindsorContainer(); | ||
var f = new WindsorServiceProviderFactory(container); | ||
f.CreateBuilder(serviceProvider); | ||
|
||
container.Register( | ||
Component.For<IUserService>().ImplementedBy<UserService>() | ||
); | ||
|
||
IServiceProvider sp = f.CreateServiceProvider(container); | ||
|
||
var actualUserService = sp.GetService<IUserService>(); | ||
Assert.NotNull(actualUserService); | ||
|
||
/* | ||
ThreadPool.UnsafeQueueUserWorkItem(state => { | ||
// resolving using castle (without scopes) works | ||
var actualUserService = container.Resolve<IUserService>(nameof(UserService)); | ||
Assert.NotNull(actualUserService); | ||
}, null); | ||
*/ | ||
|
||
TaskCompletionSource<IUserService> tcs = new TaskCompletionSource<IUserService>(); | ||
|
||
ThreadPool.UnsafeQueueUserWorkItem(state => { | ||
try { | ||
var actualUserService = sp.GetService<IUserService>(); | ||
Assert.NotNull(actualUserService); | ||
} | ||
catch (Exception ex) { | ||
tcs.SetException(ex); | ||
return; | ||
} | ||
tcs.SetResult(actualUserService); | ||
}, null); | ||
|
||
// Wait for the work item to complete. | ||
var task = tcs.Task; | ||
IUserService result = await task; | ||
Assert.NotNull(result); | ||
} | ||
} | ||
} |