EventQueueTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. config.Configs["Startup"].Set("EventQueue", "true");
  68. CapabilitiesModule capsModule = new CapabilitiesModule();
  69. m_eqgMod = new EventQueueGetModule();
  70. // For NPC test support
  71. config.AddConfig("NPC");
  72. config.Configs["NPC"].Set("Enabled", "true");
  73. m_npcMod = new NPCModule();
  74. m_scene = new SceneHelpers().SetupScene();
  75. SceneHelpers.SetupSceneModules(m_scene, config, capsModule, m_eqgMod, m_npcMod);
  76. }
  77. [Test]
  78. public void TestAddForClient()
  79. {
  80. TestHelpers.InMethod();
  81. // log4net.Config.XmlConfigurator.Configure();
  82. SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  83. // TODO: Add more assertions for the other aspects of event queues
  84. Assert.That(MainServer.Instance.GetPollServiceHandlerKeys().Count, Is.EqualTo(1));
  85. }
  86. [Test]
  87. public void TestRemoveForClient()
  88. {
  89. TestHelpers.InMethod();
  90. // TestHelpers.EnableLogging();
  91. UUID spId = TestHelpers.ParseTail(0x1);
  92. SceneHelpers.AddScenePresence(m_scene, spId);
  93. m_scene.CloseAgent(spId, false);
  94. // TODO: Add more assertions for the other aspects of event queues
  95. Assert.That(MainServer.Instance.GetPollServiceHandlerKeys().Count, Is.EqualTo(0));
  96. }
  97. [Test]
  98. public void TestEnqueueMessage()
  99. {
  100. TestHelpers.InMethod();
  101. // log4net.Config.XmlConfigurator.Configure();
  102. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  103. string messageName = "TestMessage";
  104. m_eqgMod.Enqueue(m_eqgMod.BuildEvent(messageName, new OSDMap()), sp.UUID);
  105. Hashtable eventsResponse = m_eqgMod.GetEvents(UUID.Zero, sp.UUID);
  106. Assert.That((int)eventsResponse["int_response_code"], Is.EqualTo((int)HttpStatusCode.OK));
  107. // Console.WriteLine("Response [{0}]", (string)eventsResponse["str_response_string"]);
  108. OSDMap rawOsd = (OSDMap)OSDParser.DeserializeLLSDXml((string)eventsResponse["str_response_string"]);
  109. OSDArray eventsOsd = (OSDArray)rawOsd["events"];
  110. bool foundUpdate = false;
  111. foreach (OSD osd in eventsOsd)
  112. {
  113. OSDMap eventOsd = (OSDMap)osd;
  114. if (eventOsd["message"] == messageName)
  115. foundUpdate = true;
  116. }
  117. Assert.That(foundUpdate, Is.True, string.Format("Did not find {0} in response", messageName));
  118. }
  119. /// <summary>
  120. /// Test an attempt to put a message on the queue of a user that is not in the region.
  121. /// </summary>
  122. [Test]
  123. public void TestEnqueueMessageNoUser()
  124. {
  125. TestHelpers.InMethod();
  126. TestHelpers.EnableLogging();
  127. string messageName = "TestMessage";
  128. m_eqgMod.Enqueue(m_eqgMod.BuildEvent(messageName, new OSDMap()), TestHelpers.ParseTail(0x1));
  129. Hashtable eventsResponse = m_eqgMod.GetEvents(UUID.Zero, TestHelpers.ParseTail(0x1));
  130. Assert.That((int)eventsResponse["int_response_code"], Is.EqualTo((int)HttpStatusCode.BadGateway));
  131. }
  132. /// <summary>
  133. /// NPCs do not currently have an event queue but a caller may try to send a message anyway, so check behaviour.
  134. /// </summary>
  135. [Test]
  136. public void TestEnqueueMessageToNpc()
  137. {
  138. TestHelpers.InMethod();
  139. // TestHelpers.EnableLogging();
  140. UUID npcId
  141. = m_npcMod.CreateNPC(
  142. "John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, new AvatarAppearance());
  143. ScenePresence npc = m_scene.GetScenePresence(npcId);
  144. string messageName = "TestMessage";
  145. m_eqgMod.Enqueue(m_eqgMod.BuildEvent(messageName, new OSDMap()), npc.UUID);
  146. Hashtable eventsResponse = m_eqgMod.GetEvents(UUID.Zero, npc.UUID);
  147. Assert.That((int)eventsResponse["int_response_code"], Is.EqualTo((int)HttpStatusCode.BadGateway));
  148. }
  149. }
  150. }