123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
- #if !SILVERLIGHT // MethodAccessException
- using System;
- using System.Threading.Tasks;
- using System.Reactive.Concurrency;
- using System.Reflection;
- using Xunit;
- namespace ReactiveTests.Tests
- {
-
- public class AsyncLockTest
- {
- [Fact]
- public void Wait_ArgumentChecking()
- {
- var asyncLock = new AsyncLock();
- Assert.Throws<ArgumentNullException>(() => asyncLock.Wait(null));
- }
- [Fact]
- public void Wait_Graceful()
- {
- var ok = false;
- new AsyncLock().Wait(() => { ok = true; });
- Assert.True(ok);
- }
- [Fact]
- public void Wait_Fail()
- {
- var l = new AsyncLock();
- var ex = new Exception();
- try
- {
- l.Wait(() => { throw ex; });
- Assert.True(false);
- }
- catch (Exception e)
- {
- Assert.Same(ex, e);
- }
- // has faulted; should not run
- l.Wait(() => { Assert.True(false); });
- }
- [Fact]
- public void Wait_QueuesWork()
- {
- var l = new AsyncLock();
- var l1 = false;
- var l2 = false;
- l.Wait(() => { l.Wait(() => { Assert.True(l1); l2 = true; }); l1 = true; });
- Assert.True(l2);
- }
- [Fact]
- public void Dispose()
- {
- var l = new AsyncLock();
- var l1 = false;
- var l2 = false;
- var l3 = false;
- var l4 = false;
- l.Wait(() =>
- {
- l.Wait(() =>
- {
- l.Wait(() =>
- {
- l3 = true;
- });
- l2 = true;
- l.Dispose();
- l.Wait(() =>
- {
- l4 = true;
- });
- });
- l1 = true;
- });
- Assert.True(l1);
- Assert.True(l2);
- Assert.False(l3);
- Assert.False(l4);
- }
- public class AsyncLock
- {
- object instance;
- public AsyncLock()
- {
- instance = typeof(Scheduler).GetTypeInfo().Assembly.GetType("System.Reactive.Concurrency.AsyncLock").GetConstructor(new Type[] { }).Invoke(new object[] { });
- }
- public void Wait(Action action)
- {
- try
- {
- instance.GetType().GetMethod("Wait").Invoke(instance, new object[] { action });
- }
- catch (TargetInvocationException ex)
- {
- throw ex.InnerException;
- }
- }
- public void Dispose()
- {
- try
- {
- instance.GetType().GetMethod("Dispose").Invoke(instance, new object[0]);
- }
- catch (TargetInvocationException ex)
- {
- throw ex.InnerException;
- }
- }
- }
- }
- }
- #endif
|