This repository was archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy paththreading_spec.rb
More file actions
90 lines (77 loc) · 2.08 KB
/
threading_spec.rb
File metadata and controls
90 lines (77 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'spec_helper'
describe "Timeouts" do
it "allows for timeout on context" do
ctx = V8::Context.new(:timeout => 10)
lambda {ctx.eval("while(true){}")}.should(raise_error)
# context should not be bust after it exploded once
ctx["x"] = 1;
ctx.eval("x=2;")
ctx["x"].should == 2
end
it "allows changing the timeout on an existing context" do
ctx = V8::Context.new(:timeout => 200)
js = <<-js
// sleep for 50 ms
var now = new Date().getTime();
while(new Date().getTime() < now + 50) {}
if(typeof x === 'undefined') {
x = 1;
} else {
x = 2;
}
js
ctx.eval(js)
ctx["x"].should == 1
ctx.timeout = 10
lambda {ctx.eval(js)}.should(raise_error)
ctx.timeout = 200
ctx.eval(js)
ctx["x"].should == 2
end
end
describe "using v8 from multiple threads", :threads => true do
it "creates contexts from within threads" do
10.times.collect do
Thread.new do
V8::Context.new
end
end.each {|t| t.join}
V8::Context.new
end
it "executes codes on multiple threads simultaneously" do
5.times.collect{V8::Context.new}.collect do |ctx|
Thread.new do
ctx['x'] = 99
while ctx['x'] > 0
ctx.eval 'for (i=10000;i;i--){};--x'
end
end
end.each {|t| t.join}
end
it "can access the current thread id" do
V8::C::Locker() do
V8::C::V8::GetCurrentThreadId().should_not be_nil
end
end
it "can pre-empt a running JavaScript thread" do
pending "need to release the GIL while executing V8 code"
begin
V8::C::Locker::StartPreemption(2)
thread_id = nil
Thread.new do
loop until thread_id
puts "thread id: #{thread_id}"
V8::C::V8::TerminateExecution(thread_id)
end
Thread.new do
V8::C::Locker() do
thread_id = V8::C::V8::GetCurrentThreadId()
V8::Context.new {|cxt| cxt.eval('while (true) {}')}
end
end
V8::C::V8::TerminateExecution(thread_id)
ensure
V8::C::Locker::StopPreemption()
end
end
end