PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Ninject.Test/Unit/CacheTests.cs

http://github.com/ninject/ninject
C# | 246 lines | 205 code | 41 blank | 0 comment | 2 complexity | 09b09a1348bbd2895ad8ac938425eabd MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. namespace Ninject.Tests.Unit.CacheTests
  2. {
  3. using System;
  4. using FluentAssertions;
  5. using Moq;
  6. using Ninject.Activation;
  7. using Ninject.Activation.Caching;
  8. using Ninject.Infrastructure.Disposal;
  9. using Ninject.Planning.Bindings;
  10. using Ninject.Tests.Fakes;
  11. using Xunit;
  12. public class CacheContext
  13. {
  14. protected Mock<ICachePruner> cachePrunerMock;
  15. protected IBindingConfiguration bindingConfiguration;
  16. protected Cache cache;
  17. protected Mock<IPipeline> pipelineMock;
  18. public CacheContext()
  19. {
  20. this.cachePrunerMock = new Mock<ICachePruner>();
  21. this.bindingConfiguration = new Mock<IBindingConfiguration>().Object;
  22. this.pipelineMock = new Mock<IPipeline>();
  23. this.cache = new Cache(this.pipelineMock.Object, this.cachePrunerMock.Object);
  24. }
  25. protected static IContext CreateContext(object scope, IBindingConfiguration bindingConfiguration, params Type[] genericArguments)
  26. {
  27. var contextMock = new Mock<IContext>();
  28. var bindingMock = new Mock<IBinding>();
  29. bindingMock.SetupGet(binding => binding.BindingConfiguration).Returns(bindingConfiguration);
  30. contextMock.SetupGet(context => context.Binding).Returns(bindingMock.Object);
  31. contextMock.Setup(context => context.GetScope()).Returns(scope);
  32. contextMock.SetupGet(context => context.GenericArguments).Returns(genericArguments);
  33. contextMock.SetupGet(context => context.HasInferredGenericArguments).Returns(genericArguments != null && genericArguments.Length > 0);
  34. return contextMock.Object;
  35. }
  36. }
  37. public class WhenTryGetInstanceIsCalled : CacheContext
  38. {
  39. [Fact]
  40. public void ReturnsNullIfNoInstancesHaveBeenAddedToCache()
  41. {
  42. var scope = new TestObject(42);
  43. var context = CreateContext(scope, this.bindingConfiguration);
  44. var instance = this.cache.TryGet(context);
  45. instance.Should().BeNull();
  46. }
  47. [Fact]
  48. public void ReturnsCachedInstanceIfOneHasBeenAddedWithinSpecifiedScope()
  49. {
  50. var scope = new TestObject(42);
  51. var reference = new InstanceReference { Instance = new Sword() };
  52. var context1 = CreateContext(scope, this.bindingConfiguration);
  53. var context2 = CreateContext(scope, this.bindingConfiguration);
  54. this.cache.Remember(context1, reference);
  55. object instance = this.cache.TryGet(context2);
  56. instance.Should().BeSameAs(reference.Instance);
  57. }
  58. [Fact]
  59. public void ReturnsNullIfNoInstancesHaveBeenAddedWithinSpecifiedScope()
  60. {
  61. var reference = new InstanceReference { Instance = new Sword() };
  62. var context1 = CreateContext(new TestObject(42), this.bindingConfiguration);
  63. var context2 = CreateContext(new TestObject(42), this.bindingConfiguration);
  64. this.cache.Remember(context1, reference);
  65. object instance = this.cache.TryGet(context2);
  66. instance.Should().BeNull();
  67. }
  68. [Fact]
  69. public void ReturnsNullIfScopeIsNull()
  70. {
  71. var reference = new InstanceReference { Instance = new Sword() };
  72. var context1 = CreateContext(new TestObject(42), this.bindingConfiguration);
  73. var context2 = CreateContext(null, this.bindingConfiguration);
  74. this.cache.Remember(context1, reference);
  75. object instance = this.cache.TryGet(context2);
  76. instance.Should().BeNull();
  77. }
  78. }
  79. public class WhenTryGetInstanceIsCalledForContextWithGenericInference : CacheContext
  80. {
  81. [Fact]
  82. public void ReturnsInstanceIfOneHasBeenCachedWithSameGenericParameters()
  83. {
  84. var scope = new TestObject(42);
  85. var reference = new InstanceReference { Instance = new Sword() };
  86. var context1 = CreateContext(scope, this.bindingConfiguration, typeof(int));
  87. var context2 = CreateContext(scope, this.bindingConfiguration, typeof(int));
  88. this.cache.Remember(context1, reference);
  89. object instance = this.cache.TryGet(context2);
  90. instance.Should().BeSameAs(reference.Instance);
  91. }
  92. [Fact]
  93. public void ReturnsNullIfInstanceAddedToCacheHasDifferentGenericParameters()
  94. {
  95. var scope = new TestObject(42);
  96. var reference = new InstanceReference { Instance = new Sword() };
  97. var context1 = CreateContext(scope, this.bindingConfiguration, typeof(int));
  98. var context2 = CreateContext(scope, this.bindingConfiguration, typeof(double));
  99. this.cache.Remember(context1, reference);
  100. object instance = this.cache.TryGet(context2);
  101. instance.Should().BeNull();
  102. }
  103. }
  104. public class WhenReleaseIsCalled : CacheContext
  105. {
  106. [Fact]
  107. public void ReturnsFalseIfInstanceIsNotTracked()
  108. {
  109. bool result = this.cache.Release(new TestObject(42));
  110. result.Should().BeFalse();
  111. }
  112. [Fact]
  113. public void ReturnsTrueIfInstanceIsTracked()
  114. {
  115. var scope = new TestObject(42);
  116. var instance = new Sword();
  117. var reference = new InstanceReference { Instance = instance };
  118. var writeContext = CreateContext(scope, this.bindingConfiguration, typeof(int));
  119. this.cache.Remember(writeContext, reference);
  120. bool result = this.cache.Release(instance);
  121. result.Should().BeTrue();
  122. }
  123. [Fact]
  124. public void InstanceIsRemovedFromCache()
  125. {
  126. var scope = new TestObject(42);
  127. var sword = new Sword();
  128. var reference = new InstanceReference { Instance = sword };
  129. var writeContext = CreateContext(scope, this.bindingConfiguration, typeof(int));
  130. var readContext = CreateContext(scope, this.bindingConfiguration, typeof(int));
  131. this.cache.Remember(writeContext, reference);
  132. object instance1 = this.cache.TryGet(readContext);
  133. bool result = this.cache.Release(instance1);
  134. object instance2 = this.cache.TryGet(readContext);
  135. instance1.Should().BeSameAs(reference.Instance);
  136. result.Should().BeTrue();
  137. instance2.Should().BeNull();
  138. }
  139. }
  140. public class WhenClearIsCalled : CacheContext
  141. {
  142. [Fact]
  143. public void WhenScopeIsDefinedItsEntriesAreReleased()
  144. {
  145. var scope = new TestObject(42);
  146. var sword = new Sword();
  147. var reference = new InstanceReference { Instance = sword };
  148. var context1 = CreateContext(scope, this.bindingConfiguration);
  149. var context2 = CreateContext(new TestObject(42), this.bindingConfiguration);
  150. this.cache.Remember(context1, reference);
  151. this.cache.Remember(context2, reference);
  152. this.cache.Clear(scope);
  153. var instance1 = this.cache.TryGet(context1);
  154. var instance2 = this.cache.TryGet(context2);
  155. instance1.Should().BeNull();
  156. instance2.Should().NotBeNull();
  157. }
  158. [Fact]
  159. public void WhenNoScopeIsDefinedAllEntriesAreReleased()
  160. {
  161. var sword = new Sword();
  162. var reference = new InstanceReference { Instance = sword };
  163. var context1 = CreateContext(new TestObject(42), this.bindingConfiguration);
  164. var context2 = CreateContext(new TestObject(42), this.bindingConfiguration);
  165. this.cache.Remember(context1, reference);
  166. this.cache.Remember(context2, reference);
  167. this.cache.Clear();
  168. var instance1 = this.cache.TryGet(context1);
  169. var instance2 = this.cache.TryGet(context2);
  170. instance1.Should().BeNull();
  171. instance2.Should().BeNull();
  172. }
  173. }
  174. public class WhenNotifiesWhenDisposedScopeIsDisposed : CacheContext
  175. {
  176. [Fact]
  177. public void CachedObjectsAreReleased()
  178. {
  179. var scopeMock = new Mock<INotifyWhenDisposed>();
  180. var sword = new Sword();
  181. var reference = new InstanceReference { Instance = sword };
  182. var context = CreateContext(scopeMock.Object, this.bindingConfiguration);
  183. this.cache.Remember(context, reference);
  184. scopeMock.Raise(scope => scope.Disposed += null, EventArgs.Empty);
  185. object instance = this.cache.TryGet(context);
  186. instance.Should().BeNull();
  187. }
  188. }
  189. public class WhenScopeIsReleasedFormCache : CacheContext
  190. {
  191. [Fact]
  192. public void CachedObjectsAreReleased()
  193. {
  194. var scope = new TestObject(42);
  195. var scopeOfScope = new TestObject(42);
  196. var sword = new Sword();
  197. var context = CreateContext(scope, this.bindingConfiguration);
  198. this.cache.Remember(context, new InstanceReference { Instance = sword });
  199. this.cache.Remember(CreateContext(scopeOfScope, this.bindingConfiguration), new InstanceReference { Instance = scope });
  200. this.cache.Clear(scopeOfScope);
  201. var instance = this.cache.TryGet(context);
  202. instance.Should().BeNull();
  203. }
  204. }
  205. }