Exemplo n.º 1
0
        public void LockedResource_Set_DisposableResource_Execute_Func_Release_Resource_Test()
        {
            var mre      = new ManualResetEvent(false);
            var locked   = new LockedResource <ITestResource>();
            var resource = MockRepository.GenerateMock <IDisposableTestResource>();

            resource.Expect(x => x.Func()).Do(new Func <int>(() =>
            {
                locked.Release();
                mre.Set();
                return(1);
            })).Repeat.Once();

            locked.Set(resource);

            var tasks = new List <Task>();

            tasks.Add(Task.Factory.StartNew(() => locked.Execute(x => x.Func())));
            mre.WaitOne();
            tasks.Add(Task.Factory.StartNew(() => locked.Execute(x => x.Func())));

            Task.WaitAll(tasks.ToArray());
            Assert.AreEqual(1, ((Task <int>)tasks[0]).Result);
            Assert.AreEqual(0, ((Task <int>)tasks[1]).Result);
            resource.VerifyAllExpectations();
        }
Exemplo n.º 2
0
 public void LockedResource_Set_Resource_Release_Resource_Test()
 {
    var locked = new LockedResource<ITestResource>();
    locked.Set(MockRepository.GenerateStub<ITestResource>());
    Assert.IsTrue(locked.IsAvailable);
    locked.Release();
    Assert.IsFalse(locked.IsAvailable);
 }
Exemplo n.º 3
0
        public void LockedResource_Set_Resource_Release_Resource_Test()
        {
            var locked = new LockedResource <ITestResource>();

            locked.Set(MockRepository.GenerateStub <ITestResource>());
            Assert.IsTrue(locked.IsAvailable);
            locked.Release();
            Assert.IsFalse(locked.IsAvailable);
        }
Exemplo n.º 4
0
      public void LockedResource_Set_DisposableResource_Release_DisposableResource_Test()
      {
         var disposableResource = MockRepository.GenerateMock<IDisposableTestResource>();

         var locked = new LockedResource<ITestResource>();
         locked.Set(disposableResource);
         Assert.IsTrue(locked.IsAvailable);
         locked.Release();
         Assert.IsFalse(locked.IsAvailable);

         disposableResource.AssertWasCalled(x => x.Dispose());
      }
Exemplo n.º 5
0
        public void LockedResource_Set_DisposableResource_Release_DisposableResource_Test()
        {
            var disposableResource = MockRepository.GenerateMock <IDisposableTestResource>();

            var locked = new LockedResource <ITestResource>();

            locked.Set(disposableResource);
            Assert.IsTrue(locked.IsAvailable);
            locked.Release();
            Assert.IsFalse(locked.IsAvailable);

            disposableResource.AssertWasCalled(x => x.Dispose());
        }
Exemplo n.º 6
0
        public void LockedResource_Set_Resource_Execute_Action_Release_Resource_Test()
        {
            var mre      = new ManualResetEvent(false);
            var locked   = new LockedResource <ITestResource>();
            var resource = MockRepository.GenerateMock <ITestResource>();

            resource.Expect(x => x.Action()).Do(new Action(() =>
            {
                locked.Release();
                mre.Set();
            })).Repeat.Once();

            locked.Set(resource);

            var tasks = new List <Task>();

            tasks.Add(Task.Factory.StartNew(() => locked.Execute(x => x.Action())));
            mre.WaitOne();
            tasks.Add(Task.Factory.StartNew(() => locked.Execute(x => Assert.Fail())));

            Task.WaitAll(tasks.ToArray());
            resource.VerifyAllExpectations();
        }
Exemplo n.º 7
0
      public void LockedResource_Set_Resource_Execute_Action_Release_Resource_Test()
      {
         var mre = new ManualResetEvent(false);
         var locked = new LockedResource<ITestResource>();
         var resource = MockRepository.GenerateMock<ITestResource>();

         resource.Expect(x => x.Action()).Do(new Action(() =>
         {
            locked.Release();
            mre.Set();
         })).Repeat.Once();

         locked.Set(resource);
         
         var tasks = new List<Task>();
         tasks.Add(Task.Factory.StartNew(() => locked.Execute(x => x.Action())));
         mre.WaitOne();
         tasks.Add(Task.Factory.StartNew(() => locked.Execute(x => Assert.Fail())));
         
         Task.WaitAll(tasks.ToArray());
         resource.VerifyAllExpectations();
      }
Exemplo n.º 8
0
      public void LockedResource_Set_DisposableResource_Execute_Func_Release_Resource_Test()
      {
         var mre = new ManualResetEvent(false);
         var locked = new LockedResource<ITestResource>();
         var resource = MockRepository.GenerateMock<IDisposableTestResource>();

         resource.Expect(x => x.Func()).Do(new Func<int>(() =>
         {
            locked.Release();
            mre.Set();
            return 1;
         })).Repeat.Once();

         locked.Set(resource);

         var tasks = new List<Task>();
         tasks.Add(Task.Factory.StartNew(() => locked.Execute(x => x.Func())));
         mre.WaitOne();
         tasks.Add(Task.Factory.StartNew(() => locked.Execute(x => x.Func())));

         Task.WaitAll(tasks.ToArray());
         Assert.AreEqual(1, ((Task<int>)tasks[0]).Result);
         Assert.AreEqual(0, ((Task<int>)tasks[1]).Result);
         resource.VerifyAllExpectations();
      }