Esempio 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();
        }
Esempio n. 2
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();
        }