1
0

EntityManagerTests.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 OpenSim 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. using NUnit.Framework.SyntaxHelpers;
  28. using System;
  29. using System.Threading;
  30. using System.Text;
  31. using System.Collections.Generic;
  32. using Nini.Config;
  33. using NUnit.Framework;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Communications;
  36. using OpenSim.Region.Environment.Scenes;
  37. using OpenMetaverse;
  38. namespace OpenSim.Region.Environment.Scenes.Tests
  39. {
  40. /// <summary>
  41. /// Scene oriented tests
  42. /// </summary>
  43. [TestFixture]
  44. public class EntityManagerTests
  45. {
  46. static public Random random;
  47. SceneObjectGroup found;
  48. Scene scene = SceneTestUtils.SetupScene();
  49. [Test]
  50. public void T010_AddObjects()
  51. {
  52. random = new Random();
  53. SceneObjectGroup found;
  54. EntityManager entman = new EntityManager();
  55. SceneObjectGroup sog = NewSOG();
  56. UUID obj1 = sog.UUID;
  57. uint li1 = sog.LocalId;
  58. entman.Add(sog);
  59. sog = NewSOG();
  60. UUID obj2 = sog.UUID;
  61. uint li2 = sog.LocalId;
  62. entman.Add(sog);
  63. found = (SceneObjectGroup)entman[obj1];
  64. Assert.That(found.UUID ,Is.EqualTo(obj1) );
  65. found = (SceneObjectGroup)entman[li1];
  66. Assert.That(found.UUID ,Is.EqualTo(obj1) );
  67. found = (SceneObjectGroup)entman[obj2];
  68. Assert.That(found.UUID ,Is.EqualTo(obj2) );
  69. found = (SceneObjectGroup)entman[li2];
  70. Assert.That(found.UUID ,Is.EqualTo(obj2) );
  71. entman.Remove(obj1);
  72. entman.Remove(li2);
  73. Assert.That(entman.ContainsKey(obj1), Is.False);
  74. Assert.That(entman.ContainsKey(li1), Is.False);
  75. Assert.That(entman.ContainsKey(obj2), Is.False);
  76. Assert.That(entman.ContainsKey(li2), Is.False);
  77. }
  78. [Test]
  79. public void T011_ThreadAddRemoveTest()
  80. {
  81. // This test adds and removes with mutiple threads, attempting to break the
  82. // uuid and localid dictionary coherence.
  83. EntityManager entman = new EntityManager();
  84. SceneObjectGroup sog = NewSOG();
  85. for (int j=0; j<20; j++)
  86. {
  87. List<Thread> trdlist = new List<Thread>();
  88. for (int i=0; i<4; i++)
  89. {
  90. // Adds scene object
  91. NewTestThreads test = new NewTestThreads(entman,sog);
  92. Thread start = new Thread(new ThreadStart(test.TestAddSceneObject));
  93. start.Start();
  94. trdlist.Add(start);
  95. // Removes it
  96. test = new NewTestThreads(entman,sog);
  97. start = new Thread(new ThreadStart(test.TestRemoveSceneObject));
  98. start.Start();
  99. trdlist.Add(start);
  100. }
  101. foreach (Thread thread in trdlist)
  102. {
  103. thread.Join();
  104. }
  105. if (entman.ContainsKey(sog.UUID) || entman.ContainsKey(sog.LocalId)) {
  106. found = (SceneObjectGroup)entman[sog.UUID];
  107. Assert.That(found.UUID,Is.EqualTo(sog.UUID));
  108. found = (SceneObjectGroup)entman[sog.LocalId];
  109. Assert.That(found.UUID,Is.EqualTo(sog.UUID));
  110. }
  111. }
  112. }
  113. private SceneObjectGroup NewSOG()
  114. {
  115. SceneObjectGroup sog = new SceneObjectGroup();
  116. SceneObjectPart sop = new SceneObjectPart(UUID.Random(), PrimitiveBaseShape.Default, Vector3.Zero, Quaternion.Identity, Vector3.Zero);
  117. sop.Name = RandomName();
  118. sop.Description = sop.Name;
  119. sop.Text = RandomName();
  120. sop.SitName = RandomName();
  121. sop.TouchName = RandomName();
  122. sop.ObjectFlags |= (uint)PrimFlags.Phantom;
  123. sog.SetRootPart(sop);
  124. scene.AddNewSceneObject(sog, false);
  125. return sog;
  126. }
  127. private static string RandomName()
  128. {
  129. StringBuilder name = new StringBuilder();
  130. int size = random.Next(40,80);
  131. char ch ;
  132. for (int i=0; i<size; i++)
  133. {
  134. ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))) ;
  135. name.Append(ch);
  136. }
  137. return name.ToString();
  138. }
  139. }
  140. public class NewTestThreads
  141. {
  142. private EntityManager entman;
  143. private SceneObjectGroup sog;
  144. private Random random;
  145. public NewTestThreads(EntityManager entman, SceneObjectGroup sog)
  146. {
  147. this.entman = entman;
  148. this.sog = sog;
  149. this.random = new Random();
  150. }
  151. public void TestAddSceneObject()
  152. {
  153. Thread.Sleep(random.Next(0,50));
  154. entman.Add(sog);
  155. }
  156. public void TestRemoveSceneObject()
  157. {
  158. Thread.Sleep(random.Next(0,50));
  159. entman.Remove(sog.UUID);
  160. }
  161. }
  162. }