|
18 | 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
19 | 19 | # THE SOFTWARE. |
20 | 20 |
|
| 21 | +require 'async/rspec' |
21 | 22 | require 'async/reactor' |
22 | 23 | require 'async/barrier' |
23 | 24 | require 'net/http' |
24 | 25 |
|
25 | | -RSpec.describe Async::Scheduler do |
26 | | - include_context Async::RSpec::Reactor |
27 | | - |
28 | | - it "can intercept sleep" do |
29 | | - expect(reactor).to receive(:kernel_sleep).with(0.001) |
30 | | - |
31 | | - sleep(0.001) |
32 | | - end |
33 | | - |
| 26 | +RSpec.describe Async::Scheduler do |
34 | 27 | describe 'Fiber.schedule' do |
35 | 28 | it "can start child task" do |
36 | 29 | fiber = nil |
|
44 | 37 | expect(fiber).to_not be_nil |
45 | 38 | expect(fiber).to be_kind_of(Fiber) |
46 | 39 | end |
47 | | - end |
48 | | - |
49 | | - describe 'Process.wait' do |
50 | | - it "can wait on child process" do |
51 | | - expect(reactor).to receive(:process_wait).and_call_original |
52 | | - |
53 | | - pid = ::Process.spawn("true") |
54 | | - _, status = Process.wait2(pid) |
55 | | - expect(status).to be_success |
56 | | - end |
57 | | - end |
58 | | - |
59 | | - describe 'Kernel#system' do |
60 | | - it "can execute child process" do |
61 | | - # expect(reactor).to receive(:process_wait).and_call_original |
62 | | - |
63 | | - ::Kernel.system("true") |
64 | | - expect($?).to be_success |
65 | | - end |
66 | | - end |
67 | | - |
68 | | - describe 'Kernel#`' do |
69 | | - it "can execute child process and capture output" do |
70 | | - expect(`echo OK`).to be == "OK\n" |
71 | | - expect($?).to be_success |
72 | | - end |
73 | | - |
74 | | - it "can execute child process with delay and capture output" do |
75 | | - expect(`sleep 1; echo OK`).to be == "OK\n" |
76 | | - expect($?).to be_success |
77 | | - end |
78 | | - end |
79 | | - |
80 | | - describe 'IO.pipe' do |
81 | | - let(:message) {"Helloooooo World!"} |
82 | 40 |
|
83 | | - it "can send message via pipe" do |
84 | | - input, output = IO.pipe |
| 41 | + it "can schedule task before starting scheduler" do |
| 42 | + sequence = [] |
85 | 43 |
|
86 | | - reactor.async do |
87 | | - sleep(0.001) |
| 44 | + thread = Thread.new do |
| 45 | + scheduler = Async::Scheduler.new |
88 | 46 |
|
89 | | - message.each_char do |character| |
90 | | - output.write(character) |
| 47 | + scheduler.async do |
| 48 | + sequence << :running |
91 | 49 | end |
92 | 50 |
|
93 | | - output.close |
| 51 | + Fiber.set_scheduler(scheduler) |
94 | 52 | end |
95 | 53 |
|
96 | | - expect(input.read).to be == message |
97 | | - |
98 | | - ensure |
99 | | - input.close |
100 | | - output.close |
101 | | - end |
102 | | - |
103 | | - it "can fetch website using Net::HTTP" do |
104 | | - barrier = Async::Barrier.new |
105 | | - events = [] |
106 | | - |
107 | | - 3.times do |i| |
108 | | - barrier.async do |
109 | | - events << i |
110 | | - response = Net::HTTP.get(URI "https://www.codeotaku.com/index") |
111 | | - expect(response).to_not be_nil |
112 | | - events << i |
113 | | - end |
114 | | - end |
115 | | - |
116 | | - barrier.wait |
117 | | - |
118 | | - # The requests all get started concurrently: |
119 | | - expect(events.first(3)).to be == [0, 1, 2] |
120 | | - end |
121 | | - end |
122 | | - |
123 | | - context "with thread" do |
124 | | - it "can join thread" do |
125 | | - queue = Thread::Queue.new |
126 | | - thread = Thread.new{queue.pop} |
127 | | - |
128 | | - waiting = 0 |
129 | | - |
130 | | - 3.times do |
131 | | - Async do |
132 | | - waiting += 1 |
133 | | - thread.join |
134 | | - waiting -= 1 |
135 | | - end |
136 | | - end |
137 | | - |
138 | | - expect(waiting).to be == 3 |
139 | | - queue.close |
140 | | - end |
141 | | - end |
142 | | - |
143 | | - context "with queue" do |
144 | | - subject {::Thread::Queue.new} |
145 | | - let(:item) {"Hello World"} |
146 | | - |
147 | | - it "can pass items between thread and fiber" do |
148 | | - Async do |
149 | | - expect(subject.pop).to be == item |
150 | | - end |
| 54 | + thread.join |
151 | 55 |
|
152 | | - ::Thread.new do |
153 | | - expect(Fiber).to be_blocking |
154 | | - subject.push(item) |
155 | | - end.join |
| 56 | + expect(sequence).to be == [:running] |
156 | 57 | end |
157 | 58 | end |
158 | 59 | end |
0 commit comments