| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if !NO_REMOTING |
| 4 |
|
| 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 |
#if NUNIT |
| 12 |
using NUnit.Framework; |
| 13 |
using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; |
| 14 |
using TestMethodAttribute = NUnit.Framework.TestAttribute; |
| 15 |
using TestInitializeAttribute = NUnit.Framework.SetUpAttribute; |
| 16 |
#else |
| 17 |
using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 18 |
#endif |
| 19 |
|
| 20 |
namespace ReactiveTests.Tests |
| 21 |
{ |
| 22 |
[TestClass] |
| 23 |
public partial class ObservableRemotingTest : ReactiveTest |
| 24 |
{ |
| 25 |
[TestMethod] |
| 26 |
public void Remotable_ArgumentChecking() |
| 27 |
{ |
| 28 |
ReactiveAssert.Throws<ArgumentNullException>(() => RemotingObservable.Remotable(default(IObservable<int>))); |
| 29 |
ReactiveAssert.Throws<ArgumentNullException>(() => RemotingObservable.Remotable(default(IObservable<int>), new MyLease())); |
| 30 |
|
| 31 |
ReactiveAssert.Throws<ArgumentNullException>(() => RemotingObservable.Remotable(default(IQbservable<int>))); |
| 32 |
ReactiveAssert.Throws<ArgumentNullException>(() => RemotingObservable.Remotable(default(IQbservable<int>), new MyLease())); |
| 33 |
|
| 34 |
RemotingObservable.Remotable(Observable.Return(42)); |
| 35 |
RemotingObservable.Remotable(Observable.Return(42), null /* valid lease object */); |
| 36 |
|
| 37 |
#if !NO_REFLECTION_EMIT |
| 38 |
RemotingObservable.Remotable(Qbservable.Return(Qbservable.Provider, 42)); |
| 39 |
RemotingObservable.Remotable(Qbservable.Return(Qbservable.Provider, 42), null /* valid lease object */); |
| 40 |
#endif |
| 41 |
} |
| 42 |
|
| 43 |
class MyLease : ILease |
| 44 |
{ |
| 45 |
public TimeSpan CurrentLeaseTime |
| 46 |
{ |
| 47 |
get { throw new NotImplementedException(); } |
| 48 |
} |
| 49 |
|
| 50 |
public LeaseState CurrentState |
| 51 |
{ |
| 52 |
get { throw new NotImplementedException(); } |
| 53 |
} |
| 54 |
|
| 55 |
public TimeSpan InitialLeaseTime |
| 56 |
{ |
| 57 |
get |
| 58 |
{ |
| 59 |
throw new NotImplementedException(); |
| 60 |
} |
| 61 |
set |
| 62 |
{ |
| 63 |
throw new NotImplementedException(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public void Register(ISponsor obj) |
| 68 |
{ |
| 69 |
throw new NotImplementedException(); |
| 70 |
} |
| 71 |
|
| 72 |
public void Register(ISponsor obj, TimeSpan renewalTime) |
| 73 |
{ |
| 74 |
throw new NotImplementedException(); |
| 75 |
} |
| 76 |
|
| 77 |
public TimeSpan Renew(TimeSpan renewalTime) |
| 78 |
{ |
| 79 |
throw new NotImplementedException(); |
| 80 |
} |
| 81 |
|
| 82 |
public TimeSpan RenewOnCallTime |
| 83 |
{ |
| 84 |
get |
| 85 |
{ |
| 86 |
throw new NotImplementedException(); |
| 87 |
} |
| 88 |
set |
| 89 |
{ |
| 90 |
throw new NotImplementedException(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public TimeSpan SponsorshipTimeout |
| 95 |
{ |
| 96 |
get |
| 97 |
{ |
| 98 |
throw new NotImplementedException(); |
| 99 |
} |
| 100 |
set |
| 101 |
{ |
| 102 |
throw new NotImplementedException(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
public void Unregister(ISponsor obj) |
| 107 |
{ |
| 108 |
throw new NotImplementedException(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
[TestMethod] |
| 113 |
public void Remotable_Empty() |
| 114 |
{ |
| 115 |
var evt = new ManualResetEvent(false); |
| 116 |
|
| 117 |
var e = GetRemoteObservable(t => t.Empty()); |
| 118 |
using (e.Subscribe(_ => { Assert.Fail(); }, _ => { Assert.Fail(); }, () => { evt.Set(); })) |
| 119 |
{ |
| 120 |
evt.WaitOne(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
[TestMethod] |
| 125 |
public void Remotable_Return() |
| 126 |
{ |
| 127 |
var evt = new ManualResetEvent(false); |
| 128 |
|
| 129 |
bool next = false; |
| 130 |
var e = GetRemoteObservable(t => t.Return(42)); |
| 131 |
using (e.Subscribe(value => { next = true; Assert.AreEqual(42, value); }, _ => { Assert.Fail(); }, () => { evt.Set(); })) |
| 132 |
{ |
| 133 |
evt.WaitOne(); |
| 134 |
Assert.IsTrue(next); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
[TestMethod] |
| 139 |
public void Remotable_Return_LongLease() |
| 140 |
{ |
| 141 |
var evt = new ManualResetEvent(false); |
| 142 |
|
| 143 |
bool next = false; |
| 144 |
var e = GetRemoteObservable(t => t.ReturnLongLease(42)); |
| 145 |
using (e.Subscribe(value => { next = true; Assert.AreEqual(42, value); }, _ => { Assert.Fail(); }, () => { evt.Set(); })) |
| 146 |
{ |
| 147 |
evt.WaitOne(); |
| 148 |
Assert.IsTrue(next); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
[TestMethod] |
| 153 |
public void Remotable_Throw() |
| 154 |
{ |
| 155 |
var ex = new InvalidOperationException("Oops!"); |
| 156 |
|
| 157 |
var evt = new ManualResetEvent(false); |
| 158 |
|
| 159 |
bool error = false; |
| 160 |
var e = GetRemoteObservable(t => t.Throw(ex)); |
| 161 |
using (e.Subscribe(value => { Assert.Fail(); }, err => { error = true; Assert.IsTrue(err is InvalidOperationException && err.Message == ex.Message); evt.Set(); }, () => { Assert.Fail(); })) |
| 162 |
{ |
| 163 |
evt.WaitOne(); |
| 164 |
Assert.IsTrue(error); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
[TestMethod] |
| 169 |
public void Remotable_Disposal() |
| 170 |
{ |
| 171 |
var test = GetRemoteTestObject(); |
| 172 |
test.Disposal().Subscribe().Dispose(); |
| 173 |
Assert.IsTrue(test.Disposed); |
| 174 |
} |
| 175 |
|
| 176 |
private IObservable<int> GetRemoteObservable(Func<RemotingTest, IObservable<int>> f) |
| 177 |
{ |
| 178 |
var test = GetRemoteTestObject(); |
| 179 |
return f(test); |
| 180 |
} |
| 181 |
|
| 182 |
private RemotingTest GetRemoteTestObject() |
| 183 |
{ |
| 184 |
var ads = new AppDomainSetup { ApplicationBase = Utils.GetTestBaseDirectory() }; |
| 185 |
var ad = AppDomain.CreateDomain("test", null, ads); |
| 186 |
var test = (RemotingTest)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "ReactiveTests.Tests.RemotingTest"); |
| 187 |
return test; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
public class RemotingTest : MarshalByRefObject |
| 192 |
{ |
| 193 |
public override object InitializeLifetimeService() |
| 194 |
{ |
| 195 |
return null; |
| 196 |
} |
| 197 |
|
| 198 |
public IObservable<int> Empty() |
| 199 |
{ |
| 200 |
return Observable.Empty<int>().Remotable(); |
| 201 |
} |
| 202 |
|
| 203 |
public IObservable<int> Return(int value) |
| 204 |
{ |
| 205 |
return Observable.Return<int>(value).Remotable(); |
| 206 |
} |
| 207 |
|
| 208 |
public IObservable<int> ReturnLongLease(int value) |
| 209 |
{ |
| 210 |
return Observable.Return<int>(value).Remotable(null); |
| 211 |
} |
| 212 |
|
| 213 |
public IObservable<int> Throw(Exception ex) |
| 214 |
{ |
| 215 |
return Observable.Throw<int>(ex).Remotable(); |
| 216 |
} |
| 217 |
|
| 218 |
public IObservable<int> Disposal() |
| 219 |
{ |
| 220 |
return Observable.Create<int>(obs => |
| 221 |
{ |
| 222 |
return () => { Disposed = true; }; |
| 223 |
}).Remotable(); |
| 224 |
} |
| 225 |
|
| 226 |
public bool Disposed { get; set; } |
| 227 |
} |
| 228 |
} |
| 229 |
#endif |