|
8 | 8 |
|
9 | 9 | using System; |
10 | 10 | using System.Reflection; |
| 11 | +using System.Threading.Tasks; |
11 | 12 | using CoreInject; |
12 | 13 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
13 | 14 |
|
@@ -158,6 +159,49 @@ public void TryResolveShouldReturnResolvedService() |
158 | 159 | // Assert: Ensure that the service is resolved successfully |
159 | 160 | Assert.IsNotNull(service, "TryResolve should return the resolved service."); |
160 | 161 | } |
| 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() { } |
161 | 205 | } |
162 | 206 |
|
163 | 207 | /// <summary> |
|
0 commit comments