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_DisposableResource_Test()
        {
            var locked = new LockedResource <ITestResource>();

            locked.Set(MockRepository.GenerateStub <IDisposableTestResource>());
            Assert.IsTrue(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_Resource_Release_Resource_Test()
        {
            var locked = new LockedResource <ITestResource>();

            locked.Set(MockRepository.GenerateStub <ITestResource>());
            Assert.IsTrue(locked.IsAvailable);
            locked.Release();
            Assert.IsFalse(locked.IsAvailable);
        }
        public void Write_ValueType()
        {
            var locked = new LockedResource <int>();
            var i      = 1;

            locked.Write(i);

            Assert.AreEqual(i, locked.Read());
        }
        public void Write_ReferenceType()
        {
            var        locked = new LockedResource <Func <int> >();
            Func <int> func   = () => 1;

            locked.Write(func);

            Assert.IsNotNull(locked.Read());
            Assert.ReferenceEquals(func, locked.Read());
        }
Exemplo n.º 7
0
      public void LockedResource_Set_DisposableResource_Set_Resource_Test()
      {
         var disposableResource = MockRepository.GenerateMock<IDisposableTestResource>();

         var locked = new LockedResource<ITestResource>();
         locked.Set(disposableResource);
         Assert.IsTrue(locked.IsAvailable);
         locked.Set(MockRepository.GenerateStub<ITestResource>());
         Assert.IsTrue(locked.IsAvailable);
         
         disposableResource.AssertWasCalled(x => x.Dispose());
      }
Exemplo n.º 8
0
        public void LockedResource_Set_DisposableResource_Set_Resource_Test()
        {
            var disposableResource = MockRepository.GenerateMock <IDisposableTestResource>();

            var locked = new LockedResource <ITestResource>();

            locked.Set(disposableResource);
            Assert.IsTrue(locked.IsAvailable);
            locked.Set(MockRepository.GenerateStub <ITestResource>());
            Assert.IsTrue(locked.IsAvailable);

            disposableResource.AssertWasCalled(x => x.Dispose());
        }
        public void Multithreading()
        {
            var res  = new Resource <int>(1);
            var res1 = new Resource <int>(1);
            var res2 = new Resource <int>(2);

            Func <Resource <int> >   get = () => res;
            Action <Resource <int> > set = r => { Thread.Sleep(1200); res = r; };

            var locked = new LockedResource <Resource <int> >(new MockResourceManager <Resource <int> >(get, set));

            locked.Write(res);

            var order = new List <int>();

            var first = new Task(() =>
            {
                order.Add(res1.Value);
                locked.Write(res1);
                order.Add(res1.Value);
            });
            var second = new Task(() =>
            {
                Thread.Sleep(100);
                order.Add(res2.Value);
                locked.Write(res2);
                order.Add(res2.Value);
            });

            first.Start();
            second.Start();
            Task.WaitAll(new[] { first, second });

            Assert.ReferenceEquals(res2, res);
            CollectionAssert.AreEquivalent(new List <int>()
            {
                1, 2, 1, 2
            }, order);
        }
Exemplo n.º 10
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.º 11
0
        public void Constructor_ValueType()
        {
            var locked = new LockedResource <int>();

            Assert.AreEqual(default(int), locked.Read());
        }
Exemplo n.º 12
0
        public void Constructor_ReferenceType()
        {
            var locked = new LockedResource <Func <int> >();

            Assert.IsNull(locked.Read());
        }
Exemplo n.º 13
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.º 14
0
 public void LockedResource_DefaultValues_Test()
 {
    var locked = new LockedResource<ITestResource>();
    Assert.IsFalse(locked.IsAvailable);
 }
Exemplo n.º 15
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.º 16
0
        public void LockedResource_DefaultValues_Test()
        {
            var locked = new LockedResource <ITestResource>();

            Assert.IsFalse(locked.IsAvailable);
        }
Exemplo n.º 17
0
 public void LockedResource_Set_DisposableResource_Test()
 {
    var locked = new LockedResource<ITestResource>();
    locked.Set(MockRepository.GenerateStub<IDisposableTestResource>());
    Assert.IsTrue(locked.IsAvailable);
 }
Exemplo n.º 18
0
      /// <summary>
      /// Initializes a new instance of the Connection class.
      /// </summary>
      internal Connection(ITcpClientFactory tcpClientFactory)
      {
         ConnectTimeout = DefaultTimeoutLength;

         _tcpClientFactory = tcpClientFactory;
         _lockedStream = new LockedResource<INetworkStream>();
         _tcpClient = CreateClient();
         _internalBuffer = new byte[InternalBufferSize];
         _readBuffer = new StringBuilder();
      }