OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
EntityManagerTests.cs
Go to the documentation of this file.
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.Region.Framework.Scenes;
38 using OpenSim.Tests.Common;
39 
40 namespace OpenSim.Region.Framework.Scenes.Tests
41 {
42  [TestFixture, LongRunning]
44  {
45  static public Random random;
46  SceneObjectGroup found;
47  Scene scene = new SceneHelpers().SetupScene();
48 
49  [Test]
50  public void T010_AddObjects()
51  {
52  TestHelpers.InMethod();
53 
54  random = new Random();
55  SceneObjectGroup found;
56  EntityManager entman = new EntityManager();
57  SceneObjectGroup sog = NewSOG();
58  UUID obj1 = sog.UUID;
59  uint li1 = sog.LocalId;
60  entman.Add(sog);
61  sog = NewSOG();
62  UUID obj2 = sog.UUID;
63  uint li2 = sog.LocalId;
64  entman.Add(sog);
65 
66  found = (SceneObjectGroup)entman[obj1];
67  Assert.That(found.UUID ,Is.EqualTo(obj1));
68  found = (SceneObjectGroup)entman[li1];
69  Assert.That(found.UUID ,Is.EqualTo(obj1));
70  found = (SceneObjectGroup)entman[obj2];
71  Assert.That(found.UUID ,Is.EqualTo(obj2));
72  found = (SceneObjectGroup)entman[li2];
73  Assert.That(found.UUID ,Is.EqualTo(obj2));
74 
75  entman.Remove(obj1);
76  entman.Remove(li2);
77 
78  Assert.That(entman.ContainsKey(obj1), Is.False);
79  Assert.That(entman.ContainsKey(li1), Is.False);
80  Assert.That(entman.ContainsKey(obj2), Is.False);
81  Assert.That(entman.ContainsKey(li2), Is.False);
82  }
83 
84  [Test]
86  {
87  TestHelpers.InMethod();
88 
89  // This test adds and removes with mutiple threads, attempting to break the
90  // uuid and localid dictionary coherence.
91  EntityManager entman = new EntityManager();
92  SceneObjectGroup sog = NewSOG();
93  for (int j=0; j<20; j++)
94  {
95  List<Thread> trdlist = new List<Thread>();
96 
97  for (int i=0; i<4; i++)
98  {
99  // Adds scene object
100  NewTestThreads test = new NewTestThreads(entman,sog);
101  Thread start = new Thread(new ThreadStart(test.TestAddSceneObject));
102  start.Start();
103  trdlist.Add(start);
104 
105  // Removes it
106  test = new NewTestThreads(entman,sog);
107  start = new Thread(new ThreadStart(test.TestRemoveSceneObject));
108  start.Start();
109  trdlist.Add(start);
110  }
111  foreach (Thread thread in trdlist)
112  {
113  thread.Join();
114  }
115  if (entman.ContainsKey(sog.UUID) || entman.ContainsKey(sog.LocalId)) {
116  found = (SceneObjectGroup)entman[sog.UUID];
117  Assert.That(found.UUID,Is.EqualTo(sog.UUID));
118  found = (SceneObjectGroup)entman[sog.LocalId];
119  Assert.That(found.UUID,Is.EqualTo(sog.UUID));
120  }
121  }
122  }
123 
124  private SceneObjectGroup NewSOG()
125  {
126  SceneObjectPart sop = new SceneObjectPart(UUID.Random(), PrimitiveBaseShape.Default, Vector3.Zero, Quaternion.Identity, Vector3.Zero);
127  sop.Name = RandomName();
128  sop.Description = sop.Name;
129  sop.Text = RandomName();
130  sop.SitName = RandomName();
131  sop.TouchName = RandomName();
132  sop.Flags |= PrimFlags.Phantom;
133 
134  SceneObjectGroup sog = new SceneObjectGroup(sop);
135  scene.AddNewSceneObject(sog, false);
136 
137  return sog;
138  }
139 
140  private static string RandomName()
141  {
142  StringBuilder name = new StringBuilder();
143  int size = random.Next(40,80);
144  char ch ;
145  for (int i=0; i<size; i++)
146  {
147  ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))) ;
148  name.Append(ch);
149  }
150  return name.ToString();
151  }
152  }
153 
154  public class NewTestThreads
155  {
156  private EntityManager entman;
157  private SceneObjectGroup sog;
158  private Random random;
159 
161  {
162  this.entman = entman;
163  this.sog = sog;
164  this.random = new Random();
165  }
166  public void TestAddSceneObject()
167  {
168  Thread.Sleep(random.Next(0,50));
169  entman.Add(sog);
170  }
171  public void TestRemoveSceneObject()
172  {
173  Thread.Sleep(random.Next(0,50));
174  entman.Remove(sog.UUID);
175  }
176  }
177 }
A scene object group is conceptually an object in the scene. The object is constituted of SceneObject...
Helpers for setting up scenes.
Definition: SceneHelpers.cs:60
NewTestThreads(EntityManager entman, SceneObjectGroup sog)