EventQueueTests.cs 7.2 KB

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