EventQueueTests.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using log4net.Config;
  32. using Nini.Config;
  33. using NUnit.Framework;
  34. using OpenMetaverse;
  35. using OpenMetaverse.Packets;
  36. using OpenMetaverse.StructuredData;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Framework.Servers.HttpServer;
  40. using OpenSim.Region.ClientStack.Linden;
  41. using OpenSim.Region.CoreModules.Framework;
  42. using OpenSim.Region.Framework.Scenes;
  43. using OpenSim.Region.OptionalModules.World.NPC;
  44. using OpenSim.Tests.Common;
  45. namespace OpenSim.Region.ClientStack.Linden.Tests
  46. {
  47. [TestFixture]
  48. public class EventQueueTests : OpenSimTestCase
  49. {
  50. private TestScene m_scene;
  51. private EventQueueGetModule m_eqgMod;
  52. private NPCModule m_npcMod;
  53. [SetUp]
  54. public override void SetUp()
  55. {
  56. base.SetUp();
  57. uint port = 9999;
  58. uint sslPort = 9998;
  59. // This is an unfortunate bit of clean up we have to do because MainServer manages things through static
  60. // variables and the VM is not restarted between tests.
  61. MainServer.RemoveHttpServer(port);
  62. BaseHttpServer server = new BaseHttpServer(port, false, sslPort, "");
  63. MainServer.AddHttpServer(server);
  64. MainServer.Instance = server;
  65. IConfigSource config = new IniConfigSource();
  66. config.AddConfig("Startup");
  67. CapabilitiesModule capsModule = new CapabilitiesModule();
  68. m_eqgMod = new EventQueueGetModule();
  69. // For NPC test support
  70. config.AddConfig("NPC");
  71. config.Configs["NPC"].Set("Enabled", "true");
  72. m_npcMod = new NPCModule();
  73. m_scene = new SceneHelpers().SetupScene();
  74. SceneHelpers.SetupSceneModules(m_scene, config, capsModule, m_eqgMod, m_npcMod);
  75. }
  76. [Test]
  77. public void TestAddForClient()
  78. {
  79. TestHelpers.InMethod();
  80. // log4net.Config.XmlConfigurator.Configure();
  81. SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  82. // TODO: Add more assertions for the other aspects of event queues
  83. Assert.That(MainServer.Instance.GetPollServiceHandlerKeys().Count, Is.EqualTo(1));
  84. }
  85. [Test]
  86. public void TestRemoveForClient()
  87. {
  88. TestHelpers.InMethod();
  89. // TestHelpers.EnableLogging();
  90. UUID spId = TestHelpers.ParseTail(0x1);
  91. SceneHelpers.AddScenePresence(m_scene, spId);
  92. m_scene.CloseAgent(spId, false);
  93. // TODO: Add more assertions for the other aspects of event queues
  94. Assert.That(MainServer.Instance.GetPollServiceHandlerKeys().Count, Is.EqualTo(0));
  95. }
  96. [Test]
  97. public void TestEnqueueMessage()
  98. {
  99. TestHelpers.InMethod();
  100. // log4net.Config.XmlConfigurator.Configure();
  101. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  102. string messageName = "TestMessage";
  103. m_eqgMod.Enqueue(m_eqgMod.BuildEvent(messageName, new OSDMap()), sp.UUID);
  104. Hashtable eventsResponse = m_eqgMod.GetEvents(UUID.Zero, sp.UUID);
  105. // initial queue as null events
  106. eventsResponse = m_eqgMod.GetEvents(UUID.Zero, sp.UUID);
  107. if((int)eventsResponse["int_response_code"] != (int)HttpStatusCode.OK)
  108. {
  109. eventsResponse = m_eqgMod.GetEvents(UUID.Zero, sp.UUID);
  110. if((int)eventsResponse["int_response_code"] != (int)HttpStatusCode.OK)
  111. eventsResponse = m_eqgMod.GetEvents(UUID.Zero, sp.UUID);
  112. }
  113. Assert.That((int)eventsResponse["int_response_code"], Is.EqualTo((int)HttpStatusCode.OK));
  114. // Console.WriteLine("Response [{0}]", (string)eventsResponse["str_response_string"]);
  115. OSDMap rawOsd = (OSDMap)OSDParser.DeserializeLLSDXml((string)eventsResponse["str_response_string"]);
  116. OSDArray eventsOsd = (OSDArray)rawOsd["events"];
  117. bool foundUpdate = false;
  118. foreach (OSD osd in eventsOsd)
  119. {
  120. OSDMap eventOsd = (OSDMap)osd;
  121. if (eventOsd["message"] == messageName)
  122. foundUpdate = true;
  123. }
  124. Assert.That(foundUpdate, Is.True, string.Format("Did not find {0} in response", messageName));
  125. }
  126. /// <summary>
  127. /// Test an attempt to put a message on the queue of a user that is not in the region.
  128. /// </summary>
  129. [Test]
  130. public void TestEnqueueMessageNoUser()
  131. {
  132. TestHelpers.InMethod();
  133. TestHelpers.EnableLogging();
  134. string messageName = "TestMessage";
  135. m_eqgMod.Enqueue(m_eqgMod.BuildEvent(messageName, new OSDMap()), TestHelpers.ParseTail(0x1));
  136. Hashtable eventsResponse = m_eqgMod.GetEvents(UUID.Zero, TestHelpers.ParseTail(0x1));
  137. Assert.That((int)eventsResponse["int_response_code"], Is.EqualTo((int)HttpStatusCode.BadGateway));
  138. }
  139. /// <summary>
  140. /// NPCs do not currently have an event queue but a caller may try to send a message anyway, so check behaviour.
  141. /// </summary>
  142. [Test]
  143. public void TestEnqueueMessageToNpc()
  144. {
  145. TestHelpers.InMethod();
  146. // TestHelpers.EnableLogging();
  147. UUID npcId
  148. = m_npcMod.CreateNPC(
  149. "John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, new AvatarAppearance());
  150. ScenePresence npc = m_scene.GetScenePresence(npcId);
  151. string messageName = "TestMessage";
  152. m_eqgMod.Enqueue(m_eqgMod.BuildEvent(messageName, new OSDMap()), npc.UUID);
  153. Hashtable eventsResponse = m_eqgMod.GetEvents(UUID.Zero, npc.UUID);
  154. Assert.That((int)eventsResponse["int_response_code"], Is.EqualTo((int)HttpStatusCode.BadGateway));
  155. }
  156. }
  157. }