Browse Source

Use a more specific exception in the `CancellationConstraint` class (#769)

Andrey Taritsyn 5 years ago
parent
commit
6da41ae948

+ 35 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -2,6 +2,7 @@
 using System.Globalization;
 using System.Globalization;
 using System.IO;
 using System.IO;
 using System.Reflection;
 using System.Reflection;
+using System.Threading;
 using Esprima;
 using Esprima;
 using Esprima.Ast;
 using Esprima.Ast;
 using Jint.Native;
 using Jint.Native;
@@ -738,6 +739,40 @@ namespace Jint.Tests.Runtime
             );
             );
         }
         }
 
 
+        [Fact]
+        public void ShouldThrowExecutionCanceled()
+        {
+            Assert.Throws<ExecutionCanceledException>(
+                () =>
+                {
+                    using (var tcs = new CancellationTokenSource())
+                    using (var waitHandle = new ManualResetEvent(false))
+                    {
+                        var engine = new Engine(cfg => cfg.CancellationToken(tcs.Token));
+
+                        ThreadPool.QueueUserWorkItem(state =>
+                        {
+                            waitHandle.WaitOne();
+                            tcs.Cancel();
+                        });
+
+                        engine.SetValue("waitHandle", waitHandle);
+                        engine.Execute(@"
+                            function sleep(millisecondsTimeout) {
+                                var totalMilliseconds = new Date().getTime() + millisecondsTimeout;
+
+                                while (new Date() < totalMilliseconds) { }
+                            }
+
+                            sleep(100);
+                            waitHandle.Set();
+                            sleep(5000);
+                        ");
+                    }
+                }
+            );
+        }
+
 
 
         [Fact]
         [Fact]
         public void CanDiscardRecursion()
         public void CanDiscardRecursion()

+ 1 - 1
Jint/Constraints/CancellationConstraint.cs

@@ -16,7 +16,7 @@ namespace Jint.Constraints
         {
         {
             if (_cancellationToken.IsCancellationRequested)
             if (_cancellationToken.IsCancellationRequested)
             {
             {
-                ExceptionHelper.ThrowStatementsCountOverflowException();
+                ExceptionHelper.ThrowExecutionCanceledException();
             }
             }
         }
         }
 
 

+ 5 - 0
Jint/Runtime/ExceptionHelper.cs

@@ -182,5 +182,10 @@ namespace Jint.Runtime
         {
         {
             throw new MemoryLimitExceededException(message);
             throw new MemoryLimitExceededException(message);
         }
         }
+
+        public static void ThrowExecutionCanceledException()
+        {
+            throw new ExecutionCanceledException();
+        }
     }
     }
 }
 }

+ 9 - 0
Jint/Runtime/ExecutionCanceledException.cs

@@ -0,0 +1,9 @@
+namespace Jint.Runtime
+{
+    public class ExecutionCanceledException : JintException
+    {
+        public ExecutionCanceledException() : base("The script execution was canceled.")
+        {
+        }
+    }
+}