aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Tests/LocklessQueueTests.cs
blob: e34f767bda473b8909075655820dada9c0e8e6c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using NUnit.Framework;
using System.Threading;

namespace OpenSim.Framework.Tests
{
    [TestFixture]
    public class LocklessQueueTests
    {
        public LocklessQueue<int> sharedQueue;
        [SetUp]
        public void build()
        {
            sharedQueue = new LocklessQueue<int>();
            
        }

        [Test]
        public void EnqueueDequeueTest()
        {
            sharedQueue.Enqueue(1);
            int dequeue;
            sharedQueue.Dequeue(out dequeue);
            Assert.That(dequeue == 1, "Enqueued 1.   Dequeue should also be 1");
            Assert.That(sharedQueue.Count == 0, "We Dequeued the last item, count should be 0");

        }

        [Test]
        public void ThreadedSimpleEnqueueDequeueTest()
        {
            int loopamountA = 5000;
            int loopamountB = 5000;
            int loopamountC = 5000;
            int loopamountD = 5000;

            threadObject1 obj1 = new threadObject1(this, loopamountA);
            threadObject1 obj2 = new threadObject1(this, loopamountB);
            threadObject1 obj3 = new threadObject1(this, loopamountC);
            threadObject1 obj4 = new threadObject1(this, loopamountD);
            for (int i=0;i<1;i++)
            {
                sharedQueue.Enqueue(i);
            }

            Thread thr = new Thread(obj1.thread1Action);
            Thread thr2 = new Thread(obj2.thread1Action);
            Thread thr3 = new Thread(obj3.thread1Action);
            Thread thr4 = new Thread(obj4.thread1Action);
            thr.Start();
            thr2.Start();
            thr3.Start();
            thr4.Start();

            thr.Join();
            thr2.Join();
            thr3.Join();
            thr4.Join();

            Assert.That(sharedQueue.Count == 1);
            int result = 0;
            sharedQueue.Dequeue(out result);
            Assert.That(result == loopamountD + loopamountC + loopamountB + loopamountA, "Threaded Result test failed.  Expected the sum of all of the threads adding to the item in the queue.  Got {0}, Expected {1}", result, loopamountD + loopamountC + loopamountB + loopamountA);

        }

        /* This test fails.   Need clarification if this should work
        [Test]
        public void ThreadedAdvancedEnqueueDequeueTest()
        {
            int loopamountA = 5000;
            int loopamountB = 5000;
            int loopamountC = 5000;
            int loopamountD = 5000;

            threadObject1 obj1 = new threadObject1(this, loopamountA);
            threadObject2 obj2 = new threadObject2(this, loopamountB);
            threadObject1 obj3 = new threadObject1(this, loopamountC);
            threadObject2 obj4 = new threadObject2(this, loopamountD);
            for (int i = 0; i < 1; i++)
            {
                sharedQueue.Enqueue(i);
            }

            Thread thr = new Thread(obj1.thread1Action);
            Thread thr2 = new Thread(obj2.thread1Action);
            Thread thr3 = new Thread(obj3.thread1Action);
            Thread thr4 = new Thread(obj4.thread1Action);
            thr.Start();
            thr2.Start();
            thr3.Start();
            thr4.Start();

            thr.Join();
            thr2.Join();
            thr3.Join();
            thr4.Join();

            Assert.That(sharedQueue.Count == 1);
            int result = 0;
            sharedQueue.Dequeue(out result);
            Assert.That(result == loopamountA - loopamountB + loopamountC - loopamountD, "Threaded Result test failed.  Expected the sum of all of the threads adding to the item in the queue.  Got {0}, Expected {1}", result, loopamountA - loopamountB + loopamountC - loopamountD);

        }
         */
    }
    // Dequeue one from the locklessqueue add one to it and enqueue it again.
    public class threadObject1
    {
        private LocklessQueueTests m_tests;
        private int m_loopamount = 0;
        public threadObject1(LocklessQueueTests tst, int loopamount)
        {
            m_tests = tst;
            m_loopamount = loopamount;
        }
        public void thread1Action(object o)
        {
            for (int i=0;i<m_loopamount;i++)
            {
                int j = 0;
                m_tests.sharedQueue.Dequeue(out j);
                m_tests.sharedQueue.Enqueue(++j);
            }
        }
    }
    // Dequeue one from the locklessqueue subtract one from it and enqueue it again.
    public class threadObject2
    {
        private LocklessQueueTests m_tests;
        private int m_loopamount = 0;
        public threadObject2(LocklessQueueTests tst, int loopamount)
        {
            m_tests = tst;
            m_loopamount = loopamount;
        }
        public void thread1Action(object o)
        {
            for (int i = 0; i < m_loopamount; i++)
            {
                int j = 0;
                m_tests.sharedQueue.Dequeue(out j);
                m_tests.sharedQueue.Enqueue(--j);
            }
        }
    }
}