EntityManagerTests.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. using System;
  28. using System.Reflection;
  29. using System.Threading;
  30. using System.Text;
  31. using System.Collections.Generic;
  32. using Nini.Config;
  33. using NUnit.Framework;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Tests.Common;
  38. namespace OpenSim.Region.Framework.Scenes.Tests
  39. {
  40. [TestFixture, LongRunning]
  41. public class EntityManagerTests : OpenSimTestCase
  42. {
  43. static public Random random;
  44. SceneObjectGroup found;
  45. Scene scene = new SceneHelpers().SetupScene();
  46. [Test]
  47. public void T010_AddObjects()
  48. {
  49. TestHelpers.InMethod();
  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. TestHelpers.InMethod();
  80. // This test adds and removes with mutiple threads, attempting to break the
  81. // uuid and localid dictionary coherence.
  82. EntityManager entman = new EntityManager();
  83. SceneObjectGroup sog = NewSOG();
  84. for (int j=0; j<20; j++)
  85. {
  86. List<Thread> trdlist = new List<Thread>();
  87. for (int i=0; i<4; i++)
  88. {
  89. // Adds scene object
  90. NewTestThreads test = new NewTestThreads(entman,sog);
  91. Thread start = new Thread(new ThreadStart(test.TestAddSceneObject));
  92. start.Start();
  93. trdlist.Add(start);
  94. // Removes it
  95. test = new NewTestThreads(entman,sog);
  96. start = new Thread(new ThreadStart(test.TestRemoveSceneObject));
  97. start.Start();
  98. trdlist.Add(start);
  99. }
  100. foreach (Thread thread in trdlist)
  101. {
  102. thread.Join();
  103. }
  104. if (entman.ContainsKey(sog.UUID) || entman.ContainsKey(sog.LocalId)) {
  105. found = (SceneObjectGroup)entman[sog.UUID];
  106. Assert.That(found.UUID,Is.EqualTo(sog.UUID));
  107. found = (SceneObjectGroup)entman[sog.LocalId];
  108. Assert.That(found.UUID,Is.EqualTo(sog.UUID));
  109. }
  110. }
  111. }
  112. private SceneObjectGroup NewSOG()
  113. {
  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.Flags |= PrimFlags.Phantom;
  121. SceneObjectGroup sog = new SceneObjectGroup(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. }