コード例 #1
0
ファイル: TestGatewayLock.cs プロジェクト: lunaxi7/XTMF
        public void TestWriterLock()
        {
            GatewayLock gate  = new GatewayLock();
            Stopwatch   watch = new Stopwatch();

            watch.Start();
            long finishedPassThrough = 0;
            long inLock        = 0;
            Task secondaryTask = null;
            var  mainTask      = Task.Factory.StartNew(
                () =>
            {
                gate.PassThrough(
                    () =>
                {
                    secondaryTask = Task.Factory.StartNew(
                        () =>
                    {
                        gate.Lock(
                            () =>
                        {
                            // chill
                            inLock = watch.ElapsedMilliseconds;
                        });
                    });
                    Thread.Sleep(100);
                    finishedPassThrough = watch.ElapsedMilliseconds;
                });
            });

            mainTask.Wait();
            secondaryTask.Wait();
            watch.Stop();
            Assert.IsTrue(inLock >= finishedPassThrough);
        }
コード例 #2
0
        public void TestListWriter()
        {
            GatewayLock gate = new GatewayLock();

            for (int iteration = 0; iteration < 100; iteration++)
            {
                var list = new List <Entry>();
                Parallel.For(0, 1000, (int i) =>
                {
                    Random r = new Random();
                    for (int j = 0; j < 100; j++)
                    {
                        var num    = r.Next(10);
                        bool found = false;
                        gate.PassThrough(() =>
                        {
                            foreach (var entry in list)
                            {
                                if (entry.Number == num)
                                {
                                    lock ( entry )
                                    {
                                        entry.TimeFound++;
                                        found = true;
                                        return;
                                    }
                                }
                            }
                        });
                        if (!found)
                        {
                            gate.Lock(() =>
                            {
                                foreach (var entry in list)
                                {
                                    if (entry.Number == num)
                                    {
                                        entry.TimeFound++;
                                        found = true;
                                        return;
                                    }
                                }
                                list.Add(new Entry()
                                {
                                    Number = num, TimeFound = 1
                                });
                            });
                        }
                    }
                });
                Assert.IsTrue(list.Count <= 10);
            }
        }
コード例 #3
0
ファイル: TestGatewayLock.cs プロジェクト: lunaxi7/XTMF
        public void TestWriterHoldThenReaders()
        {
            GatewayLock gate  = new GatewayLock();
            Stopwatch   watch = new Stopwatch();

            watch.Start();
            bool writerDone = false;
            bool anyFails   = false;
            Task main       = Task.Factory.StartNew(
                () =>
            {
                var ourTasks = new Task[10];
                gate.Lock(() =>
                {
                    for (int i = 0; i < ourTasks.Length; i++)
                    {
                        ourTasks[i] = Task.Factory.StartNew(
                            () =>
                        {
                            gate.PassThrough(() =>
                            {
                                // ReSharper disable once AccessToModifiedClosure
                                if (!writerDone)
                                {
                                    anyFails = true;
                                }
                            });
                        });
                    }
                    Thread.Sleep(1000);
                    writerDone = true;
                });
                Task.WaitAll(ourTasks);
            });

            main.Wait();
            Thread.MemoryBarrier();
            Assert.IsFalse(anyFails);
        }