1
0

ScenePresenceTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using System.Threading;
  32. using System.Timers;
  33. using Timer=System.Timers.Timer;
  34. using Nini.Config;
  35. using NUnit.Framework;
  36. using NUnit.Framework.SyntaxHelpers;
  37. using OpenMetaverse;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Communications;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Interregion;
  43. using OpenSim.Region.CoreModules.World.Serialiser;
  44. using OpenSim.Tests.Common;
  45. using OpenSim.Tests.Common.Mock;
  46. using OpenSim.Tests.Common.Setup;
  47. namespace OpenSim.Region.Framework.Scenes.Tests
  48. {
  49. /// <summary>
  50. /// Scene presence tests
  51. /// </summary>
  52. [TestFixture]
  53. public class ScenePresenceTests
  54. {
  55. public Scene scene, scene2, scene3;
  56. public UUID agent1, agent2, agent3;
  57. public static Random random;
  58. public ulong region1,region2,region3;
  59. public TestCommunicationsManager cm;
  60. public AgentCircuitData acd1;
  61. public SceneObjectGroup sog1, sog2, sog3;
  62. public TestClient testclient;
  63. [TestFixtureSetUp]
  64. public void Init()
  65. {
  66. cm = new TestCommunicationsManager();
  67. scene = SceneSetupHelpers.SetupScene("Neighbour x", UUID.Random(), 1000, 1000, cm);
  68. scene2 = SceneSetupHelpers.SetupScene("Neighbour x+1", UUID.Random(), 1001, 1000, cm);
  69. scene3 = SceneSetupHelpers.SetupScene("Neighbour x-1", UUID.Random(), 999, 1000, cm);
  70. ISharedRegionModule interregionComms = new RESTInterregionComms();
  71. interregionComms.Initialise(new IniConfigSource());
  72. interregionComms.PostInitialise();
  73. SceneSetupHelpers.SetupSceneModules(scene, new IniConfigSource(), interregionComms);
  74. SceneSetupHelpers.SetupSceneModules(scene2, new IniConfigSource(), interregionComms);
  75. SceneSetupHelpers.SetupSceneModules(scene3, new IniConfigSource(), interregionComms);
  76. agent1 = UUID.Random();
  77. agent2 = UUID.Random();
  78. agent3 = UUID.Random();
  79. random = new Random();
  80. sog1 = NewSOG(UUID.Random(), scene, agent1);
  81. sog2 = NewSOG(UUID.Random(), scene, agent1);
  82. sog3 = NewSOG(UUID.Random(), scene, agent1);
  83. //ulong neighbourHandle = Utils.UIntsToLong((uint)(neighbourx * Constants.RegionSize), (uint)(neighboury * Constants.RegionSize));
  84. region1 = scene.RegionInfo.RegionHandle;
  85. region2 = scene2.RegionInfo.RegionHandle;
  86. region3 = scene3.RegionInfo.RegionHandle;
  87. }
  88. /// <summary>
  89. /// Test adding a root agent to a scene. Doesn't yet actually complete crossing the agent into the scene.
  90. /// </summary>
  91. [Test]
  92. public void T010_TestAddRootAgent()
  93. {
  94. TestHelper.InMethod();
  95. string firstName = "testfirstname";
  96. AgentCircuitData agent = new AgentCircuitData();
  97. agent.AgentID = agent1;
  98. agent.firstname = firstName;
  99. agent.lastname = "testlastname";
  100. agent.SessionID = UUID.Zero;
  101. agent.SecureSessionID = UUID.Zero;
  102. agent.circuitcode = 123;
  103. agent.BaseFolder = UUID.Zero;
  104. agent.InventoryFolder = UUID.Zero;
  105. agent.startpos = Vector3.Zero;
  106. agent.CapsPath = GetRandomCapsObjectPath();
  107. string reason;
  108. scene.NewUserConnection(agent, out reason);
  109. testclient = new TestClient(agent, scene);
  110. scene.AddNewClient(testclient);
  111. ScenePresence presence = scene.GetScenePresence(agent1);
  112. Assert.That(presence, Is.Not.Null, "presence is null");
  113. Assert.That(presence.Firstname, Is.EqualTo(firstName), "First name not same");
  114. acd1 = agent;
  115. }
  116. /// <summary>
  117. /// Test removing an uncrossed root agent from a scene.
  118. /// </summary>
  119. [Test]
  120. public void T011_TestRemoveRootAgent()
  121. {
  122. TestHelper.InMethod();
  123. scene.RemoveClient(agent1);
  124. ScenePresence presence = scene.GetScenePresence(agent1);
  125. Assert.That(presence, Is.Null, "presence is not null");
  126. }
  127. [Test]
  128. public void T012_TestAddNeighbourRegion()
  129. {
  130. TestHelper.InMethod();
  131. string reason;
  132. scene.NewUserConnection(acd1, out reason);
  133. scene.AddNewClient(testclient);
  134. ScenePresence presence = scene.GetScenePresence(agent1);
  135. presence.MakeRootAgent(new Vector3(90,90,90),false);
  136. string cap = presence.ControllingClient.RequestClientInfo().CapsPath;
  137. presence.AddNeighbourRegion(region2, cap);
  138. presence.AddNeighbourRegion(region3, cap);
  139. List<ulong> neighbours = presence.GetKnownRegionList();
  140. Assert.That(neighbours.Count, Is.EqualTo(2));
  141. }
  142. [Test]
  143. public void T013_TestRemoveNeighbourRegion()
  144. {
  145. TestHelper.InMethod();
  146. ScenePresence presence = scene.GetScenePresence(agent1);
  147. presence.RemoveNeighbourRegion(region3);
  148. List<ulong> neighbours = presence.GetKnownRegionList();
  149. Assert.That(neighbours.Count,Is.EqualTo(1));
  150. /*
  151. presence.MakeChildAgent;
  152. presence.MakeRootAgent;
  153. CompleteAvatarMovement
  154. */
  155. }
  156. [Test]
  157. public void T020_TestMakeRootAgent()
  158. {
  159. TestHelper.InMethod();
  160. ScenePresence presence = scene.GetScenePresence(agent1);
  161. Assert.That(presence.IsChildAgent, Is.False, "Starts out as a root agent");
  162. presence.MakeChildAgent();
  163. Assert.That(presence.IsChildAgent, Is.True, "Did not change to child agent after MakeChildAgent");
  164. // Accepts 0 but rejects Constants.RegionSize
  165. Vector3 pos = new Vector3(0,Constants.RegionSize-1,0);
  166. presence.MakeRootAgent(pos,true);
  167. Assert.That(presence.IsChildAgent, Is.False, "Did not go back to root agent");
  168. Assert.That(presence.AbsolutePosition, Is.EqualTo(pos), "Position is not the same one entered");
  169. }
  170. [Test]
  171. public void T021_TestCrossToNewRegion()
  172. {
  173. TestHelper.InMethod();
  174. // Adding child agent to region 1001
  175. string reason;
  176. scene2.NewUserConnection(acd1, out reason);
  177. scene2.AddNewClient(testclient);
  178. ScenePresence presence = scene.GetScenePresence(agent1);
  179. ScenePresence presence2 = scene2.GetScenePresence(agent1);
  180. // Adding neighbour region caps info to presence2
  181. string cap = presence.ControllingClient.RequestClientInfo().CapsPath;
  182. presence2.AddNeighbourRegion(region1, cap);
  183. scene.RegisterRegionWithGrid();
  184. scene2.RegisterRegionWithGrid();
  185. Assert.That(presence.IsChildAgent, Is.False, "Did not start root in origin region.");
  186. Assert.That(presence2.IsChildAgent, Is.True, "Is not a child on destination region.");
  187. // Cross to x+1
  188. presence.AbsolutePosition = new Vector3(Constants.RegionSize+1,3,100);
  189. presence.Update();
  190. EventWaitHandle wh = new EventWaitHandle (false, EventResetMode.AutoReset, "Crossing");
  191. // Mimicking communication between client and server, by waiting OK from client
  192. // sent by TestClient.CrossRegion call. Originally, this is network comm.
  193. if (!wh.WaitOne(5000,false))
  194. {
  195. presence.Update();
  196. if (!wh.WaitOne(8000,false))
  197. throw new ArgumentException("1 - Timeout waiting for signal/variable.");
  198. }
  199. // This is a TestClient specific method that fires OnCompleteMovementToRegion event, which
  200. // would normally be fired after receiving the reply packet from comm. done on the last line.
  201. testclient.CompleteMovement();
  202. // Crossings are asynchronous
  203. int timer = 10;
  204. // Make sure cross hasn't already finished
  205. if (!presence.IsInTransit && !presence.IsChildAgent)
  206. {
  207. // If not and not in transit yet, give it some more time
  208. Thread.Sleep(5000);
  209. }
  210. // Enough time, should at least be in transit by now.
  211. while (presence.IsInTransit && timer > 0)
  212. {
  213. Thread.Sleep(1000);
  214. timer-=1;
  215. }
  216. Assert.That(timer,Is.GreaterThan(0),"Timed out waiting to cross 2->1.");
  217. Assert.That(presence.IsChildAgent, Is.True, "Did not complete region cross as expected.");
  218. Assert.That(presence2.IsChildAgent, Is.False, "Did not receive root status after receiving agent.");
  219. // Cross Back
  220. presence2.AbsolutePosition = new Vector3(-10, 3, 100);
  221. presence2.Update();
  222. if (!wh.WaitOne(5000,false))
  223. {
  224. presence2.Update();
  225. if (!wh.WaitOne(8000,false))
  226. throw new ArgumentException("2 - Timeout waiting for signal/variable.");
  227. }
  228. testclient.CompleteMovement();
  229. if (!presence2.IsInTransit && !presence2.IsChildAgent)
  230. {
  231. // If not and not in transit yet, give it some more time
  232. Thread.Sleep(5000);
  233. }
  234. // Enough time, should at least be in transit by now.
  235. while (presence2.IsInTransit && timer > 0)
  236. {
  237. Thread.Sleep(1000);
  238. timer-=1;
  239. }
  240. Assert.That(timer,Is.GreaterThan(0),"Timed out waiting to cross 1->2.");
  241. Assert.That(presence2.IsChildAgent, Is.True, "Did not return from region as expected.");
  242. Assert.That(presence.IsChildAgent, Is.False, "Presence was not made root in old region again.");
  243. }
  244. [Test]
  245. public void T030_TestAddAttachments()
  246. {
  247. TestHelper.InMethod();
  248. ScenePresence presence = scene.GetScenePresence(agent1);
  249. presence.AddAttachment(sog1);
  250. presence.AddAttachment(sog2);
  251. presence.AddAttachment(sog3);
  252. Assert.That(presence.HasAttachments(), Is.True);
  253. Assert.That(presence.ValidateAttachments(), Is.True);
  254. }
  255. [Test]
  256. public void T031_RemoveAttachments()
  257. {
  258. TestHelper.InMethod();
  259. ScenePresence presence = scene.GetScenePresence(agent1);
  260. presence.RemoveAttachment(sog1);
  261. presence.RemoveAttachment(sog2);
  262. presence.RemoveAttachment(sog3);
  263. Assert.That(presence.HasAttachments(), Is.False);
  264. }
  265. [Test]
  266. public void T032_CrossAttachments()
  267. {
  268. TestHelper.InMethod();
  269. ScenePresence presence = scene.GetScenePresence(agent1);
  270. ScenePresence presence2 = scene2.GetScenePresence(agent1);
  271. presence2.AddAttachment(sog1);
  272. presence2.AddAttachment(sog2);
  273. IRegionModule serialiser = new SerialiserModule();
  274. SceneSetupHelpers.SetupSceneModules(scene, new IniConfigSource(), serialiser);
  275. SceneSetupHelpers.SetupSceneModules(scene2, new IniConfigSource(), serialiser);
  276. Assert.That(presence.HasAttachments(), Is.False, "Presence has attachments before cross");
  277. Assert.That(presence2.CrossAttachmentsIntoNewRegion(region1, true), Is.True, "Cross was not successful");
  278. Assert.That(presence2.HasAttachments(), Is.False, "Presence2 objects were not deleted");
  279. Assert.That(presence.HasAttachments(), Is.True, "Presence has not received new objects");
  280. }
  281. public static string GetRandomCapsObjectPath()
  282. {
  283. TestHelper.InMethod();
  284. UUID caps = UUID.Random();
  285. string capsPath = caps.ToString();
  286. capsPath = capsPath.Remove(capsPath.Length - 4, 4);
  287. return capsPath;
  288. }
  289. private SceneObjectGroup NewSOG(UUID uuid, Scene scene, UUID agent)
  290. {
  291. SceneObjectPart sop = new SceneObjectPart();
  292. sop.Name = RandomName();
  293. sop.Description = RandomName();
  294. sop.Text = RandomName();
  295. sop.SitName = RandomName();
  296. sop.TouchName = RandomName();
  297. sop.UUID = uuid;
  298. sop.Shape = PrimitiveBaseShape.Default;
  299. sop.Shape.State = 1;
  300. sop.OwnerID = agent;
  301. SceneObjectGroup sog = new SceneObjectGroup();
  302. sog.SetScene(scene);
  303. sog.SetRootPart(sop);
  304. return sog;
  305. }
  306. private static string RandomName()
  307. {
  308. StringBuilder name = new StringBuilder();
  309. int size = random.Next(5,12);
  310. char ch ;
  311. for (int i=0; i<size; i++)
  312. {
  313. ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))) ;
  314. name.Append(ch);
  315. }
  316. return name.ToString();
  317. }
  318. }
  319. }