ScenePresenceTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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.World.Serialiser;
  43. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Simulation;
  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 AgentCircuitData acd1;
  60. public SceneObjectGroup sog1, sog2, sog3;
  61. public TestClient testclient;
  62. [TestFixtureSetUp]
  63. public void Init()
  64. {
  65. scene = SceneSetupHelpers.SetupScene("Neighbour x", UUID.Random(), 1000, 1000);
  66. scene2 = SceneSetupHelpers.SetupScene("Neighbour x+1", UUID.Random(), 1001, 1000);
  67. scene3 = SceneSetupHelpers.SetupScene("Neighbour x-1", UUID.Random(), 999, 1000);
  68. ISharedRegionModule interregionComms = new LocalSimulationConnectorModule();
  69. interregionComms.Initialise(new IniConfigSource());
  70. interregionComms.PostInitialise();
  71. SceneSetupHelpers.SetupSceneModules(scene, new IniConfigSource(), interregionComms);
  72. SceneSetupHelpers.SetupSceneModules(scene2, new IniConfigSource(), interregionComms);
  73. SceneSetupHelpers.SetupSceneModules(scene3, new IniConfigSource(), interregionComms);
  74. agent1 = UUID.Random();
  75. agent2 = UUID.Random();
  76. agent3 = UUID.Random();
  77. random = new Random();
  78. sog1 = NewSOG(UUID.Random(), scene, agent1);
  79. sog2 = NewSOG(UUID.Random(), scene, agent1);
  80. sog3 = NewSOG(UUID.Random(), scene, agent1);
  81. //ulong neighbourHandle = Utils.UIntsToLong((uint)(neighbourx * Constants.RegionSize), (uint)(neighboury * Constants.RegionSize));
  82. region1 = scene.RegionInfo.RegionHandle;
  83. region2 = scene2.RegionInfo.RegionHandle;
  84. region3 = scene3.RegionInfo.RegionHandle;
  85. }
  86. /// <summary>
  87. /// Test adding a root agent to a scene. Doesn't yet actually complete crossing the agent into the scene.
  88. /// </summary>
  89. [Test]
  90. public void T010_TestAddRootAgent()
  91. {
  92. TestHelper.InMethod();
  93. string firstName = "testfirstname";
  94. AgentCircuitData agent = new AgentCircuitData();
  95. agent.AgentID = agent1;
  96. agent.firstname = firstName;
  97. agent.lastname = "testlastname";
  98. agent.SessionID = UUID.Zero;
  99. agent.SecureSessionID = UUID.Zero;
  100. agent.circuitcode = 123;
  101. agent.BaseFolder = UUID.Zero;
  102. agent.InventoryFolder = UUID.Zero;
  103. agent.startpos = Vector3.Zero;
  104. agent.CapsPath = GetRandomCapsObjectPath();
  105. agent.ChildrenCapSeeds = new Dictionary<ulong, string>();
  106. agent.child = true;
  107. string reason;
  108. scene.NewUserConnection(agent, (uint)TeleportFlags.ViaLogin, 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. if (acd1 == null)
  133. fixNullPresence();
  134. scene.NewUserConnection(acd1, 0, out reason);
  135. if (testclient == null)
  136. testclient = new TestClient(acd1, scene);
  137. scene.AddNewClient(testclient);
  138. ScenePresence presence = scene.GetScenePresence(agent1);
  139. presence.MakeRootAgent(new Vector3(90,90,90),false);
  140. string cap = presence.ControllingClient.RequestClientInfo().CapsPath;
  141. presence.AddNeighbourRegion(region2, cap);
  142. presence.AddNeighbourRegion(region3, cap);
  143. List<ulong> neighbours = presence.GetKnownRegionList();
  144. Assert.That(neighbours.Count, Is.EqualTo(2));
  145. }
  146. public void fixNullPresence()
  147. {
  148. string firstName = "testfirstname";
  149. AgentCircuitData agent = new AgentCircuitData();
  150. agent.AgentID = agent1;
  151. agent.firstname = firstName;
  152. agent.lastname = "testlastname";
  153. agent.SessionID = UUID.Zero;
  154. agent.SecureSessionID = UUID.Zero;
  155. agent.circuitcode = 123;
  156. agent.BaseFolder = UUID.Zero;
  157. agent.InventoryFolder = UUID.Zero;
  158. agent.startpos = Vector3.Zero;
  159. agent.CapsPath = GetRandomCapsObjectPath();
  160. acd1 = agent;
  161. }
  162. [Test]
  163. public void T013_TestRemoveNeighbourRegion()
  164. {
  165. TestHelper.InMethod();
  166. ScenePresence presence = scene.GetScenePresence(agent1);
  167. presence.RemoveNeighbourRegion(region3);
  168. List<ulong> neighbours = presence.GetKnownRegionList();
  169. Assert.That(neighbours.Count,Is.EqualTo(1));
  170. /*
  171. presence.MakeChildAgent;
  172. presence.MakeRootAgent;
  173. CompleteAvatarMovement
  174. */
  175. }
  176. // I'm commenting this test, because this is not supposed to happen here
  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,unchecked(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. // I'm commenting this test because it does not represent
  192. // crossings. The Thread.Sleep's in here are not meaningful mocks,
  193. // and they sometimes fail in panda.
  194. // We need to talk in order to develop a test
  195. // that really tests region crossings. There are 3 async components,
  196. // but things are synchronous among them. So there should be
  197. // 3 threads in here.
  198. //[Test]
  199. public void T021_TestCrossToNewRegion()
  200. {
  201. TestHelper.InMethod();
  202. scene.RegisterRegionWithGrid();
  203. scene2.RegisterRegionWithGrid();
  204. // Adding child agent to region 1001
  205. string reason;
  206. scene2.NewUserConnection(acd1,0, out reason);
  207. scene2.AddNewClient(testclient);
  208. ScenePresence presence = scene.GetScenePresence(agent1);
  209. presence.MakeRootAgent(new Vector3(0,unchecked(Constants.RegionSize-1),0), true);
  210. ScenePresence presence2 = scene2.GetScenePresence(agent1);
  211. // Adding neighbour region caps info to presence2
  212. string cap = presence.ControllingClient.RequestClientInfo().CapsPath;
  213. presence2.AddNeighbourRegion(region1, cap);
  214. Assert.That(presence.IsChildAgent, Is.False, "Did not start root in origin region.");
  215. Assert.That(presence2.IsChildAgent, Is.True, "Is not a child on destination region.");
  216. // Cross to x+1
  217. presence.AbsolutePosition = new Vector3(Constants.RegionSize+1,3,100);
  218. presence.Update();
  219. EventWaitHandle wh = new EventWaitHandle (false, EventResetMode.AutoReset, "Crossing");
  220. // Mimicking communication between client and server, by waiting OK from client
  221. // sent by TestClient.CrossRegion call. Originally, this is network comm.
  222. if (!wh.WaitOne(5000,false))
  223. {
  224. presence.Update();
  225. if (!wh.WaitOne(8000,false))
  226. throw new ArgumentException("1 - Timeout waiting for signal/variable.");
  227. }
  228. // This is a TestClient specific method that fires OnCompleteMovementToRegion event, which
  229. // would normally be fired after receiving the reply packet from comm. done on the last line.
  230. testclient.CompleteMovement();
  231. // Crossings are asynchronous
  232. int timer = 10;
  233. // Make sure cross hasn't already finished
  234. if (!presence.IsInTransit && !presence.IsChildAgent)
  235. {
  236. // If not and not in transit yet, give it some more time
  237. Thread.Sleep(5000);
  238. }
  239. // Enough time, should at least be in transit by now.
  240. while (presence.IsInTransit && timer > 0)
  241. {
  242. Thread.Sleep(1000);
  243. timer-=1;
  244. }
  245. Assert.That(timer,Is.GreaterThan(0),"Timed out waiting to cross 2->1.");
  246. Assert.That(presence.IsChildAgent, Is.True, "Did not complete region cross as expected.");
  247. Assert.That(presence2.IsChildAgent, Is.False, "Did not receive root status after receiving agent.");
  248. // Cross Back
  249. presence2.AbsolutePosition = new Vector3(-10, 3, 100);
  250. presence2.Update();
  251. if (!wh.WaitOne(5000,false))
  252. {
  253. presence2.Update();
  254. if (!wh.WaitOne(8000,false))
  255. throw new ArgumentException("2 - Timeout waiting for signal/variable.");
  256. }
  257. testclient.CompleteMovement();
  258. if (!presence2.IsInTransit && !presence2.IsChildAgent)
  259. {
  260. // If not and not in transit yet, give it some more time
  261. Thread.Sleep(5000);
  262. }
  263. // Enough time, should at least be in transit by now.
  264. while (presence2.IsInTransit && timer > 0)
  265. {
  266. Thread.Sleep(1000);
  267. timer-=1;
  268. }
  269. Assert.That(timer,Is.GreaterThan(0),"Timed out waiting to cross 1->2.");
  270. Assert.That(presence2.IsChildAgent, Is.True, "Did not return from region as expected.");
  271. Assert.That(presence.IsChildAgent, Is.False, "Presence was not made root in old region again.");
  272. }
  273. [Test]
  274. public void T030_TestAddAttachments()
  275. {
  276. TestHelper.InMethod();
  277. ScenePresence presence = scene.GetScenePresence(agent1);
  278. presence.AddAttachment(sog1);
  279. presence.AddAttachment(sog2);
  280. presence.AddAttachment(sog3);
  281. Assert.That(presence.HasAttachments(), Is.True);
  282. Assert.That(presence.ValidateAttachments(), Is.True);
  283. }
  284. [Test]
  285. public void T031_RemoveAttachments()
  286. {
  287. TestHelper.InMethod();
  288. ScenePresence presence = scene.GetScenePresence(agent1);
  289. presence.RemoveAttachment(sog1);
  290. presence.RemoveAttachment(sog2);
  291. presence.RemoveAttachment(sog3);
  292. Assert.That(presence.HasAttachments(), Is.False);
  293. }
  294. // I'm commenting this test because scene setup NEEDS InventoryService to
  295. // be non-null
  296. //[Test]
  297. public void T032_CrossAttachments()
  298. {
  299. TestHelper.InMethod();
  300. ScenePresence presence = scene.GetScenePresence(agent1);
  301. ScenePresence presence2 = scene2.GetScenePresence(agent1);
  302. presence2.AddAttachment(sog1);
  303. presence2.AddAttachment(sog2);
  304. ISharedRegionModule serialiser = new SerialiserModule();
  305. SceneSetupHelpers.SetupSceneModules(scene, new IniConfigSource(), serialiser);
  306. SceneSetupHelpers.SetupSceneModules(scene2, new IniConfigSource(), serialiser);
  307. Assert.That(presence.HasAttachments(), Is.False, "Presence has attachments before cross");
  308. //Assert.That(presence2.CrossAttachmentsIntoNewRegion(region1, true), Is.True, "Cross was not successful");
  309. Assert.That(presence2.HasAttachments(), Is.False, "Presence2 objects were not deleted");
  310. Assert.That(presence.HasAttachments(), Is.True, "Presence has not received new objects");
  311. }
  312. [TearDown]
  313. public void TearDown()
  314. {
  315. if (MainServer.Instance != null) MainServer.Instance.Stop();
  316. }
  317. public static string GetRandomCapsObjectPath()
  318. {
  319. TestHelper.InMethod();
  320. UUID caps = UUID.Random();
  321. string capsPath = caps.ToString();
  322. capsPath = capsPath.Remove(capsPath.Length - 4, 4);
  323. return capsPath;
  324. }
  325. private SceneObjectGroup NewSOG(UUID uuid, Scene scene, UUID agent)
  326. {
  327. SceneObjectPart sop = new SceneObjectPart();
  328. sop.Name = RandomName();
  329. sop.Description = RandomName();
  330. sop.Text = RandomName();
  331. sop.SitName = RandomName();
  332. sop.TouchName = RandomName();
  333. sop.UUID = uuid;
  334. sop.Shape = PrimitiveBaseShape.Default;
  335. sop.Shape.State = 1;
  336. sop.OwnerID = agent;
  337. SceneObjectGroup sog = new SceneObjectGroup(sop);
  338. sog.SetScene(scene);
  339. return sog;
  340. }
  341. private static string RandomName()
  342. {
  343. StringBuilder name = new StringBuilder();
  344. int size = random.Next(5,12);
  345. char ch ;
  346. for (int i=0; i<size; i++)
  347. {
  348. ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))) ;
  349. name.Append(ch);
  350. }
  351. return name.ToString();
  352. }
  353. }
  354. }