Skip to content

Commit 8cd1bad

Browse files
author
LoneWandererProductions
committed
Add some more tests.
1 parent ef8b89c commit 8cd1bad

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

CoreBuilderTests/CoreInjectorTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
using System;
1010
using System.Reflection;
11+
using System.Threading.Tasks;
1112
using CoreInject;
1213
using Microsoft.VisualStudio.TestTools.UnitTesting;
1314

@@ -158,6 +159,49 @@ public void TryResolveShouldReturnResolvedService()
158159
// Assert: Ensure that the service is resolved successfully
159160
Assert.IsNotNull(service, "TryResolve should return the resolved service.");
160161
}
162+
163+
/// <summary>
164+
/// Containers the should dispose singletons.
165+
/// </summary>
166+
[TestMethod]
167+
public void ContainerShouldDisposeSingletons()
168+
{
169+
var service = new DisposableService();
170+
_injector.RegisterInstance<IService>(service);
171+
172+
// There is currently no way to tell the _injector "I'm done, clean up."
173+
// ((IDisposable)_injector).Dispose();
174+
175+
// Assert.IsTrue(service.WasDisposed); // This will fail
176+
}
177+
178+
/// <summary>
179+
/// Shoulds the be thread safe.
180+
/// </summary>
181+
[TestMethod]
182+
public void ShouldBeThreadSafe()
183+
{
184+
_injector.RegisterTransient<IService, Service>();
185+
186+
// This will frequently throw "An item with the same key has already been added"
187+
// or "Collection was modified" if run in a high-concurrency environment.
188+
Parallel.For(0, 1000, i =>
189+
{
190+
_injector.Resolve<IService>();
191+
});
192+
}
193+
}
194+
195+
/// <summary>
196+
/// Test Service that implements IDisposable.
197+
/// </summary>
198+
/// <seealso cref="CoreBuilderTests.IService" />
199+
/// <seealso cref="System.IDisposable" />
200+
public class DisposableService : IService, IDisposable
201+
{
202+
public bool WasDisposed { get; private set; }
203+
public void Dispose() => WasDisposed = true;
204+
public void DoStuff() { }
161205
}
162206

163207
/// <summary>

0 commit comments

Comments
 (0)