33using System . Linq ;
44using Microsoft . VisualStudio . TestTools . UnitTesting ;
55using FluentAssertions ;
6+ using System ;
67
78namespace Noesis . Javascript . Tests
89{
@@ -14,29 +15,29 @@ namespace Noesis.Javascript.Tests
1415 public class IsolationTests
1516 {
1617 [ TestMethod ]
17- public void RunIsolatesTest ( )
18+ public void IsolatesRunSimultaneously ( )
1819 {
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 ( ) ;
2934 }
3035
31- static void RunInstance ( )
36+ static void RunAndCallbackIntoCsharp ( Action run_in_javascript_thread )
3237 {
3338 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();" ) ;
4041 }
4142 }
4243
0 commit comments