EntityManagerTests.cs 7.0 KB

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