ScenePresenceTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. agent.ChildrenCapSeeds = new Dictionary<ulong, string>();
  108. string reason;
  109. scene.NewUserConnection(agent, out reason);
  110. testclient = new TestClient(agent, scene);
  111. scene.AddNewClient(testclient);
  112. ScenePresence presence = scene.GetScenePresence(agent1);
  113. Assert.That(presence, Is.Not.Null, "presence is null");
  114. Assert.That(presence.Firstname, Is.EqualTo(firstName), "First name not same");
  115. acd1 = agent;
  116. }
  117. /// <summary>
  118. /// Test removing an uncrossed root agent from a scene.
  119. /// </summary>
  120. [Test]
  121. public void T011_TestRemoveRootAgent()
  122. {
  123. TestHelper.InMethod();
  124. scene.RemoveClient(agent1);
  125. ScenePresence presence = scene.GetScenePresence(agent1);
  126. Assert.That(presence, Is.Null, "presence is not null");
  127. }
  128. [Test]
  129. public void T012_TestAddNeighbourRegion()
  130. {
  131. TestHelper.InMethod();
  132. string reason;
  133. if (acd1 == null)
  134. fixNullPresence();
  135. scene.NewUserConnection(acd1, out reason);
  136. if (testclient == null)
  137. testclient = new TestClient(acd1, scene);
  138. scene.AddNewClient(testclient);
  139. ScenePresence presence = scene.GetScenePresence(agent1);
  140. presence.MakeRootAgent(new Vector3(90,90,90),false);
  141. string cap = presence.ControllingClient.RequestClientInfo().CapsPath;
  142. presence.AddNeighbourRegion(region2, cap);
  143. presence.AddNeighbourRegion(region3, cap);
  144. List<ulong> neighbours = presence.GetKnownRegionList();
  145. Assert.That(neighbours.Count, Is.EqualTo(2));
  146. }
  147. public void fixNullPresence()
  148. {
  149. string firstName = "testfirstname";
  150. AgentCircuitData agent = new AgentCircuitData();
  151. agent.AgentID = agent1;
  152. agent.firstname = firstName;
  153. agent.lastname = "testlastname";
  154. agent.SessionID = UUID.Zero;
  155. agent.SecureSessionID = UUID.Zero;
  156. agent.circuitcode = 123;
  157. agent.BaseFolder = UUID.Zero;
  158. agent.InventoryFolder = UUID.Zero;
  159. agent.startpos = Vector3.Zero;
  160. agent.CapsPath = GetRandomCapsObjectPath();
  161. acd1 = agent;
  162. }
  163. [Test]
  164. public void T013_TestRemoveNeighbourRegion()
  165. {
  166. TestHelper.InMethod();
  167. ScenePresence presence = scene.GetScenePresence(agent1);
  168. presence.RemoveNeighbourRegion(region3);
  169. List<ulong> neighbours = presence.GetKnownRegionList();
  170. Assert.That(neighbours.Count,Is.EqualTo(1));
  171. /*
  172. presence.MakeChildAgent;
  173. presence.MakeRootAgent;
  174. CompleteAvatarMovement
  175. */
  176. }
  177. [Test]
  178. public void T020_TestMakeRootAgent()
  179. {
  180. TestHelper.InMethod();
  181. ScenePresence presence = scene.GetScenePresence(agent1);
  182. Assert.That(presence.IsChildAgent, Is.False, "Starts out as a root agent");
  183. presence.MakeChildAgent();
  184. Assert.That(presence.IsChildAgent, Is.True, "Did not change to child agent after MakeChildAgent");
  185. // Accepts 0 but rejects Constants.RegionSize
  186. Vector3 pos = new Vector3(0,Constants.RegionSize-1,0);
  187. presence.MakeRootAgent(pos,true);
  188. Assert.That(presence.IsChildAgent, Is.False, "Did not go back to root agent");
  189. Assert.That(presence.AbsolutePosition, Is.EqualTo(pos), "Position is not the same one entered");
  190. }
  191. [Test]
  192. public void T021_TestCrossToNewRegion()
  193. {
  194. TestHelper.InMethod();
  195. // Adding child agent to region 1001
  196. string reason;
  197. scene2.NewUserConnection(acd1, out reason);
  198. scene2.AddNewClient(testclient);
  199. ScenePresence presence = scene.GetScenePresence(agent1);
  200. ScenePresence presence2 = scene2.GetScenePresence(agent1);
  201. // Adding neighbour region caps info to presence2
  202. string cap = presence.ControllingClient.RequestClientInfo().CapsPath;
  203. presence2.AddNeighbourRegion(region1, cap);
  204. scene.RegisterRegionWithGrid();
  205. scene2.RegisterRegionWithGrid();
  206. Assert.That(presence.IsChildAgent, Is.False, "Did not start root in origin region.");
  207. Assert.That(presence2.IsChildAgent, Is.True, "Is not a child on destination region.");
  208. // Cross to x+1
  209. presence.AbsolutePosition = new Vector3(Constants.RegionSize+1,3,100);
  210. presence.Update();
  211. EventWaitHandle wh = new EventWaitHandle (false, EventResetMode.AutoReset, "Crossing");
  212. // Mimicking communication between client and server, by waiting OK from client
  213. // sent by TestClient.CrossRegion call. Originally, this is network comm.
  214. if (!wh.WaitOne(5000,false))
  215. {
  216. presence.Update();
  217. if (!wh.WaitOne(8000,false))
  218. throw new ArgumentException("1 - Timeout waiting for signal/variable.");
  219. }
  220. // This is a TestClient specific method that fires OnCompleteMovementToRegion event, which
  221. // would normally be fired after receiving the reply packet from comm. done on the last line.
  222. testclient.CompleteMovement();
  223. // Crossings are asynchronous
  224. int timer = 10;
  225. // Make sure cross hasn't already finished
  226. if (!presence.IsInTransit && !presence.IsChildAgent)
  227. {
  228. // If not and not in transit yet, give it some more time
  229. Thread.Sleep(5000);
  230. }
  231. // Enough time, should at least be in transit by now.
  232. while (presence.IsInTransit && timer > 0)
  233. {
  234. Thread.Sleep(1000);
  235. timer-=1;
  236. }
  237. Assert.That(timer,Is.GreaterThan(0),"Timed out waiting to cross 2->1.");
  238. Assert.That(presence.IsChildAgent, Is.True, "Did not complete region cross as expected.");
  239. Assert.That(presence2.IsChildAgent, Is.False, "Did not receive root status after receiving agent.");
  240. // Cross Back
  241. presence2.AbsolutePosition = new Vector3(-10, 3, 100);
  242. presence2.Update();
  243. if (!wh.WaitOne(5000,false))
  244. {
  245. presence2.Update();
  246. if (!wh.WaitOne(8000,false))
  247. throw new ArgumentException("2 - Timeout waiting for signal/variable.");
  248. }
  249. testclient.CompleteMovement();
  250. if (!presence2.IsInTransit && !presence2.IsChildAgent)
  251. {
  252. // If not and not in transit yet, give it some more time
  253. Thread.Sleep(5000);
  254. }
  255. // Enough time, should at least be in transit by now.
  256. while (presence2.IsInTransit && timer > 0)
  257. {
  258. Thread.Sleep(1000);
  259. timer-=1;
  260. }
  261. Assert.That(timer,Is.GreaterThan(0),"Timed out waiting to cross 1->2.");
  262. Assert.That(presence2.IsChildAgent, Is.True, "Did not return from region as expected.");
  263. Assert.That(presence.IsChildAgent, Is.False, "Presence was not made root in old region again.");
  264. }
  265. [Test]
  266. public void T030_TestAddAttachments()
  267. {
  268. TestHelper.InMethod();
  269. ScenePresence presence = scene.GetScenePresence(agent1);
  270. presence.AddAttachment(sog1);
  271. presence.AddAttachment(sog2);
  272. presence.AddAttachment(sog3);
  273. Assert.That(presence.HasAttachments(), Is.True);
  274. Assert.That(presence.ValidateAttachments(), Is.True);
  275. }
  276. [Test]
  277. public void T031_RemoveAttachments()
  278. {
  279. TestHelper.InMethod();
  280. ScenePresence presence = scene.GetScenePresence(agent1);
  281. presence.RemoveAttachment(sog1);
  282. presence.RemoveAttachment(sog2);
  283. presence.RemoveAttachment(sog3);
  284. Assert.That(presence.HasAttachments(), Is.False);
  285. }
  286. [Test]
  287. public void T032_CrossAttachments()
  288. {
  289. TestHelper.InMethod();
  290. ScenePresence presence = scene.GetScenePresence(agent1);
  291. ScenePresence presence2 = scene2.GetScenePresence(agent1);
  292. presence2.AddAttachment(sog1);
  293. presence2.AddAttachment(sog2);
  294. IRegionModule serialiser = new SerialiserModule();
  295. SceneSetupHelpers.SetupSceneModules(scene, new IniConfigSource(), serialiser);
  296. SceneSetupHelpers.SetupSceneModules(scene2, new IniConfigSource(), serialiser);
  297. Assert.That(presence.HasAttachments(), Is.False, "Presence has attachments before cross");
  298. Assert.That(presence2.CrossAttachmentsIntoNewRegion(region1, true), Is.True, "Cross was not successful");
  299. Assert.That(presence2.HasAttachments(), Is.False, "Presence2 objects were not deleted");
  300. Assert.That(presence.HasAttachments(), Is.True, "Presence has not received new objects");
  301. }
  302. public static string GetRandomCapsObjectPath()
  303. {
  304. TestHelper.InMethod();
  305. UUID caps = UUID.Random();
  306. string capsPath = caps.ToString();
  307. capsPath = capsPath.Remove(capsPath.Length - 4, 4);
  308. return capsPath;
  309. }
  310. private SceneObjectGroup NewSOG(UUID uuid, Scene scene, UUID agent)
  311. {
  312. SceneObjectPart sop = new SceneObjectPart();
  313. sop.Name = RandomName();
  314. sop.Description = RandomName();
  315. sop.Text = RandomName();
  316. sop.SitName = RandomName();
  317. sop.TouchName = RandomName();
  318. sop.UUID = uuid;
  319. sop.Shape = PrimitiveBaseShape.Default;
  320. sop.Shape.State = 1;
  321. sop.OwnerID = agent;
  322. SceneObjectGroup sog = new SceneObjectGroup();
  323. sog.SetScene(scene);
  324. sog.SetRootPart(sop);
  325. return sog;
  326. }
  327. private static string RandomName()
  328. {
  329. StringBuilder name = new StringBuilder();
  330. int size = random.Next(5,12);
  331. char ch ;
  332. for (int i=0; i<size; i++)
  333. {
  334. ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))) ;
  335. name.Append(ch);
  336. }
  337. return name.ToString();
  338. }
  339. }
  340. }