ObservableRemotingTest.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #if HAS_REMOTING
  5. using System;
  6. using System.Reactive.Linq;
  7. using System.Reflection;
  8. using System.Runtime.Remoting.Lifetime;
  9. using System.Threading;
  10. using Microsoft.Reactive.Testing;
  11. using Microsoft.VisualStudio.TestTools.UnitTesting;
  12. using Assert = Xunit.Assert;
  13. namespace ReactiveTests.Tests
  14. {
  15. [TestClass]
  16. public partial class ObservableRemotingTest : ReactiveTest
  17. {
  18. [TestMethod]
  19. public void Remotable_ArgumentChecking()
  20. {
  21. ReactiveAssert.Throws<ArgumentNullException>(() => RemotingObservable.Remotable(default(IObservable<int>)));
  22. ReactiveAssert.Throws<ArgumentNullException>(() => RemotingObservable.Remotable(default(IObservable<int>), new MyLease()));
  23. ReactiveAssert.Throws<ArgumentNullException>(() => RemotingObservable.Remotable(default(IQbservable<int>)));
  24. ReactiveAssert.Throws<ArgumentNullException>(() => RemotingObservable.Remotable(default(IQbservable<int>), new MyLease()));
  25. RemotingObservable.Remotable(Observable.Return(42));
  26. RemotingObservable.Remotable(Observable.Return(42), null /* valid lease object */);
  27. RemotingObservable.Remotable(Qbservable.Return(Qbservable.Provider, 42));
  28. RemotingObservable.Remotable(Qbservable.Return(Qbservable.Provider, 42), null /* valid lease object */);
  29. }
  30. private class MyLease : ILease
  31. {
  32. public TimeSpan CurrentLeaseTime
  33. {
  34. get { throw new NotImplementedException(); }
  35. }
  36. public LeaseState CurrentState
  37. {
  38. get { throw new NotImplementedException(); }
  39. }
  40. public TimeSpan InitialLeaseTime
  41. {
  42. get
  43. {
  44. throw new NotImplementedException();
  45. }
  46. set
  47. {
  48. throw new NotImplementedException();
  49. }
  50. }
  51. public void Register(ISponsor obj)
  52. {
  53. throw new NotImplementedException();
  54. }
  55. public void Register(ISponsor obj, TimeSpan renewalTime)
  56. {
  57. throw new NotImplementedException();
  58. }
  59. public TimeSpan Renew(TimeSpan renewalTime)
  60. {
  61. throw new NotImplementedException();
  62. }
  63. public TimeSpan RenewOnCallTime
  64. {
  65. get
  66. {
  67. throw new NotImplementedException();
  68. }
  69. set
  70. {
  71. throw new NotImplementedException();
  72. }
  73. }
  74. public TimeSpan SponsorshipTimeout
  75. {
  76. get
  77. {
  78. throw new NotImplementedException();
  79. }
  80. set
  81. {
  82. throw new NotImplementedException();
  83. }
  84. }
  85. public void Unregister(ISponsor obj)
  86. {
  87. throw new NotImplementedException();
  88. }
  89. }
  90. [TestMethod]
  91. public void Remotable_Empty()
  92. {
  93. var evt = new ManualResetEvent(false);
  94. var e = GetRemoteObservable(t => t.Empty());
  95. using (e.Subscribe(_ => { Assert.True(false); }, _ => { Assert.True(false); }, () => { evt.Set(); }))
  96. {
  97. evt.WaitOne();
  98. }
  99. }
  100. [TestMethod]
  101. public void Remotable_Return()
  102. {
  103. var evt = new ManualResetEvent(false);
  104. var next = false;
  105. var e = GetRemoteObservable(t => t.Return(42));
  106. using (e.Subscribe(value => { next = true; Assert.Equal(42, value); }, _ => { Assert.True(false); }, () => { evt.Set(); }))
  107. {
  108. evt.WaitOne();
  109. Assert.True(next);
  110. }
  111. }
  112. [TestMethod]
  113. public void Remotable_Return_LongLease()
  114. {
  115. var evt = new ManualResetEvent(false);
  116. var next = false;
  117. var e = GetRemoteObservable(t => t.ReturnLongLease(42));
  118. using (e.Subscribe(value => { next = true; Assert.Equal(42, value); }, _ => { Assert.True(false); }, () => { evt.Set(); }))
  119. {
  120. evt.WaitOne();
  121. Assert.True(next);
  122. }
  123. }
  124. [TestMethod]
  125. public void Remotable_Throw()
  126. {
  127. var ex = new InvalidOperationException("Oops!");
  128. var evt = new ManualResetEvent(false);
  129. var error = false;
  130. var e = GetRemoteObservable(t => t.Throw(ex));
  131. using (e.Subscribe(value => { Assert.True(false); }, err => { error = true; Assert.True(err is InvalidOperationException && err.Message == ex.Message); evt.Set(); }, () => { Assert.True(false); }))
  132. {
  133. evt.WaitOne();
  134. Assert.True(error);
  135. }
  136. }
  137. [TestMethod]
  138. public void Remotable_Disposal()
  139. {
  140. var test = GetRemoteTestObject();
  141. test.Disposal().Subscribe().Dispose();
  142. Assert.True(test.Disposed);
  143. }
  144. private IObservable<int> GetRemoteObservable(Func<RemotingTest, IObservable<int>> f)
  145. {
  146. var test = GetRemoteTestObject();
  147. return f(test);
  148. }
  149. private RemotingTest GetRemoteTestObject()
  150. {
  151. var ads = new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.BaseDirectory };
  152. var ad = AppDomain.CreateDomain("test", null, ads);
  153. var test = (RemotingTest)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "ReactiveTests.Tests.RemotingTest");
  154. return test;
  155. }
  156. }
  157. public class RemotingTest : MarshalByRefObject
  158. {
  159. public override object InitializeLifetimeService()
  160. {
  161. return null;
  162. }
  163. public IObservable<int> Empty()
  164. {
  165. return Observable.Empty<int>().Remotable();
  166. }
  167. public IObservable<int> Return(int value)
  168. {
  169. return Observable.Return(value).Remotable();
  170. }
  171. public IObservable<int> ReturnLongLease(int value)
  172. {
  173. return Observable.Return(value).Remotable(null);
  174. }
  175. public IObservable<int> Throw(Exception ex)
  176. {
  177. return Observable.Throw<int>(ex).Remotable();
  178. }
  179. public IObservable<int> Disposal()
  180. {
  181. return Observable.Create<int>(obs =>
  182. {
  183. return () => { Disposed = true; };
  184. }).Remotable();
  185. }
  186. public bool Disposed { get; set; }
  187. }
  188. }
  189. #endif