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 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.Framework.Communications;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Tests.Common;
  39. namespace OpenSim.Region.Framework.Scenes.Tests
  40. {
  41. [TestFixture, LongRunning]
  42. public class EntityManagerTests
  43. {
  44. static public Random random;
  45. SceneObjectGroup found;
  46. Scene scene = new SceneHelpers().SetupScene();
  47. [Test]
  48. public void T010_AddObjects()
  49. {
  50. TestHelpers.InMethod();
  51. random = new Random();
  52. SceneObjectGroup found;
  53. EntityManager entman = new EntityManager();
  54. SceneObjectGroup sog = NewSOG();
  55. UUID obj1 = sog.UUID;
  56. uint li1 = sog.LocalId;
  57. entman.Add(sog);
  58. sog = NewSOG();
  59. UUID obj2 = sog.UUID;
  60. uint li2 = sog.LocalId;
  61. entman.Add(sog);
  62. found = (SceneObjectGroup)entman[obj1];
  63. Assert.That(found.UUID ,Is.EqualTo(obj1));
  64. found = (SceneObjectGroup)entman[li1];
  65. Assert.That(found.UUID ,Is.EqualTo(obj1));
  66. found = (SceneObjectGroup)entman[obj2];
  67. Assert.That(found.UUID ,Is.EqualTo(obj2));
  68. found = (SceneObjectGroup)entman[li2];
  69. Assert.That(found.UUID ,Is.EqualTo(obj2));
  70. entman.Remove(obj1);
  71. entman.Remove(li2);
  72. Assert.That(entman.ContainsKey(obj1), Is.False);
  73. Assert.That(entman.ContainsKey(li1), Is.False);
  74. Assert.That(entman.ContainsKey(obj2), Is.False);
  75. Assert.That(entman.ContainsKey(li2), Is.False);
  76. }
  77. [Test]
  78. public void T011_ThreadAddRemoveTest()
  79. {
  80. TestHelpers.InMethod();
  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. SceneObjectPart sop = new SceneObjectPart(UUID.Random(), PrimitiveBaseShape.Default, Vector3.Zero, Quaternion.Identity, Vector3.Zero);
  116. sop.Name = RandomName();
  117. sop.Description = sop.Name;
  118. sop.Text = RandomName();
  119. sop.SitName = RandomName();
  120. sop.TouchName = RandomName();
  121. sop.Flags |= PrimFlags.Phantom;
  122. SceneObjectGroup sog = new SceneObjectGroup(sop);
  123. scene.AddNewSceneObject(sog, false);
  124. return sog;
  125. }
  126. private static string RandomName()
  127. {
  128. StringBuilder name = new StringBuilder();
  129. int size = random.Next(40,80);
  130. char ch ;
  131. for (int i=0; i<size; i++)
  132. {
  133. ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))) ;
  134. name.Append(ch);
  135. }
  136. return name.ToString();
  137. }
  138. }
  139. public class NewTestThreads
  140. {
  141. private EntityManager entman;
  142. private SceneObjectGroup sog;
  143. private Random random;
  144. public NewTestThreads(EntityManager entman, SceneObjectGroup sog)
  145. {
  146. this.entman = entman;
  147. this.sog = sog;
  148. this.random = new Random();
  149. }
  150. public void TestAddSceneObject()
  151. {
  152. Thread.Sleep(random.Next(0,50));
  153. entman.Add(sog);
  154. }
  155. public void TestRemoveSceneObject()
  156. {
  157. Thread.Sleep(random.Next(0,50));
  158. entman.Remove(sog.UUID);
  159. }
  160. }
  161. }