ScenePresenceAgentTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 OpenMetaverse;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Communications;
  39. using OpenSim.Region.Framework.Scenes;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.CoreModules.Framework.EntityTransfer;
  42. using OpenSim.Region.CoreModules.World.Serialiser;
  43. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Simulation;
  44. using OpenSim.Tests.Common;
  45. using OpenSim.Tests.Common.Mock;
  46. namespace OpenSim.Region.Framework.Scenes.Tests
  47. {
  48. /// <summary>
  49. /// Scene presence tests
  50. /// </summary>
  51. [TestFixture]
  52. public class ScenePresenceAgentTests
  53. {
  54. public Scene scene, scene2, scene3;
  55. public UUID agent1, agent2, agent3;
  56. public static Random random;
  57. public ulong region1,region2,region3;
  58. public AgentCircuitData acd1;
  59. public SceneObjectGroup sog1, sog2, sog3;
  60. public TestClient testclient;
  61. [TestFixtureSetUp]
  62. public void Init()
  63. {
  64. TestHelpers.InMethod();
  65. scene = SceneHelpers.SetupScene("Neighbour x", UUID.Random(), 1000, 1000);
  66. scene2 = SceneHelpers.SetupScene("Neighbour x+1", UUID.Random(), 1001, 1000);
  67. scene3 = SceneHelpers.SetupScene("Neighbour x-1", UUID.Random(), 999, 1000);
  68. ISharedRegionModule interregionComms = new LocalSimulationConnectorModule();
  69. interregionComms.Initialise(new IniConfigSource());
  70. interregionComms.PostInitialise();
  71. SceneHelpers.SetupSceneModules(scene, new IniConfigSource(), interregionComms);
  72. SceneHelpers.SetupSceneModules(scene2, new IniConfigSource(), interregionComms);
  73. SceneHelpers.SetupSceneModules(scene3, new IniConfigSource(), interregionComms);
  74. agent1 = UUID.Random();
  75. agent2 = UUID.Random();
  76. agent3 = UUID.Random();
  77. random = new Random();
  78. sog1 = SceneHelpers.CreateSceneObject(1, agent1);
  79. scene.AddSceneObject(sog1);
  80. sog2 = SceneHelpers.CreateSceneObject(1, agent1);
  81. scene.AddSceneObject(sog2);
  82. sog3 = SceneHelpers.CreateSceneObject(1, agent1);
  83. scene.AddSceneObject(sog3);
  84. region1 = scene.RegionInfo.RegionHandle;
  85. region2 = scene2.RegionInfo.RegionHandle;
  86. region3 = scene3.RegionInfo.RegionHandle;
  87. }
  88. [Test]
  89. public void TestCloseAgent()
  90. {
  91. TestHelpers.InMethod();
  92. // log4net.Config.XmlConfigurator.Configure();
  93. TestScene scene = SceneHelpers.SetupScene();
  94. ScenePresence sp = SceneHelpers.AddScenePresence(scene, TestHelpers.ParseTail(0x1));
  95. Assert.That(scene.AuthenticateHandler.GetAgentCircuitData(sp.UUID), Is.Not.Null);
  96. scene.IncomingCloseAgent(sp.UUID);
  97. Assert.That(scene.GetScenePresence(sp.UUID), Is.Null);
  98. Assert.That(scene.AuthenticateHandler.GetAgentCircuitData(sp.UUID), Is.Null);
  99. }
  100. /// <summary>
  101. /// Test that if a root agent logs into a region, a child agent is also established in the neighbouring region
  102. /// </summary>
  103. /// <remarks>
  104. /// Please note that unlike the other tests here, this doesn't rely on structures
  105. /// </remarks>
  106. [Test]
  107. public void TestChildAgentEstablished()
  108. {
  109. TestHelpers.InMethod();
  110. // log4net.Config.XmlConfigurator.Configure();
  111. UUID agent1Id = UUID.Parse("00000000-0000-0000-0000-000000000001");
  112. TestScene myScene1 = SceneHelpers.SetupScene("Neighbour y", UUID.Random(), 1000, 1000);
  113. // TestScene myScene2 = SceneHelpers.SetupScene("Neighbour y + 1", UUID.Random(), 1001, 1000);
  114. IConfigSource configSource = new IniConfigSource();
  115. configSource.AddConfig("Modules").Set("EntityTransferModule", "BasicEntityTransferModule");
  116. EntityTransferModule etm = new EntityTransferModule();
  117. SceneHelpers.SetupSceneModules(myScene1, configSource, etm);
  118. SceneHelpers.AddScenePresence(myScene1, agent1Id);
  119. // ScenePresence childPresence = myScene2.GetScenePresence(agent1);
  120. // TODO: Need to do a fair amount of work to allow synchronous establishment of child agents
  121. // Assert.That(childPresence, Is.Not.Null);
  122. // Assert.That(childPresence.IsChildAgent, Is.True);
  123. }
  124. // /// <summary>
  125. // /// Test adding a root agent to a scene. Doesn't yet actually complete crossing the agent into the scene.
  126. // /// </summary>
  127. // [Test]
  128. // public void T010_TestAddRootAgent()
  129. // {
  130. // TestHelpers.InMethod();
  131. //
  132. // string firstName = "testfirstname";
  133. //
  134. // AgentCircuitData agent = new AgentCircuitData();
  135. // agent.AgentID = agent1;
  136. // agent.firstname = firstName;
  137. // agent.lastname = "testlastname";
  138. // agent.SessionID = UUID.Random();
  139. // agent.SecureSessionID = UUID.Random();
  140. // agent.circuitcode = 123;
  141. // agent.BaseFolder = UUID.Zero;
  142. // agent.InventoryFolder = UUID.Zero;
  143. // agent.startpos = Vector3.Zero;
  144. // agent.CapsPath = GetRandomCapsObjectPath();
  145. // agent.ChildrenCapSeeds = new Dictionary<ulong, string>();
  146. // agent.child = true;
  147. //
  148. // scene.PresenceService.LoginAgent(agent.AgentID.ToString(), agent.SessionID, agent.SecureSessionID);
  149. //
  150. // string reason;
  151. // scene.NewUserConnection(agent, (uint)TeleportFlags.ViaLogin, out reason);
  152. // testclient = new TestClient(agent, scene);
  153. // scene.AddNewClient(testclient);
  154. //
  155. // ScenePresence presence = scene.GetScenePresence(agent1);
  156. //
  157. // Assert.That(presence, Is.Not.Null, "presence is null");
  158. // Assert.That(presence.Firstname, Is.EqualTo(firstName), "First name not same");
  159. // acd1 = agent;
  160. // }
  161. //
  162. // /// <summary>
  163. // /// Test removing an uncrossed root agent from a scene.
  164. // /// </summary>
  165. // [Test]
  166. // public void T011_TestRemoveRootAgent()
  167. // {
  168. // TestHelpers.InMethod();
  169. //
  170. // scene.RemoveClient(agent1);
  171. //
  172. // ScenePresence presence = scene.GetScenePresence(agent1);
  173. //
  174. // Assert.That(presence, Is.Null, "presence is not null");
  175. // }
  176. [Test]
  177. public void T012_TestAddNeighbourRegion()
  178. {
  179. TestHelpers.InMethod();
  180. string reason;
  181. if (acd1 == null)
  182. fixNullPresence();
  183. scene.NewUserConnection(acd1, 0, out reason);
  184. if (testclient == null)
  185. testclient = new TestClient(acd1, scene);
  186. scene.AddNewClient(testclient, PresenceType.User);
  187. ScenePresence presence = scene.GetScenePresence(agent1);
  188. presence.MakeRootAgent(new Vector3(90,90,90),false);
  189. string cap = presence.ControllingClient.RequestClientInfo().CapsPath;
  190. presence.AddNeighbourRegion(region2, cap);
  191. presence.AddNeighbourRegion(region3, cap);
  192. List<ulong> neighbours = presence.GetKnownRegionList();
  193. Assert.That(neighbours.Count, Is.EqualTo(2));
  194. }
  195. [Test]
  196. public void T013_TestRemoveNeighbourRegion()
  197. {
  198. TestHelpers.InMethod();
  199. ScenePresence presence = scene.GetScenePresence(agent1);
  200. presence.RemoveNeighbourRegion(region3);
  201. List<ulong> neighbours = presence.GetKnownRegionList();
  202. Assert.That(neighbours.Count,Is.EqualTo(1));
  203. /*
  204. presence.MakeChildAgent;
  205. presence.MakeRootAgent;
  206. CompleteAvatarMovement
  207. */
  208. }
  209. // I'm commenting this test because it does not represent
  210. // crossings. The Thread.Sleep's in here are not meaningful mocks,
  211. // and they sometimes fail in panda.
  212. // We need to talk in order to develop a test
  213. // that really tests region crossings. There are 3 async components,
  214. // but things are synchronous among them. So there should be
  215. // 3 threads in here.
  216. //[Test]
  217. public void T021_TestCrossToNewRegion()
  218. {
  219. TestHelpers.InMethod();
  220. scene.RegisterRegionWithGrid();
  221. scene2.RegisterRegionWithGrid();
  222. // Adding child agent to region 1001
  223. string reason;
  224. scene2.NewUserConnection(acd1,0, out reason);
  225. scene2.AddNewClient(testclient, PresenceType.User);
  226. ScenePresence presence = scene.GetScenePresence(agent1);
  227. presence.MakeRootAgent(new Vector3(0,unchecked(Constants.RegionSize-1),0), true);
  228. ScenePresence presence2 = scene2.GetScenePresence(agent1);
  229. // Adding neighbour region caps info to presence2
  230. string cap = presence.ControllingClient.RequestClientInfo().CapsPath;
  231. presence2.AddNeighbourRegion(region1, cap);
  232. Assert.That(presence.IsChildAgent, Is.False, "Did not start root in origin region.");
  233. Assert.That(presence2.IsChildAgent, Is.True, "Is not a child on destination region.");
  234. // Cross to x+1
  235. presence.AbsolutePosition = new Vector3(Constants.RegionSize+1,3,100);
  236. presence.Update();
  237. EventWaitHandle wh = new EventWaitHandle (false, EventResetMode.AutoReset, "Crossing");
  238. // Mimicking communication between client and server, by waiting OK from client
  239. // sent by TestClient.CrossRegion call. Originally, this is network comm.
  240. if (!wh.WaitOne(5000,false))
  241. {
  242. presence.Update();
  243. if (!wh.WaitOne(8000,false))
  244. throw new ArgumentException("1 - Timeout waiting for signal/variable.");
  245. }
  246. // This is a TestClient specific method that fires OnCompleteMovementToRegion event, which
  247. // would normally be fired after receiving the reply packet from comm. done on the last line.
  248. testclient.CompleteMovement();
  249. // Crossings are asynchronous
  250. int timer = 10;
  251. // Make sure cross hasn't already finished
  252. if (!presence.IsInTransit && !presence.IsChildAgent)
  253. {
  254. // If not and not in transit yet, give it some more time
  255. Thread.Sleep(5000);
  256. }
  257. // Enough time, should at least be in transit by now.
  258. while (presence.IsInTransit && timer > 0)
  259. {
  260. Thread.Sleep(1000);
  261. timer-=1;
  262. }
  263. Assert.That(timer,Is.GreaterThan(0),"Timed out waiting to cross 2->1.");
  264. Assert.That(presence.IsChildAgent, Is.True, "Did not complete region cross as expected.");
  265. Assert.That(presence2.IsChildAgent, Is.False, "Did not receive root status after receiving agent.");
  266. // Cross Back
  267. presence2.AbsolutePosition = new Vector3(-10, 3, 100);
  268. presence2.Update();
  269. if (!wh.WaitOne(5000,false))
  270. {
  271. presence2.Update();
  272. if (!wh.WaitOne(8000,false))
  273. throw new ArgumentException("2 - Timeout waiting for signal/variable.");
  274. }
  275. testclient.CompleteMovement();
  276. if (!presence2.IsInTransit && !presence2.IsChildAgent)
  277. {
  278. // If not and not in transit yet, give it some more time
  279. Thread.Sleep(5000);
  280. }
  281. // Enough time, should at least be in transit by now.
  282. while (presence2.IsInTransit && timer > 0)
  283. {
  284. Thread.Sleep(1000);
  285. timer-=1;
  286. }
  287. Assert.That(timer,Is.GreaterThan(0),"Timed out waiting to cross 1->2.");
  288. Assert.That(presence2.IsChildAgent, Is.True, "Did not return from region as expected.");
  289. Assert.That(presence.IsChildAgent, Is.False, "Presence was not made root in old region again.");
  290. }
  291. public void fixNullPresence()
  292. {
  293. string firstName = "testfirstname";
  294. AgentCircuitData agent = new AgentCircuitData();
  295. agent.AgentID = agent1;
  296. agent.firstname = firstName;
  297. agent.lastname = "testlastname";
  298. agent.SessionID = UUID.Zero;
  299. agent.SecureSessionID = UUID.Zero;
  300. agent.circuitcode = 123;
  301. agent.BaseFolder = UUID.Zero;
  302. agent.InventoryFolder = UUID.Zero;
  303. agent.startpos = Vector3.Zero;
  304. agent.CapsPath = GetRandomCapsObjectPath();
  305. agent.Appearance = new AvatarAppearance();
  306. acd1 = agent;
  307. }
  308. public static string GetRandomCapsObjectPath()
  309. {
  310. UUID caps = UUID.Random();
  311. string capsPath = caps.ToString();
  312. capsPath = capsPath.Remove(capsPath.Length - 4, 4);
  313. return capsPath;
  314. }
  315. }
  316. }