aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim
diff options
context:
space:
mode:
authorTeravus Ovares (Dan Olivares)2009-08-13 22:12:37 -0400
committerTeravus Ovares (Dan Olivares)2009-08-13 22:12:37 -0400
commit0781ac9a5e307e6ed0a0685c1dad98d5fb51b76b (patch)
tree8e1a3d42e2dfe0f9c7bd4f55b6983daae5c4dc59 /OpenSim
parent* Add AgentCircuitManager tests for adding, removing, changing circuit code, ... (diff)
downloadopensim-SC-0781ac9a5e307e6ed0a0685c1dad98d5fb51b76b.zip
opensim-SC-0781ac9a5e307e6ed0a0685c1dad98d5fb51b76b.tar.gz
opensim-SC-0781ac9a5e307e6ed0a0685c1dad98d5fb51b76b.tar.bz2
opensim-SC-0781ac9a5e307e6ed0a0685c1dad98d5fb51b76b.tar.xz
* Add ThreadTracker Tests, Tests default thread, Adding Testing and Removing a thread, a dead thread, and a null Thread
* Fix a null thread situation
Diffstat (limited to 'OpenSim')
-rw-r--r--OpenSim/Framework/Tests/ThreadTrackerTests.cs192
-rw-r--r--OpenSim/Framework/ThreadTracker.cs36
2 files changed, 216 insertions, 12 deletions
diff --git a/OpenSim/Framework/Tests/ThreadTrackerTests.cs b/OpenSim/Framework/Tests/ThreadTrackerTests.cs
new file mode 100644
index 0000000..37c75ef
--- /dev/null
+++ b/OpenSim/Framework/Tests/ThreadTrackerTests.cs
@@ -0,0 +1,192 @@
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
28using System;
29using NUnit.Framework;
30using System.Threading;
31using System.Collections.Generic;
32
33namespace OpenSim.Framework.Tests
34{
35 [TestFixture]
36 public class ThreadTrackerTests
37 {
38 private bool running = true;
39 private bool running2 = true;
40
41 [Test]
42 public void DefaultThreadTrackerTest()
43 {
44 List<Thread> lThread = ThreadTracker.GetThreads();
45
46 /*
47 foreach (Thread t in lThread)
48 {
49 System.Console.WriteLine(t.Name);
50 }
51 */
52
53 Assert.That(lThread.Count == 1);
54 Assert.That(lThread[0].Name == "ThreadTrackerThread");
55 }
56
57 /// <summary>
58 /// Validate that adding a thread to the thread tracker works
59 /// Validate that removing a thread from the thread tracker also works.
60 /// </summary>
61 [Test]
62 public void AddThreadToThreadTrackerTestAndRemoveTest()
63 {
64 Thread t = new Thread(run);
65 t.Name = "TestThread";
66 t.Priority = ThreadPriority.BelowNormal;
67 t.IsBackground = true;
68 t.SetApartmentState(ApartmentState.MTA);
69 t.Start();
70 ThreadTracker.Add(t);
71
72 List<Thread> lThread = ThreadTracker.GetThreads();
73
74 Assert.That(lThread.Count == 2);
75
76 foreach (Thread tr in lThread)
77 {
78 Assert.That((tr.Name == "ThreadTrackerThread" || tr.Name == "TestThread"));
79 }
80 running = false;
81 ThreadTracker.Remove(t);
82
83 lThread = ThreadTracker.GetThreads();
84
85 Assert.That(lThread.Count == 1);
86
87 foreach (Thread tr in lThread)
88 {
89 Assert.That((tr.Name == "ThreadTrackerThread"));
90 }
91
92
93 }
94
95 /// <summary>
96 /// Test a dead thread removal by aborting it and setting it's last seen active date to 50 seconds
97 /// </summary>
98 [Test]
99 public void DeadThreadTest()
100 {
101 Thread t = new Thread(run2);
102 t.Name = "TestThread";
103 t.Priority = ThreadPriority.BelowNormal;
104 t.IsBackground = true;
105 t.SetApartmentState(ApartmentState.MTA);
106 t.Start();
107 ThreadTracker.Add(t);
108 t.Abort();
109 Thread.Sleep(5000);
110 ThreadTracker.m_Threads[1].LastSeenActive = DateTime.Now.Ticks - (50*10000000);
111 ThreadTracker.CleanUp();
112 List<Thread> lThread = ThreadTracker.GetThreads();
113
114 Assert.That(lThread.Count == 1);
115
116 foreach (Thread tr in lThread)
117 {
118 Assert.That((tr.Name == "ThreadTrackerThread"));
119 }
120 }
121
122 [Test]
123 public void UnstartedThreadTest()
124 {
125 Thread t = new Thread(run2);
126 t.Name = "TestThread";
127 t.Priority = ThreadPriority.BelowNormal;
128 t.IsBackground = true;
129 t.SetApartmentState(ApartmentState.MTA);
130 ThreadTracker.Add(t);
131 ThreadTracker.m_Threads[1].LastSeenActive = DateTime.Now.Ticks - (50 * 10000000);
132 ThreadTracker.CleanUp();
133 List<Thread> lThread = ThreadTracker.GetThreads();
134
135 Assert.That(lThread.Count == 1);
136
137 foreach (Thread tr in lThread)
138 {
139 Assert.That((tr.Name == "ThreadTrackerThread"));
140 }
141 }
142
143 [Test]
144 public void NullThreadTest()
145 {
146 Thread t = null;
147 ThreadTracker.Add(t);
148
149 List<Thread> lThread = ThreadTracker.GetThreads();
150
151 Assert.That(lThread.Count == 1);
152
153 foreach (Thread tr in lThread)
154 {
155 Assert.That((tr.Name == "ThreadTrackerThread"));
156 }
157 }
158
159
160 /// <summary>
161 /// Worker thread 0
162 /// </summary>
163 /// <param name="o"></param>
164 public void run( object o)
165 {
166 while (running)
167 {
168 Thread.Sleep(5000);
169 }
170 }
171
172 /// <summary>
173 /// Worker thread 1
174 /// </summary>
175 /// <param name="o"></param>
176 public void run2(object o)
177 {
178 try
179 {
180 while (running2)
181 {
182 Thread.Sleep(5000);
183 }
184
185 }
186 catch (ThreadAbortException)
187 {
188 }
189 }
190
191 }
192}
diff --git a/OpenSim/Framework/ThreadTracker.cs b/OpenSim/Framework/ThreadTracker.cs
index d8bd2c0..fa6f0b8 100644
--- a/OpenSim/Framework/ThreadTracker.cs
+++ b/OpenSim/Framework/ThreadTracker.cs
@@ -77,12 +77,15 @@ namespace OpenSim.Framework
77 public static void Add(Thread thread) 77 public static void Add(Thread thread)
78 { 78 {
79#if DEBUG 79#if DEBUG
80 lock (m_Threads) 80 if (thread != null)
81 { 81 {
82 ThreadTrackerItem tti = new ThreadTrackerItem(); 82 lock (m_Threads)
83 tti.Thread = thread; 83 {
84 tti.LastSeenActive = DateTime.Now.Ticks; 84 ThreadTrackerItem tti = new ThreadTrackerItem();
85 m_Threads.Add(tti); 85 tti.Thread = thread;
86 tti.LastSeenActive = DateTime.Now.Ticks;
87 m_Threads.Add(tti);
88 }
86 } 89 }
87#endif 90#endif
88 } 91 }
@@ -107,16 +110,25 @@ namespace OpenSim.Framework
107 { 110 {
108 foreach (ThreadTrackerItem tti in new ArrayList(m_Threads)) 111 foreach (ThreadTrackerItem tti in new ArrayList(m_Threads))
109 { 112 {
110 if (tti.Thread.IsAlive) 113 try
111 { 114 {
112 // Its active 115
113 tti.LastSeenActive = DateTime.Now.Ticks; 116
117 if (tti.Thread.IsAlive)
118 {
119 // Its active
120 tti.LastSeenActive = DateTime.Now.Ticks;
121 }
122 else
123 {
124 // Its not active -- if its expired then remove it
125 if (tti.LastSeenActive + ThreadTimeout < DateTime.Now.Ticks)
126 m_Threads.Remove(tti);
127 }
114 } 128 }
115 else 129 catch (NullReferenceException)
116 { 130 {
117 // Its not active -- if its expired then remove it 131 m_Threads.Remove(tti);
118 if (tti.LastSeenActive + ThreadTimeout < DateTime.Now.Ticks)
119 m_Threads.Remove(tti);
120 } 132 }
121 } 133 }
122 } 134 }