ThreadTrackerTests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 NUnit.Framework;
  29. using System.Threading;
  30. using System.Collections.Generic;
  31. namespace OpenSim.Framework.Tests
  32. {
  33. [TestFixture]
  34. public class ThreadTrackerTests
  35. {
  36. private bool running = true;
  37. private bool running2 = true;
  38. [Test]
  39. public void DefaultThreadTrackerTest()
  40. {
  41. List<Thread> lThread = ThreadTracker.GetThreads();
  42. /*
  43. foreach (Thread t in lThread)
  44. {
  45. System.Console.WriteLine(t.Name);
  46. }
  47. */
  48. Assert.That(lThread.Count == 1);
  49. Assert.That(lThread[0].Name == "ThreadTrackerThread");
  50. }
  51. /// <summary>
  52. /// Validate that adding a thread to the thread tracker works
  53. /// Validate that removing a thread from the thread tracker also works.
  54. /// </summary>
  55. [Test]
  56. public void AddThreadToThreadTrackerTestAndRemoveTest()
  57. {
  58. Thread t = new Thread(run);
  59. t.Name = "TestThread";
  60. t.Priority = ThreadPriority.BelowNormal;
  61. t.IsBackground = true;
  62. t.SetApartmentState(ApartmentState.MTA);
  63. t.Start();
  64. ThreadTracker.Add(t);
  65. List<Thread> lThread = ThreadTracker.GetThreads();
  66. Assert.That(lThread.Count == 2);
  67. foreach (Thread tr in lThread)
  68. {
  69. Assert.That((tr.Name == "ThreadTrackerThread" || tr.Name == "TestThread"));
  70. }
  71. running = false;
  72. ThreadTracker.Remove(t);
  73. lThread = ThreadTracker.GetThreads();
  74. Assert.That(lThread.Count == 1);
  75. foreach (Thread tr in lThread)
  76. {
  77. Assert.That((tr.Name == "ThreadTrackerThread"));
  78. }
  79. }
  80. /// <summary>
  81. /// Test a dead thread removal by aborting it and setting it's last seen active date to 50 seconds
  82. /// </summary>
  83. [Test]
  84. public void DeadThreadTest()
  85. {
  86. Thread t = new Thread(run2);
  87. t.Name = "TestThread";
  88. t.Priority = ThreadPriority.BelowNormal;
  89. t.IsBackground = true;
  90. t.SetApartmentState(ApartmentState.MTA);
  91. t.Start();
  92. ThreadTracker.Add(t);
  93. t.Abort();
  94. Thread.Sleep(5000);
  95. ThreadTracker.m_Threads[1].LastSeenActive = DateTime.Now.Ticks - (50*10000000);
  96. ThreadTracker.CleanUp();
  97. List<Thread> lThread = ThreadTracker.GetThreads();
  98. Assert.That(lThread.Count == 1);
  99. foreach (Thread tr in lThread)
  100. {
  101. Assert.That((tr.Name == "ThreadTrackerThread"));
  102. }
  103. }
  104. [Test]
  105. public void UnstartedThreadTest()
  106. {
  107. Thread t = new Thread(run2);
  108. t.Name = "TestThread";
  109. t.Priority = ThreadPriority.BelowNormal;
  110. t.IsBackground = true;
  111. t.SetApartmentState(ApartmentState.MTA);
  112. ThreadTracker.Add(t);
  113. ThreadTracker.m_Threads[1].LastSeenActive = DateTime.Now.Ticks - (50 * 10000000);
  114. ThreadTracker.CleanUp();
  115. List<Thread> lThread = ThreadTracker.GetThreads();
  116. Assert.That(lThread.Count == 1);
  117. foreach (Thread tr in lThread)
  118. {
  119. Assert.That((tr.Name == "ThreadTrackerThread"));
  120. }
  121. }
  122. [Test]
  123. public void NullThreadTest()
  124. {
  125. Thread t = null;
  126. ThreadTracker.Add(t);
  127. List<Thread> lThread = ThreadTracker.GetThreads();
  128. Assert.That(lThread.Count == 1);
  129. foreach (Thread tr in lThread)
  130. {
  131. Assert.That((tr.Name == "ThreadTrackerThread"));
  132. }
  133. }
  134. /// <summary>
  135. /// Worker thread 0
  136. /// </summary>
  137. /// <param name="o"></param>
  138. public void run(object o)
  139. {
  140. while (running)
  141. {
  142. Thread.Sleep(5000);
  143. }
  144. }
  145. /// <summary>
  146. /// Worker thread 1
  147. /// </summary>
  148. /// <param name="o"></param>
  149. public void run2(object o)
  150. {
  151. try
  152. {
  153. while (running2)
  154. {
  155. Thread.Sleep(5000);
  156. }
  157. }
  158. catch (ThreadAbortException)
  159. {
  160. }
  161. }
  162. }
  163. }