HalfSerializerTest.cs 6.2 KB

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