Esempio n. 1
0
        public void InitializeOnlyOnce()
        {
            var control    = 0;
            var threadDone = 0;
            var needle     = new LazyNeedle <int>(() =>
            {
                Interlocked.Increment(ref control);
                return(5);
            });

            using (var manual = new ManualResetEvent(false))
            {
                var threadA = new Thread
                              (
                    () =>
                {
                    manual.WaitOne();
                    needle.Initialize();
                    Interlocked.Increment(ref threadDone);
                }
                              );
                var threadB = new Thread(() =>
                {
                    manual.WaitOne();
                    needle.Initialize();
                    Interlocked.Increment(ref threadDone);
                });
                var threadC = new Thread(() =>
                {
                    manual.WaitOne();
                    needle.Initialize();
                    Interlocked.Increment(ref threadDone);
                });
                threadA.Start();
                threadB.Start();
                threadC.Start();
                manual.Set();
                threadA.Join();
                threadB.Join();
                threadC.Join();
                needle.Initialize();
                needle.Initialize();
                Assert.IsTrue(needle.IsCompleted);
                Assert.AreEqual(needle.Value, 5);
                Assert.AreEqual(control, 1);
                Assert.AreEqual(threadDone, 3);
                manual.Close();
            }
        }
Esempio n. 2
0
        public void FixedHashCode()
        {
            //No initial value
            var a        = new LazyNeedle <int>();
            var hashcode = a.GetHashCode();

            a.Value = 5;
            Assert.AreEqual(hashcode, a.GetHashCode());
            a.Value = 6;
            Assert.AreEqual(hashcode, a.GetHashCode());
            //Initial value
            var b = new LazyNeedle <int>(5);

            hashcode = b.GetHashCode();
            b.Value  = 5;
            Assert.AreEqual(hashcode, b.GetHashCode());
            b.Value = 6;
            Assert.AreEqual(hashcode, b.GetHashCode());
            //ValueFactory
            var c = new LazyNeedle <int>(() => 5);

            hashcode = c.GetHashCode();
            c.Initialize();
            Assert.IsTrue(c.IsCompleted);
            Assert.AreEqual(c.Value, 5);
            Assert.AreEqual(hashcode, c.GetHashCode());
        }
Esempio n. 3
0
        public void Waiting()
        {
            var waitStarted = 0;
            var threadDone  = 0;
            var control     = 0;
            var readed      = 0;
            var needle      = new LazyNeedle <int>(() =>
            {
                Interlocked.Increment(ref control);
                return(5);
            });
            var threadA = new Thread(() =>
            {
                Thread.VolatileWrite(ref waitStarted, 1);
                needle.Wait();
                needle.Initialize();
                Interlocked.Increment(ref threadDone);
            });
            var threadB = new Thread
                          (
                () =>
            {
                while (Thread.VolatileRead(ref waitStarted) == 0)
                {
                    Thread.Sleep(0);
                }
                readed = needle.Value;
            }
                          );

            threadA.Start();
            threadB.Start();
            threadA.Join();
            threadB.Join();
            needle.Initialize();
            Assert.IsTrue(needle.IsCompleted);
            Assert.AreEqual(readed, 5);
            Assert.AreEqual(needle.Value, 5);
            Assert.AreEqual(control, 1);
            Assert.AreEqual(threadDone, 1);
            Assert.AreEqual(waitStarted, 1);
        }
Esempio n. 4
0
        public void WaitingAlreadyCompleted()
        {
            var needle = new LazyNeedle <int>(5);

            Assert.IsTrue(needle.IsCompleted);
            Assert.AreEqual(needle.Value, 5);
            needle.Wait();
            Assert.IsTrue(needle.IsCompleted);
            Assert.AreEqual(needle.Value, 5);
            needle.Initialize();
            Assert.IsTrue(needle.IsCompleted);
            Assert.AreEqual(needle.Value, 5);
        }
Esempio n. 5
0
 public void FixedHashCode()
 {
     //No initial value
     var a = new LazyNeedle<int>();
     var hashcode = a.GetHashCode();
     a.Value = 5;
     Assert.AreEqual(hashcode, a.GetHashCode());
     a.Value = 6;
     Assert.AreEqual(hashcode, a.GetHashCode());
     //Initial value
     var b = new LazyNeedle<int>(5);
     hashcode = b.GetHashCode();
     b.Value = 5;
     Assert.AreEqual(hashcode, b.GetHashCode());
     b.Value = 6;
     Assert.AreEqual(hashcode, b.GetHashCode());
     //ValueFactory
     var c = new LazyNeedle<int>(() => 5);
     hashcode = c.GetHashCode();
     c.Initialize();
     Assert.IsTrue(c.IsCompleted);
     Assert.AreEqual(c.Value, 5);
     Assert.AreEqual(hashcode, c.GetHashCode());
 }
Esempio n. 6
0
 public void WaitingAlreadyCompleted()
 {
     var needle = new LazyNeedle<int>(5);
     Assert.IsTrue(needle.IsCompleted);
     Assert.AreEqual(needle.Value, 5);
     needle.Wait();
     Assert.IsTrue(needle.IsCompleted);
     Assert.AreEqual(needle.Value, 5);
     needle.Initialize();
     Assert.IsTrue(needle.IsCompleted);
     Assert.AreEqual(needle.Value, 5);
 }
Esempio n. 7
0
 public void Waiting()
 {
     var waitStarted = 0;
     var threadDone = 0;
     var control = 0;
     var readed = 0;
     var needle = new LazyNeedle<int>(() =>
     {
         Interlocked.Increment(ref control); return 5;
     });
     var threadA = new Thread(() =>
     {
         Thread.VolatileWrite(ref waitStarted, 1);
         needle.Wait();
         needle.Initialize();
         Interlocked.Increment(ref threadDone);
     });
     var threadB = new Thread
     (
         () =>
         {
             while (Thread.VolatileRead(ref waitStarted) == 0)
             {
                 Thread.Sleep(0);
             }
             readed = needle.Value;
         }
     );
     threadA.Start();
     threadB.Start();
     threadA.Join();
     threadB.Join();
     needle.Initialize();
     Assert.IsTrue(needle.IsCompleted);
     Assert.AreEqual(readed, 5);
     Assert.AreEqual(needle.Value, 5);
     Assert.AreEqual(control, 1);
     Assert.AreEqual(threadDone, 1);
     Assert.AreEqual(waitStarted, 1);
 }
Esempio n. 8
0
 public void InitializeOnlyOnce()
 {
     var control = 0;
     var threadDone = 0;
     var needle = new LazyNeedle<int>(() =>
     {
         Interlocked.Increment(ref control); return 5;
     });
     var manual = new ManualResetEvent(false);
     var threadA = new Thread(() =>
     {
         manual.WaitOne();
         needle.Initialize();
         Interlocked.Increment(ref threadDone);
     });
     var threadB = new Thread(() =>
     {
         manual.WaitOne();
         needle.Initialize();
         Interlocked.Increment(ref threadDone);
     });
     var threadC = new Thread(() =>
     {
         manual.WaitOne();
         needle.Initialize();
         Interlocked.Increment(ref threadDone);
     });
     threadA.Start();
     threadB.Start();
     threadC.Start();
     manual.Set();
     threadA.Join();
     threadB.Join();
     threadC.Join();
     needle.Initialize();
     needle.Initialize();
     Assert.IsTrue(needle.IsCompleted);
     Assert.AreEqual(needle.Value, 5);
     Assert.AreEqual(control, 1);
     Assert.AreEqual(threadDone, 3);
     manual.Close();
 }