EntityManagerTests.cs 6.6 KB

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