- const char* loopForeverScript = "var startTime = currentCPUTime(); try { while (true) { if (currentCPUTime() - startTime > .150) break; } } catch(e) { }";
- JSStringRef script = JSStringCreateWithUTF8CString(loopForeverScript);
- double startTime;
- double endTime;
- exception = NULL;
- shouldTerminateCallbackWasCalled = false;
- startTime = currentCPUTime();
- v = JSEvaluateScript(context, script, NULL, NULL, 1, &exception);
- endTime = currentCPUTime();
-
- if (((endTime - startTime) >= .150f) || !shouldTerminateCallbackWasCalled) {
- if (!((endTime - startTime) < .150f))
- printf("FAIL: script did not timed out as expected.\n");
- if (!shouldTerminateCallbackWasCalled)
- printf("FAIL: script timeout callback was not called.\n");
- failed = true;
- }
-
- if (exception)
- printf("PASS: TerminatedExecutionException was not catchable as expected.\n");
- else {
- printf("FAIL: TerminatedExecutionException was caught.\n");
- failed = true;
- }
- }
-
- /* Test script timeout with no callback: */
- JSContextGroupSetExecutionTimeLimit(contextGroup, .10f, 0, 0);
- {
- const char* loopForeverScript = "var startTime = currentCPUTime(); while (true) { if (currentCPUTime() - startTime > .150) break; } ";
- JSStringRef script = JSStringCreateWithUTF8CString(loopForeverScript);
- double startTime;
- double endTime;
- exception = NULL;
- startTime = currentCPUTime();
- v = JSEvaluateScript(context, script, NULL, NULL, 1, &exception);
- endTime = currentCPUTime();
-
- if (((endTime - startTime) < .150f) && shouldTerminateCallbackWasCalled)
- printf("PASS: script timed out as expected when no callback is specified.\n");
- else {
- if (!((endTime - startTime) < .150f))
- printf("FAIL: script did not timed out as expected when no callback is specified.\n");
- failed = true;
- }
-
- if (!exception) {
- printf("FAIL: TerminatedExecutionException was not thrown.\n");
- failed = true;
- }
- }
-
- /* Test script timeout cancellation: */
- JSContextGroupSetExecutionTimeLimit(contextGroup, 0.10f, cancelTerminateCallback, 0);
- {
- const char* loopForeverScript = "var startTime = currentCPUTime(); while (true) { if (currentCPUTime() - startTime > .150) break; } ";
- JSStringRef script = JSStringCreateWithUTF8CString(loopForeverScript);
- double startTime;
- double endTime;
- exception = NULL;
- startTime = currentCPUTime();
- v = JSEvaluateScript(context, script, NULL, NULL, 1, &exception);
- endTime = currentCPUTime();
-
- if (((endTime - startTime) >= .150f) && cancelTerminateCallbackWasCalled && !exception)
- printf("PASS: script timeout was cancelled as expected.\n");
- else {
- if (((endTime - startTime) < .150) || exception)
- printf("FAIL: script timeout was not cancelled.\n");
- if (!cancelTerminateCallbackWasCalled)
- printf("FAIL: script timeout callback was not called.\n");
- failed = true;