diff options
Merge branch 'master' of ssh://melanie@opensimulator.org/var/git/opensim
Diffstat (limited to 'OpenSim/Framework/Tests/LocklessQueueTests.cs')
-rw-r--r-- | OpenSim/Framework/Tests/LocklessQueueTests.cs | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/OpenSim/Framework/Tests/LocklessQueueTests.cs b/OpenSim/Framework/Tests/LocklessQueueTests.cs new file mode 100644 index 0000000..e34f767 --- /dev/null +++ b/OpenSim/Framework/Tests/LocklessQueueTests.cs | |||
@@ -0,0 +1,147 @@ | |||
1 | using System; | ||
2 | using NUnit.Framework; | ||
3 | using System.Threading; | ||
4 | |||
5 | namespace OpenSim.Framework.Tests | ||
6 | { | ||
7 | [TestFixture] | ||
8 | public class LocklessQueueTests | ||
9 | { | ||
10 | public LocklessQueue<int> sharedQueue; | ||
11 | [SetUp] | ||
12 | public void build() | ||
13 | { | ||
14 | sharedQueue = new LocklessQueue<int>(); | ||
15 | |||
16 | } | ||
17 | |||
18 | [Test] | ||
19 | public void EnqueueDequeueTest() | ||
20 | { | ||
21 | sharedQueue.Enqueue(1); | ||
22 | int dequeue; | ||
23 | sharedQueue.Dequeue(out dequeue); | ||
24 | Assert.That(dequeue == 1, "Enqueued 1. Dequeue should also be 1"); | ||
25 | Assert.That(sharedQueue.Count == 0, "We Dequeued the last item, count should be 0"); | ||
26 | |||
27 | } | ||
28 | |||
29 | [Test] | ||
30 | public void ThreadedSimpleEnqueueDequeueTest() | ||
31 | { | ||
32 | int loopamountA = 5000; | ||
33 | int loopamountB = 5000; | ||
34 | int loopamountC = 5000; | ||
35 | int loopamountD = 5000; | ||
36 | |||
37 | threadObject1 obj1 = new threadObject1(this, loopamountA); | ||
38 | threadObject1 obj2 = new threadObject1(this, loopamountB); | ||
39 | threadObject1 obj3 = new threadObject1(this, loopamountC); | ||
40 | threadObject1 obj4 = new threadObject1(this, loopamountD); | ||
41 | for (int i=0;i<1;i++) | ||
42 | { | ||
43 | sharedQueue.Enqueue(i); | ||
44 | } | ||
45 | |||
46 | Thread thr = new Thread(obj1.thread1Action); | ||
47 | Thread thr2 = new Thread(obj2.thread1Action); | ||
48 | Thread thr3 = new Thread(obj3.thread1Action); | ||
49 | Thread thr4 = new Thread(obj4.thread1Action); | ||
50 | thr.Start(); | ||
51 | thr2.Start(); | ||
52 | thr3.Start(); | ||
53 | thr4.Start(); | ||
54 | |||
55 | thr.Join(); | ||
56 | thr2.Join(); | ||
57 | thr3.Join(); | ||
58 | thr4.Join(); | ||
59 | |||
60 | Assert.That(sharedQueue.Count == 1); | ||
61 | int result = 0; | ||
62 | sharedQueue.Dequeue(out result); | ||
63 | 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); | ||
64 | |||
65 | } | ||
66 | |||
67 | /* This test fails. Need clarification if this should work | ||
68 | [Test] | ||
69 | public void ThreadedAdvancedEnqueueDequeueTest() | ||
70 | { | ||
71 | int loopamountA = 5000; | ||
72 | int loopamountB = 5000; | ||
73 | int loopamountC = 5000; | ||
74 | int loopamountD = 5000; | ||
75 | |||
76 | threadObject1 obj1 = new threadObject1(this, loopamountA); | ||
77 | threadObject2 obj2 = new threadObject2(this, loopamountB); | ||
78 | threadObject1 obj3 = new threadObject1(this, loopamountC); | ||
79 | threadObject2 obj4 = new threadObject2(this, loopamountD); | ||
80 | for (int i = 0; i < 1; i++) | ||
81 | { | ||
82 | sharedQueue.Enqueue(i); | ||
83 | } | ||
84 | |||
85 | Thread thr = new Thread(obj1.thread1Action); | ||
86 | Thread thr2 = new Thread(obj2.thread1Action); | ||
87 | Thread thr3 = new Thread(obj3.thread1Action); | ||
88 | Thread thr4 = new Thread(obj4.thread1Action); | ||
89 | thr.Start(); | ||
90 | thr2.Start(); | ||
91 | thr3.Start(); | ||
92 | thr4.Start(); | ||
93 | |||
94 | thr.Join(); | ||
95 | thr2.Join(); | ||
96 | thr3.Join(); | ||
97 | thr4.Join(); | ||
98 | |||
99 | Assert.That(sharedQueue.Count == 1); | ||
100 | int result = 0; | ||
101 | sharedQueue.Dequeue(out result); | ||
102 | 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); | ||
103 | |||
104 | } | ||
105 | */ | ||
106 | } | ||
107 | // Dequeue one from the locklessqueue add one to it and enqueue it again. | ||
108 | public class threadObject1 | ||
109 | { | ||
110 | private LocklessQueueTests m_tests; | ||
111 | private int m_loopamount = 0; | ||
112 | public threadObject1(LocklessQueueTests tst, int loopamount) | ||
113 | { | ||
114 | m_tests = tst; | ||
115 | m_loopamount = loopamount; | ||
116 | } | ||
117 | public void thread1Action(object o) | ||
118 | { | ||
119 | for (int i=0;i<m_loopamount;i++) | ||
120 | { | ||
121 | int j = 0; | ||
122 | m_tests.sharedQueue.Dequeue(out j); | ||
123 | m_tests.sharedQueue.Enqueue(++j); | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | // Dequeue one from the locklessqueue subtract one from it and enqueue it again. | ||
128 | public class threadObject2 | ||
129 | { | ||
130 | private LocklessQueueTests m_tests; | ||
131 | private int m_loopamount = 0; | ||
132 | public threadObject2(LocklessQueueTests tst, int loopamount) | ||
133 | { | ||
134 | m_tests = tst; | ||
135 | m_loopamount = loopamount; | ||
136 | } | ||
137 | public void thread1Action(object o) | ||
138 | { | ||
139 | for (int i = 0; i < m_loopamount; i++) | ||
140 | { | ||
141 | int j = 0; | ||
142 | m_tests.sharedQueue.Dequeue(out j); | ||
143 | m_tests.sharedQueue.Enqueue(--j); | ||
144 | } | ||
145 | } | ||
146 | } | ||
147 | } | ||