1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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 Castle
.MonoRail
.Framework
.Tests
18 using System
.Threading
;
20 using Castle
.MonoRail
.Framework
.Internal
;
21 using Castle
.MonoRail
.Framework
.Services
;
23 using NUnit
.Framework
;
27 public class DescriptorBuilderMultiThreadedTestCase
29 private ManualResetEvent startEvent
= new ManualResetEvent(false);
30 private ManualResetEvent stopEvent
= new ManualResetEvent(false);
32 private DefaultControllerDescriptorProvider builder
;
37 builder
= new DefaultControllerDescriptorProvider();
38 builder
.Service(new TestServiceContainer());
42 public void MultithreadTest()
44 const int threadCount
= 20;
46 Thread
[] threads
= new Thread
[threadCount
];
48 for(int i
= 0; i
< threadCount
; i
++)
50 threads
[i
] = new Thread(new ThreadStart(ExecuteMethodUntilSignal
));
56 Thread
.CurrentThread
.Join(1 * 2000);
60 Thread
.CurrentThread
.Join(1 * 1000);
63 public void ExecuteMethodUntilSignal()
65 startEvent
.WaitOne(int.MaxValue
, false);
67 while (!stopEvent
.WaitOne(1, false))
69 ControllerMetaDescriptor desc1
= builder
.BuildDescriptor( new Controller1() );
70 ControllerMetaDescriptor desc2
= builder
.BuildDescriptor( new Controller2() );
72 Assert
.AreEqual( 0, desc1
.ActionProviders
.Count
);
73 Assert
.AreEqual( 1, desc1
.Filters
.Length
);
74 Assert
.IsNotNull( desc1
.Layout
);
75 Assert
.IsNotNull( desc1
.Rescues
);
77 Assert
.AreEqual( 0, desc2
.ActionProviders
.Count
);
78 Assert
.AreEqual( 1, desc2
.Filters
.Length
);
79 Assert
.IsNotNull( desc2
.Layout
);
80 Assert
.IsNotNull( desc2
.Rescues
);
82 ActionMetaDescriptor ac1
= desc1
.GetAction( typeof(Controller1
).GetMethod("Index") );
83 Assert
.IsNotNull( ac1
.SkipRescue
);
84 Assert
.AreEqual( 1, ac1
.SkipFilters
.Count
);
86 ActionMetaDescriptor ac2
= desc2
.GetAction( typeof(Controller2
).GetMethod("Index", new Type
[] { typeof(int) }
) );
87 Assert
.IsNotNull( ac2
.SkipRescue
);
88 Assert
.AreEqual( 1, ac2
.SkipFilters
.Count
);
90 ActionMetaDescriptor ac3
= desc2
.GetAction( typeof(Controller2
).GetMethod("Index", new Type
[] { typeof(String) }
) );
91 Assert
.IsNotNull( ac3
.SkipRescue
);
92 Assert
.AreEqual( 0, ac3
.SkipFilters
.Count
);
97 [Filter( ExecuteEnum
.AfterAction
, typeof(MyFilter
) )]
98 [Rescue("Controller1rescue")]
99 [Layout("Controller1layout")]
100 internal class Controller1
: Controller
102 [SkipRescue
, SkipFilter
]
108 [Filter( ExecuteEnum
.AfterAction
, typeof(MyFilter
) )]
109 [Rescue("Controller1rescue")]
110 [Layout("Controller1layout")]
111 internal class Controller2
: SmartDispatcherController
113 [SkipRescue
, SkipFilter
]
114 public void Index(int id
)
119 public void Index(String name
)
124 internal class MyFilter
: IFilter
126 public bool Perform(ExecuteEnum exec
, IRailsEngineContext context
, IController controller
)