diff options
Diffstat (limited to 'OpenSim/Region/Framework/Scenes/Tests/EntityManagerTests.cs')
-rw-r--r-- | OpenSim/Region/Framework/Scenes/Tests/EntityManagerTests.cs | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/OpenSim/Region/Framework/Scenes/Tests/EntityManagerTests.cs b/OpenSim/Region/Framework/Scenes/Tests/EntityManagerTests.cs new file mode 100644 index 0000000..766ce83 --- /dev/null +++ b/OpenSim/Region/Framework/Scenes/Tests/EntityManagerTests.cs | |||
@@ -0,0 +1,178 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Reflection; | ||
30 | using System.Threading; | ||
31 | using System.Text; | ||
32 | using System.Collections.Generic; | ||
33 | using Nini.Config; | ||
34 | using NUnit.Framework; | ||
35 | using OpenMetaverse; | ||
36 | using OpenSim.Framework; | ||
37 | using OpenSim.Framework.Communications; | ||
38 | using OpenSim.Region.Framework.Scenes; | ||
39 | using OpenSim.Tests.Common; | ||
40 | |||
41 | namespace OpenSim.Region.Framework.Scenes.Tests | ||
42 | { | ||
43 | [TestFixture, LongRunning] | ||
44 | public class EntityManagerTests : OpenSimTestCase | ||
45 | { | ||
46 | static public Random random; | ||
47 | SceneObjectGroup found; | ||
48 | Scene scene = new SceneHelpers().SetupScene(); | ||
49 | |||
50 | [Test] | ||
51 | public void T010_AddObjects() | ||
52 | { | ||
53 | TestHelpers.InMethod(); | ||
54 | |||
55 | random = new Random(); | ||
56 | SceneObjectGroup found; | ||
57 | EntityManager entman = new EntityManager(); | ||
58 | SceneObjectGroup sog = NewSOG(); | ||
59 | UUID obj1 = sog.UUID; | ||
60 | uint li1 = sog.LocalId; | ||
61 | entman.Add(sog); | ||
62 | sog = NewSOG(); | ||
63 | UUID obj2 = sog.UUID; | ||
64 | uint li2 = sog.LocalId; | ||
65 | entman.Add(sog); | ||
66 | |||
67 | found = (SceneObjectGroup)entman[obj1]; | ||
68 | Assert.That(found.UUID ,Is.EqualTo(obj1)); | ||
69 | found = (SceneObjectGroup)entman[li1]; | ||
70 | Assert.That(found.UUID ,Is.EqualTo(obj1)); | ||
71 | found = (SceneObjectGroup)entman[obj2]; | ||
72 | Assert.That(found.UUID ,Is.EqualTo(obj2)); | ||
73 | found = (SceneObjectGroup)entman[li2]; | ||
74 | Assert.That(found.UUID ,Is.EqualTo(obj2)); | ||
75 | |||
76 | entman.Remove(obj1); | ||
77 | entman.Remove(li2); | ||
78 | |||
79 | Assert.That(entman.ContainsKey(obj1), Is.False); | ||
80 | Assert.That(entman.ContainsKey(li1), Is.False); | ||
81 | Assert.That(entman.ContainsKey(obj2), Is.False); | ||
82 | Assert.That(entman.ContainsKey(li2), Is.False); | ||
83 | } | ||
84 | |||
85 | [Test] | ||
86 | public void T011_ThreadAddRemoveTest() | ||
87 | { | ||
88 | TestHelpers.InMethod(); | ||
89 | |||
90 | // This test adds and removes with mutiple threads, attempting to break the | ||
91 | // uuid and localid dictionary coherence. | ||
92 | EntityManager entman = new EntityManager(); | ||
93 | SceneObjectGroup sog = NewSOG(); | ||
94 | for (int j=0; j<20; j++) | ||
95 | { | ||
96 | List<Thread> trdlist = new List<Thread>(); | ||
97 | |||
98 | for (int i=0; i<4; i++) | ||
99 | { | ||
100 | // Adds scene object | ||
101 | NewTestThreads test = new NewTestThreads(entman,sog); | ||
102 | Thread start = new Thread(new ThreadStart(test.TestAddSceneObject)); | ||
103 | start.Start(); | ||
104 | trdlist.Add(start); | ||
105 | |||
106 | // Removes it | ||
107 | test = new NewTestThreads(entman,sog); | ||
108 | start = new Thread(new ThreadStart(test.TestRemoveSceneObject)); | ||
109 | start.Start(); | ||
110 | trdlist.Add(start); | ||
111 | } | ||
112 | foreach (Thread thread in trdlist) | ||
113 | { | ||
114 | thread.Join(); | ||
115 | } | ||
116 | if (entman.ContainsKey(sog.UUID) || entman.ContainsKey(sog.LocalId)) { | ||
117 | found = (SceneObjectGroup)entman[sog.UUID]; | ||
118 | Assert.That(found.UUID,Is.EqualTo(sog.UUID)); | ||
119 | found = (SceneObjectGroup)entman[sog.LocalId]; | ||
120 | Assert.That(found.UUID,Is.EqualTo(sog.UUID)); | ||
121 | } | ||
122 | } | ||
123 | } | ||
124 | |||
125 | private SceneObjectGroup NewSOG() | ||
126 | { | ||
127 | SceneObjectPart sop = new SceneObjectPart(UUID.Random(), PrimitiveBaseShape.Default, Vector3.Zero, Quaternion.Identity, Vector3.Zero); | ||
128 | sop.Name = RandomName(); | ||
129 | sop.Description = sop.Name; | ||
130 | sop.Text = RandomName(); | ||
131 | sop.SitName = RandomName(); | ||
132 | sop.TouchName = RandomName(); | ||
133 | sop.Flags |= PrimFlags.Phantom; | ||
134 | |||
135 | SceneObjectGroup sog = new SceneObjectGroup(sop); | ||
136 | scene.AddNewSceneObject(sog, false); | ||
137 | |||
138 | return sog; | ||
139 | } | ||
140 | |||
141 | private static string RandomName() | ||
142 | { | ||
143 | StringBuilder name = new StringBuilder(); | ||
144 | int size = random.Next(40,80); | ||
145 | char ch ; | ||
146 | for (int i=0; i<size; i++) | ||
147 | { | ||
148 | ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))) ; | ||
149 | name.Append(ch); | ||
150 | } | ||
151 | return name.ToString(); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | public class NewTestThreads | ||
156 | { | ||
157 | private EntityManager entman; | ||
158 | private SceneObjectGroup sog; | ||
159 | private Random random; | ||
160 | |||
161 | public NewTestThreads(EntityManager entman, SceneObjectGroup sog) | ||
162 | { | ||
163 | this.entman = entman; | ||
164 | this.sog = sog; | ||
165 | this.random = new Random(); | ||
166 | } | ||
167 | public void TestAddSceneObject() | ||
168 | { | ||
169 | Thread.Sleep(random.Next(0,50)); | ||
170 | entman.Add(sog); | ||
171 | } | ||
172 | public void TestRemoveSceneObject() | ||
173 | { | ||
174 | Thread.Sleep(random.Next(0,50)); | ||
175 | entman.Remove(sog.UUID); | ||
176 | } | ||
177 | } | ||
178 | } \ No newline at end of file | ||