CoopTerminationTests.cs 15 KB

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