Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Tools / NVelocity / src / NVelocity.Tests / MultiThreadTestCase.cs
blobf1b4533267666b82a4ee693321ce50d510961fc6
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace NVelocity
17 using System;
18 using System.Collections;
19 using System.IO;
20 using System.Text.RegularExpressions;
21 using System.Threading;
22 using App;
23 using NUnit.Framework;
24 using Test;
26 [TestFixture, Explicit]
27 public class MultiThreadTestCase
29 private ManualResetEvent startEvent = new ManualResetEvent(false);
30 private ManualResetEvent stopEvent = new ManualResetEvent(false);
31 private ArrayList items;
32 private VelocityEngine ve;
34 [SetUp]
35 public void Setup()
37 items = new ArrayList();
39 items.Add("a");
40 items.Add("b");
41 items.Add("c");
42 items.Add("d");
44 ve = new VelocityEngine();
45 ve.Init();
48 [Test]
49 public void Multithreaded_ASTExecuteDoesNotShareParams()
51 const int threadCount = 1;
53 Thread[] threads = new Thread[threadCount];
55 for (int i = 0; i < threadCount; i++)
57 threads[i] = new Thread(ExecuteMethodUntilSignal3);
58 threads[i].Start();
61 startEvent.Set();
63 Thread.CurrentThread.Join(3*5000);
65 stopEvent.Set();
68 [Test]
69 public void Multithreaded1()
71 const int threadCount = 30;
73 Thread[] threads = new Thread[threadCount];
75 for (int i = 0; i < threadCount; i++)
77 threads[i] = new Thread(ExecuteMethodUntilSignal1);
78 threads[i].Start();
81 startEvent.Set();
83 Thread.CurrentThread.Join(1*5000);
85 stopEvent.Set();
88 [Test]
89 public void Multithreaded_ReuseVelocityEngineInstanceConcurrently()
91 const int threadCount = 30;
93 Thread[] threads = new Thread[threadCount];
95 for (int i = 0; i < threadCount; i++)
97 threads[i] = new Thread(ExecuteMethodUntilSignal2);
98 threads[i].Start();
101 startEvent.Set();
103 Thread.CurrentThread.Join(1*5000);
105 stopEvent.Set();
108 /// <summary>
109 /// This test starts a VelocityEngine for each execution
110 /// </summary>
111 public void ExecuteMethodUntilSignal1()
113 startEvent.WaitOne(int.MaxValue, false);
115 while (!stopEvent.WaitOne(0, false))
117 VelocityEngine ve = new VelocityEngine();
118 ve.Init();
120 StringWriter sw = new StringWriter();
122 VelocityContext c = new VelocityContext();
123 c.Put("x", new Something());
124 c.Put("items", items);
126 bool ok = ve.Evaluate(c, sw,
127 "ContextTest.CaseInsensitive",
129 #foreach( $item in $items )
130 $item,
131 #end
134 Assert.IsTrue(ok, "Evalutation returned failure");
135 Assert.AreEqual("a,b,c,d,", Normalize(sw));
139 /// <summary>
140 /// This test uses the previously created velocity engine
141 /// </summary>
142 public void ExecuteMethodUntilSignal2()
144 startEvent.WaitOne(int.MaxValue, false);
146 while (!stopEvent.WaitOne(0, false))
148 StringWriter sw = new StringWriter();
150 VelocityContext c = new VelocityContext();
151 c.Put("x", new Something());
152 c.Put("items", items);
154 bool ok = ve.Evaluate(c, sw,
155 "ContextTest.CaseInsensitive",
157 #foreach($item in $items)
158 $item,
159 #end
161 $x.Print('hey') $x.Contents('test', '1')
164 Assert.IsTrue(ok, "Evalutation returned failure");
165 Assert.AreEqual("a,b,c,d,heytest,1", Normalize(sw));
169 public void ExecuteMethodUntilSignal3()
171 startEvent.WaitOne(int.MaxValue, false);
175 while (!stopEvent.WaitOne(0, false))
177 StringWriter sw = new StringWriter();
179 VelocityContext c = new VelocityContext();
180 c.Put("x", new Urlhelper());
182 bool ok = ve.Evaluate(c, sw, "",
183 "#foreach($i in [1..3000]) \r\n" +
184 "#set($temp = $x.For(\"%{controller='Test',id=$i}\")) \r\n" +
185 "#end \r\n");
187 Assert.IsTrue(ok, "Evalutation returned failure");
190 catch(System.Exception ex)
192 Console.WriteLine(ex.ToString());
196 private string Normalize(StringWriter sw)
198 return Regex.Replace(sw.ToString(), "\\s+", "");
201 private class Urlhelper
203 public string For(IDictionary parameters)
205 if (parameters == null)
207 throw new ArgumentNullException("parameters", "parameters cannot be null");
212 string controller = (string) parameters["controller"];
213 int id = (int) parameters["id"];
215 parameters.Remove("controller");
216 parameters.Remove("id");
218 return controller + " " + id;
220 catch (System.Exception ex)
222 Console.WriteLine(ex.ToString());
223 throw;