HalfSerializerTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Reactive;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using Assert = Xunit.Assert;
  9. namespace ReactiveTests.Tests
  10. {
  11. [TestClass]
  12. public class HalfSerializerTest
  13. {
  14. private int _wip;
  15. private Exception _error;
  16. private readonly Consumer _consumer = new();
  17. [TestMethod]
  18. public void HalfSerializer_OnNext()
  19. {
  20. HalfSerializer.ForwardOnNext(_consumer, 1, ref _wip, ref _error);
  21. Assert.Equal(0, _wip);
  22. Assert.Null(_error);
  23. Assert.Equal(1, _consumer.Items.Count);
  24. Assert.Equal(1, _consumer.Items[0]);
  25. Assert.Equal(0, _consumer.Done);
  26. Assert.Null(_consumer.Exc);
  27. }
  28. [TestMethod]
  29. public void HalfSerializer_OnError()
  30. {
  31. var ex = new InvalidOperationException();
  32. HalfSerializer.ForwardOnError(_consumer, ex, ref _wip, ref _error);
  33. Assert.Equal(1, _wip);
  34. Assert.Equal(_error, ExceptionHelper.Terminated);
  35. HalfSerializer.ForwardOnNext(_consumer, 2, ref _wip, ref _error);
  36. Assert.Equal(0, _consumer.Items.Count);
  37. Assert.Equal(0, _consumer.Done);
  38. Assert.Equal(ex, _consumer.Exc);
  39. }
  40. [TestMethod]
  41. public void HalfSerializer_OnError_Ignore_Further_Events()
  42. {
  43. var ex = new InvalidOperationException();
  44. HalfSerializer.ForwardOnError(_consumer, ex, ref _wip, ref _error);
  45. Assert.Equal(1, _wip);
  46. Assert.Equal(_error, ExceptionHelper.Terminated);
  47. HalfSerializer.ForwardOnNext(_consumer, 2, ref _wip, ref _error);
  48. var ex2 = new NotSupportedException();
  49. HalfSerializer.ForwardOnError(_consumer, ex2, ref _wip, ref _error);
  50. HalfSerializer.ForwardOnCompleted(_consumer, ref _wip, ref _error);
  51. Assert.Equal(0, _consumer.Items.Count);
  52. Assert.Equal(0, _consumer.Done);
  53. Assert.Equal(ex, _consumer.Exc);
  54. }
  55. [TestMethod]
  56. public void HalfSerializer_OnCompleted()
  57. {
  58. HalfSerializer.ForwardOnCompleted(_consumer, ref _wip, ref _error);
  59. Assert.Equal(1, _wip);
  60. Assert.Equal(_error, ExceptionHelper.Terminated);
  61. HalfSerializer.ForwardOnNext(_consumer, 2, ref _wip, ref _error);
  62. Assert.Equal(0, _consumer.Items.Count);
  63. Assert.Equal(1, _consumer.Done);
  64. Assert.Null(_consumer.Exc);
  65. }
  66. [TestMethod]
  67. public void HalfSerializer_OnCompleted_Ignore_Further_Events()
  68. {
  69. HalfSerializer.ForwardOnCompleted(_consumer, ref _wip, ref _error);
  70. Assert.Equal(1, _wip);
  71. Assert.Equal(_error, ExceptionHelper.Terminated);
  72. HalfSerializer.ForwardOnNext(_consumer, 2, ref _wip, ref _error);
  73. var ex2 = new NotSupportedException();
  74. HalfSerializer.ForwardOnError(_consumer, ex2, ref _wip, ref _error);
  75. HalfSerializer.ForwardOnCompleted(_consumer, ref _wip, ref _error);
  76. Assert.Equal(0, _consumer.Items.Count);
  77. Assert.Equal(1, _consumer.Done);
  78. Assert.Null(_consumer.Exc);
  79. }
  80. // Practically simulates concurrent invocation of the HalfSerializer methods
  81. [TestMethod]
  82. public void HalfSerializer_OnNext_Reentrant_Error()
  83. {
  84. var c = new ReentrantConsumer(this, true);
  85. HalfSerializer.ForwardOnNext(c, 1, ref _wip, ref _error);
  86. Assert.Equal(1, _wip);
  87. Assert.Equal(_error, ExceptionHelper.Terminated);
  88. Assert.Equal(1, _consumer.Items.Count);
  89. Assert.Equal(1, _consumer.Items[0]);
  90. Assert.Equal(0, _consumer.Done);
  91. Assert.Equal(c.X, _consumer.Exc);
  92. }
  93. // Practically simulates concurrent invocation of the HalfSerializer methods
  94. [TestMethod]
  95. public void HalfSerializer_OnNext_Reentrant_OnCompleted()
  96. {
  97. var c = new ReentrantConsumer(this, false);
  98. HalfSerializer.ForwardOnNext(c, 1, ref _wip, ref _error);
  99. Assert.Equal(1, _wip);
  100. Assert.Equal(_error, ExceptionHelper.Terminated);
  101. Assert.Equal(1, _consumer.Items.Count);
  102. Assert.Equal(1, _consumer.Items[0]);
  103. Assert.Equal(1, _consumer.Done);
  104. Assert.Null(_consumer.Exc);
  105. }
  106. private sealed class Consumer : ISink<int>
  107. {
  108. internal List<int> Items = new();
  109. internal int Done;
  110. internal Exception Exc;
  111. public void ForwardOnCompleted()
  112. {
  113. Done++;
  114. }
  115. public void ForwardOnError(Exception error)
  116. {
  117. Exc = error;
  118. }
  119. public void ForwardOnNext(int value)
  120. {
  121. Items.Add(value);
  122. }
  123. }
  124. private sealed class ReentrantConsumer : ISink<int>
  125. {
  126. private readonly HalfSerializerTest _parent;
  127. private readonly bool _errorReenter;
  128. internal readonly Exception X = new IndexOutOfRangeException();
  129. public ReentrantConsumer(HalfSerializerTest parent, bool errorReenter)
  130. {
  131. _parent = parent;
  132. _errorReenter = errorReenter;
  133. }
  134. public void ForwardOnCompleted()
  135. {
  136. _parent._consumer.ForwardOnCompleted();
  137. }
  138. public void ForwardOnError(Exception error)
  139. {
  140. _parent._consumer.ForwardOnError(error);
  141. }
  142. public void ForwardOnNext(int value)
  143. {
  144. _parent._consumer.ForwardOnNext(value);
  145. if (_errorReenter)
  146. {
  147. HalfSerializer.ForwardOnError(this, X, ref _parent._wip, ref _parent._error);
  148. }
  149. else
  150. {
  151. HalfSerializer.ForwardOnCompleted(this, ref _parent._wip, ref _parent._error);
  152. }
  153. }
  154. }
  155. }
  156. }