CoopTerminationTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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.Threading;
  30. using Nini.Config;
  31. using NUnit.Framework;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.CoreModules.Scripting.WorldComm;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.ScriptEngine.XEngine;
  38. using OpenSim.Tests.Common;
  39. using OpenSim.Tests.Common.Mock;
  40. namespace OpenSim.Region.ScriptEngine.Shared.Instance.Tests
  41. {
  42. /// <summary>
  43. /// Test that co-operative script thread termination is working correctly.
  44. /// </summary>
  45. [TestFixture]
  46. public class CoopTerminationTests : OpenSimTestCase
  47. {
  48. private TestScene m_scene;
  49. private OpenSim.Region.ScriptEngine.XEngine.XEngine m_xEngine;
  50. private AutoResetEvent m_chatEvent;
  51. private AutoResetEvent m_stoppedEvent;
  52. private OSChatMessage m_osChatMessageReceived;
  53. /// <summary>
  54. /// Number of chat messages received so far. Reset before each test.
  55. /// </summary>
  56. private int m_chatMessagesReceived;
  57. /// <summary>
  58. /// Number of chat messages expected. m_chatEvent is not fired until this number is reached or exceeded.
  59. /// </summary>
  60. private int m_chatMessagesThreshold;
  61. [SetUp]
  62. public void Init()
  63. {
  64. m_osChatMessageReceived = null;
  65. m_chatMessagesReceived = 0;
  66. m_chatMessagesThreshold = 0;
  67. m_chatEvent = new AutoResetEvent(false);
  68. m_stoppedEvent = new AutoResetEvent(false);
  69. //AppDomain.CurrentDomain.SetData("APPBASE", Environment.CurrentDirectory + "/bin");
  70. // Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
  71. m_xEngine = new OpenSim.Region.ScriptEngine.XEngine.XEngine();
  72. IniConfigSource configSource = new IniConfigSource();
  73. IConfig startupConfig = configSource.AddConfig("Startup");
  74. startupConfig.Set("DefaultScriptEngine", "XEngine");
  75. IConfig xEngineConfig = configSource.AddConfig("XEngine");
  76. xEngineConfig.Set("Enabled", "true");
  77. xEngineConfig.Set("StartDelay", "0");
  78. // These tests will not run with AppDomainLoading = true, at least on mono. For unknown reasons, the call
  79. // to AssemblyResolver.OnAssemblyResolve fails.
  80. xEngineConfig.Set("AppDomainLoading", "false");
  81. xEngineConfig.Set("ScriptStopStrategy", "co-op");
  82. // Make sure loops aren't actually being terminated by a script delay wait.
  83. xEngineConfig.Set("ScriptDelayFactor", 0);
  84. // This is really just set for debugging the test.
  85. xEngineConfig.Set("WriteScriptSourceToDebugFile", true);
  86. // Set to false if we need to debug test so the old scripts don't get wiped before each separate test
  87. // xEngineConfig.Set("DeleteScriptsOnStartup", false);
  88. // This is not currently used at all for co-op termination. Bumping up to demonstrate that co-op termination
  89. // has an effect - without it tests will fail due to a 120 second wait for the event to finish.
  90. xEngineConfig.Set("WaitForEventCompletionOnScriptStop", 120000);
  91. m_scene = new SceneHelpers().SetupScene("My Test", TestHelpers.ParseTail(0x9999), 1000, 1000, configSource);
  92. SceneHelpers.SetupSceneModules(m_scene, configSource, m_xEngine);
  93. m_scene.StartScripts();
  94. }
  95. /// <summary>
  96. /// Test co-operative termination on derez of an object containing a script with a long-running event.
  97. /// </summary>
  98. /// <remarks>
  99. /// TODO: Actually compiling the script is incidental to this test. Really want a way to compile test scripts
  100. /// within the build itself.
  101. /// </remarks>
  102. [Test]
  103. public void TestStopOnLongSleep()
  104. {
  105. TestHelpers.InMethod();
  106. // TestHelpers.EnableLogging();
  107. string script =
  108. @"default
  109. {
  110. state_entry()
  111. {
  112. llSay(0, ""Thin Lizzy"");
  113. llSleep(60);
  114. }
  115. }";
  116. TestStop(script);
  117. }
  118. [Test]
  119. public void TestNoStopOnSingleStatementForLoop()
  120. {
  121. TestHelpers.InMethod();
  122. // TestHelpers.EnableLogging();
  123. string script =
  124. @"default
  125. {
  126. state_entry()
  127. {
  128. integer i = 0;
  129. for (i = 0; i <= 1; i++) llSay(0, ""Iter "" + (string)i);
  130. }
  131. }";
  132. TestSingleStatementNoStop(script);
  133. }
  134. [Test]
  135. public void TestStopOnLongSingleStatementForLoop()
  136. {
  137. TestHelpers.InMethod();
  138. // TestHelpers.EnableLogging();
  139. string script =
  140. @"default
  141. {
  142. state_entry()
  143. {
  144. integer i = 0;
  145. llSay(0, ""Thin Lizzy"");
  146. for (i = 0; i < 2147483647; i++) llSay(0, ""Iter "" + (string)i);
  147. }
  148. }";
  149. TestStop(script);
  150. }
  151. [Test]
  152. public void TestStopOnLongCompoundStatementForLoop()
  153. {
  154. TestHelpers.InMethod();
  155. // TestHelpers.EnableLogging();
  156. string script =
  157. @"default
  158. {
  159. state_entry()
  160. {
  161. integer i = 0;
  162. llSay(0, ""Thin Lizzy"");
  163. for (i = 0; i < 2147483647; i++)
  164. {
  165. llSay(0, ""Iter "" + (string)i);
  166. }
  167. }
  168. }";
  169. TestStop(script);
  170. }
  171. [Test]
  172. public void TestNoStopOnSingleStatementWhileLoop()
  173. {
  174. TestHelpers.InMethod();
  175. // TestHelpers.EnableLogging();
  176. string script =
  177. @"default
  178. {
  179. state_entry()
  180. {
  181. integer i = 0;
  182. while (i < 2) llSay(0, ""Iter "" + (string)i++);
  183. }
  184. }";
  185. TestSingleStatementNoStop(script);
  186. }
  187. [Test]
  188. public void TestStopOnLongSingleStatementWhileLoop()
  189. {
  190. TestHelpers.InMethod();
  191. // TestHelpers.EnableLogging();
  192. string script =
  193. @"default
  194. {
  195. state_entry()
  196. {
  197. integer i = 0;
  198. llSay(0, ""Thin Lizzy"");
  199. while (1 == 1)
  200. llSay(0, ""Iter "" + (string)i++);
  201. }
  202. }";
  203. TestStop(script);
  204. }
  205. [Test]
  206. public void TestStopOnLongCompoundStatementWhileLoop()
  207. {
  208. TestHelpers.InMethod();
  209. // TestHelpers.EnableLogging();
  210. string script =
  211. @"default
  212. {
  213. state_entry()
  214. {
  215. integer i = 0;
  216. llSay(0, ""Thin Lizzy"");
  217. while (1 == 1)
  218. {
  219. llSay(0, ""Iter "" + (string)i++);
  220. }
  221. }
  222. }";
  223. TestStop(script);
  224. }
  225. [Test]
  226. public void TestNoStopOnSingleStatementDoWhileLoop()
  227. {
  228. TestHelpers.InMethod();
  229. // TestHelpers.EnableLogging();
  230. string script =
  231. @"default
  232. {
  233. state_entry()
  234. {
  235. integer i = 0;
  236. do llSay(0, ""Iter "" + (string)i++);
  237. while (i < 2);
  238. }
  239. }";
  240. TestSingleStatementNoStop(script);
  241. }
  242. [Test]
  243. public void TestStopOnLongSingleStatementDoWhileLoop()
  244. {
  245. TestHelpers.InMethod();
  246. // TestHelpers.EnableLogging();
  247. string script =
  248. @"default
  249. {
  250. state_entry()
  251. {
  252. integer i = 0;
  253. llSay(0, ""Thin Lizzy"");
  254. do llSay(0, ""Iter "" + (string)i++);
  255. while (1 == 1);
  256. }
  257. }";
  258. TestStop(script);
  259. }
  260. [Test]
  261. public void TestStopOnLongCompoundStatementDoWhileLoop()
  262. {
  263. TestHelpers.InMethod();
  264. // TestHelpers.EnableLogging();
  265. string script =
  266. @"default
  267. {
  268. state_entry()
  269. {
  270. integer i = 0;
  271. llSay(0, ""Thin Lizzy"");
  272. do
  273. {
  274. llSay(0, ""Iter "" + (string)i++);
  275. } while (1 == 1);
  276. }
  277. }";
  278. TestStop(script);
  279. }
  280. [Test]
  281. public void TestStopOnInfiniteJumpLoop()
  282. {
  283. TestHelpers.InMethod();
  284. // TestHelpers.EnableLogging();
  285. string script =
  286. @"default
  287. {
  288. state_entry()
  289. {
  290. integer i = 0;
  291. llSay(0, ""Thin Lizzy"");
  292. @p1;
  293. llSay(0, ""Iter "" + (string)i++);
  294. jump p1;
  295. }
  296. }";
  297. TestStop(script);
  298. }
  299. // Disabling for now as these are not particularly useful tests (since they fail due to stack overflow before
  300. // termination can even be tried.
  301. // [Test]
  302. public void TestStopOnInfiniteUserFunctionCallLoop()
  303. {
  304. TestHelpers.InMethod();
  305. // TestHelpers.EnableLogging();
  306. string script =
  307. @"
  308. integer i = 0;
  309. ufn1()
  310. {
  311. llSay(0, ""Iter ufn1() "" + (string)i++);
  312. ufn1();
  313. }
  314. default
  315. {
  316. state_entry()
  317. {
  318. integer i = 0;
  319. llSay(0, ""Thin Lizzy"");
  320. ufn1();
  321. }
  322. }";
  323. TestStop(script);
  324. }
  325. // Disabling for now as these are not particularly useful tests (since they fail due to stack overflow before
  326. // termination can even be tried.
  327. // [Test]
  328. public void TestStopOnInfiniteManualEventCallLoop()
  329. {
  330. TestHelpers.InMethod();
  331. // TestHelpers.EnableLogging();
  332. string script =
  333. @"default
  334. {
  335. state_entry()
  336. {
  337. integer i = 0;
  338. llSay(0, ""Thin Lizzy"");
  339. llSay(0, ""Iter"" + (string)i++);
  340. default_event_state_entry();
  341. }
  342. }";
  343. TestStop(script);
  344. }
  345. private SceneObjectPart CreateScript(string script, string itemName, UUID userId)
  346. {
  347. // UUID objectId = TestHelpers.ParseTail(0x100);
  348. // UUID itemId = TestHelpers.ParseTail(0x3);
  349. SceneObjectGroup so
  350. = SceneHelpers.CreateSceneObject(1, userId, string.Format("Object for {0}", itemName), 0x100);
  351. m_scene.AddNewSceneObject(so, true);
  352. InventoryItemBase itemTemplate = new InventoryItemBase();
  353. // itemTemplate.ID = itemId;
  354. itemTemplate.Name = itemName;
  355. itemTemplate.Folder = so.UUID;
  356. itemTemplate.InvType = (int)InventoryType.LSL;
  357. m_scene.EventManager.OnChatFromWorld += OnChatFromWorld;
  358. return m_scene.RezNewScript(userId, itemTemplate, script);
  359. }
  360. private void TestSingleStatementNoStop(string script)
  361. {
  362. // In these tests we expect to see at least 2 chat messages to confirm that the loop is working properly.
  363. m_chatMessagesThreshold = 2;
  364. UUID userId = TestHelpers.ParseTail(0x1);
  365. // UUID objectId = TestHelpers.ParseTail(0x100);
  366. // UUID itemId = TestHelpers.ParseTail(0x3);
  367. string itemName = "TestNoStop";
  368. SceneObjectPart partWhereRezzed = CreateScript(script, itemName, userId);
  369. // Wait for the script to start the event before we try stopping it.
  370. m_chatEvent.WaitOne(60000);
  371. if (m_osChatMessageReceived == null)
  372. Assert.Fail("Script did not start");
  373. else
  374. Assert.That(m_chatMessagesReceived, Is.EqualTo(2));
  375. bool running;
  376. TaskInventoryItem scriptItem = partWhereRezzed.Inventory.GetInventoryItem(itemName);
  377. Assert.That(
  378. SceneObjectPartInventory.TryGetScriptInstanceRunning(m_scene, scriptItem, out running), Is.True);
  379. Assert.That(running, Is.True);
  380. }
  381. private void TestStop(string script)
  382. {
  383. // In these tests we're only interested in the first message to confirm that the script has started.
  384. m_chatMessagesThreshold = 1;
  385. UUID userId = TestHelpers.ParseTail(0x1);
  386. // UUID objectId = TestHelpers.ParseTail(0x100);
  387. // UUID itemId = TestHelpers.ParseTail(0x3);
  388. string itemName = "TestStop";
  389. SceneObjectPart partWhereRezzed = CreateScript(script, itemName, userId);
  390. TaskInventoryItem rezzedItem = partWhereRezzed.Inventory.GetInventoryItem(itemName);
  391. // Wait for the script to start the event before we try stopping it.
  392. m_chatEvent.WaitOne(60000);
  393. if (m_osChatMessageReceived != null)
  394. Console.WriteLine("Script started with message [{0}]", m_osChatMessageReceived.Message);
  395. else
  396. Assert.Fail("Script did not start");
  397. // FIXME: This is a very poor way of trying to avoid a low-probability race condition where the script
  398. // executes llSay() but has not started the next statement before we try to stop it.
  399. Thread.Sleep(1000);
  400. // We need a way of carrying on if StopScript() fail, since it won't return if the script isn't actually
  401. // stopped. This kind of multi-threading is far from ideal in a regression test.
  402. new Thread(() => { m_xEngine.StopScript(rezzedItem.ItemID); m_stoppedEvent.Set(); }).Start();
  403. if (!m_stoppedEvent.WaitOne(30000))
  404. Assert.Fail("Script did not co-operatively stop.");
  405. bool running;
  406. TaskInventoryItem scriptItem = partWhereRezzed.Inventory.GetInventoryItem(itemName);
  407. Assert.That(
  408. SceneObjectPartInventory.TryGetScriptInstanceRunning(m_scene, scriptItem, out running), Is.True);
  409. Assert.That(running, Is.False);
  410. }
  411. private void OnChatFromWorld(object sender, OSChatMessage oscm)
  412. {
  413. Console.WriteLine("Got chat [{0}]", oscm.Message);
  414. m_osChatMessageReceived = oscm;
  415. if (++m_chatMessagesReceived >= m_chatMessagesThreshold)
  416. {
  417. m_scene.EventManager.OnChatFromWorld -= OnChatFromWorld;
  418. m_chatEvent.Set();
  419. }
  420. }
  421. }
  422. }