Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. private bool _initCalled = false;
  2. private void Initialize()
  3. {
  4. if (!_initCalled)
  5. {
  6. lock (this)
  7. {
  8. if (_initCalled)
  9. return;
  10. NoConfigInit();
  11. _initCalled = true;
  12. }
  13. }
  14. }
  15.  
  16. public class Class3
  17. {
  18. private bool _initCalled = false;
  19. public void Initialize()
  20. {
  21. if (!_initCalled)
  22. {
  23. lock (this)
  24. {
  25. // we need to insert an empty DummyMethod here
  26. DummyMethod();
  27. if (_initCalled)
  28. return;
  29. NoConfigInit();
  30. _initCalled = true;
  31. }
  32. }
  33. }
  34.  
  35. private void DummyMethod()
  36. {
  37. // This method stays empty in production code.
  38. // It provides the hook for the unit test.
  39. }
  40.  
  41. private void NoConfigInit()
  42. {
  43.  
  44. }
  45. }
  46.  
  47. [TestMethod]
  48. public void TestMethod1()
  49. {
  50. using (ShimsContext.Create())
  51. {
  52. // This is the value to capture whether the NoConfigInit was called
  53. var initCalled = false;
  54.  
  55. // Here the DummyMethod comes into play
  56. Namespace.Fakes.ShimClass3.AllInstances.DummyMethod =
  57. class3 =>
  58. typeof(Class3).GetField("_initCalled", BindingFlags.Instance | BindingFlags.NonPublic)
  59. .SetValue(class3, true);
  60.  
  61. // This is just a hook to check whether the NoConfigInit is called.
  62. // You may be able to test this using some other ways (e.g. asserting on state etc.)
  63. Namespace.Fakes.ShimClass3.AllInstances.NoConfigInit = class3 => initCalled = true;
  64.  
  65. // act
  66. new Class3().Initialize();
  67.  
  68. // assert
  69. Assert.IsFalse(initCalled);
  70. }
  71. }
  72.  
  73. int calls = 0;
  74. Namespace.Fakes.ShimClass3.AllInstances.InitCalledGet = class3 => calls++ > 0;
  75.  
  76. using System.Threading.Tasks;
  77. using FakeItEasy;
  78. using Xunit;
  79.  
  80. namespace Rovio.Beacon.Cloud.Client.Tests
  81. {
  82. public class MyClass
  83. {
  84. private readonly object _lock = new object();
  85. private readonly IMyInterface _noConfig;
  86. private bool _initCalled;
  87.  
  88. public MyClass(IMyInterface noConfig)
  89. {
  90. _noConfig = noConfig;
  91. }
  92.  
  93. public void Initialize()
  94. {
  95. if (_initCalled)
  96. {
  97. return;
  98. }
  99.  
  100. lock (_lock)
  101. {
  102. if (_initCalled)
  103. {
  104. return;
  105. }
  106.  
  107. _noConfig.Init();
  108. _initCalled = true;
  109. }
  110. }
  111. }
  112.  
  113. public interface IMyInterface
  114. {
  115. void Init();
  116. }
  117.  
  118. public class MyTests
  119. {
  120. private readonly IMyInterface _myInterface;
  121. private readonly MyClass _myClass;
  122.  
  123. public MyTests()
  124. {
  125. _myInterface = A.Fake<IMyInterface>();
  126. _myClass = new MyClass(_myInterface);
  127. }
  128.  
  129. [Fact]
  130. public async void Initialize_CallInitializeConcurrently_InitNoConfigOnlyCalledOnce()
  131. {
  132. Task[] tasks =
  133. {
  134. Task.Run(() => _myClass.Initialize()),
  135. Task.Run(() => _myClass.Initialize())
  136. };
  137. await Task.WhenAll(tasks);
  138.  
  139. A.CallTo(() => _myInterface.Init()).MustHaveHappenedOnceExactly();
  140. }
  141.  
  142. [Fact]
  143. public void Initialize_CallInitializeConsecutively_InitNoConfigOnlyCalledOnce()
  144. {
  145. _myClass.Initialize();
  146. _myClass.Initialize();
  147.  
  148. A.CallTo(() => _myInterface.Init()).MustHaveHappenedOnceExactly();
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement