3
3
using System . Linq ;
4
4
using Microsoft . VisualStudio . TestTools . UnitTesting ;
5
5
using FluentAssertions ;
6
+ using System ;
6
7
7
8
namespace Noesis . Javascript . Tests
8
9
{
@@ -14,29 +15,29 @@ namespace Noesis.Javascript.Tests
14
15
public class IsolationTests
15
16
{
16
17
[ TestMethod ]
17
- public void RunIsolatesTest ( )
18
+ public void IsolatesRunSimultaneously ( )
18
19
{
19
-
20
- Stopwatch timer = new Stopwatch ( ) ;
21
- timer . Start ( ) ;
22
-
23
- Thread thread = new Thread ( RunInstance ) ;
24
- thread . Start ( ) ; // First instance
25
- RunInstance ( ) ; // Second instance
26
- thread . Join ( ) ;
27
-
28
- timer . ElapsedMilliseconds . Should ( ) . BeLessThan ( 1999 , "It took too long, they must not be running in parallel." ) ;
20
+ var thread1_started = new EventWaitHandle ( false , EventResetMode . ManualReset ) ;
21
+ var thread2_started = new EventWaitHandle ( false , EventResetMode . ManualReset ) ;
22
+ Thread thread1 = new Thread ( ( ) => RunAndCallbackIntoCsharp ( ( ) => {
23
+ thread1_started . Set ( ) ;
24
+ thread2_started . WaitOne ( ) ;
25
+ } ) ) ;
26
+ Thread thread2 = new Thread ( ( ) => RunAndCallbackIntoCsharp ( ( ) => {
27
+ thread2_started . Set ( ) ;
28
+ thread1_started . WaitOne ( ) ;
29
+ } ) ) ;
30
+ thread1 . Start ( ) ;
31
+ thread2 . Start ( ) ;
32
+ thread1 . Join ( ) ;
33
+ thread2 . Join ( ) ;
29
34
}
30
35
31
- static void RunInstance ( )
36
+ static void RunAndCallbackIntoCsharp ( Action run_in_javascript_thread )
32
37
{
33
38
using ( JavascriptContext context = new JavascriptContext ( ) ) {
34
- int i = ( int ) context . Run ( @"
35
- var started = new Date();
36
- var i = 0;
37
- while (new Date().getTime() - started.getTime() < 1000)
38
- i ++;
39
- i;" ) ;
39
+ context . SetParameter ( "csharp_code" , run_in_javascript_thread ) ;
40
+ context . Run ( "csharp_code();" ) ;
40
41
}
41
42
}
42
43
0 commit comments