Skip to content

Commit 86685d8

Browse files
authored
Merge pull request #161 from rage-rb/sub-ms-sleep
Pause on sub-second sleeps
2 parents 63a124c + c40010a commit 86685d8

5 files changed

Lines changed: 30 additions & 0 deletions

File tree

lib/rage/fiber_scheduler.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def io_write(io, buffer, length, offset = 0)
6363

6464
def kernel_sleep(duration = nil)
6565
block(nil, duration || 0)
66+
Fiber.pause if duration.nil? || duration < 1
6667
end
6768

6869
# TODO: GC works a little strange with this closure;

spec/fiber_scheduler_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,22 @@
338338
-> { expect((1..1.5)).to include(result) }
339339
end
340340
end
341+
342+
it "correctly sleeps with 0" do
343+
within_reactor do
344+
Fiber.await(Fiber.schedule { sleep(0) })
345+
-> {}
346+
rescue => e
347+
-> { raise e }
348+
end
349+
end
350+
351+
it "correctly sleeps with no arguments" do
352+
within_reactor do
353+
Fiber.await(Fiber.schedule { sleep })
354+
-> {}
355+
rescue => e
356+
-> { raise e }
357+
end
358+
end
341359
end

spec/integration/integration_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@
155155
expect(response.code).to eq(500)
156156
expect(response.to_s).to include("raised from inner fiber")
157157
end
158+
159+
it "corrrectly processes short sleep calls" do
160+
response = HTTP.get("http://localhost:3000/async/short_sleep")
161+
expect(response.code).to eq(200)
162+
end
158163
end
159164

160165
context "with before actions" do

spec/integration/test_app/app/controllers/async_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ def raise_error
2828
end
2929
Fiber.await f
3030
end
31+
32+
def short_sleep
33+
sleep 0.0001
34+
head :ok
35+
end
3136
end

spec/integration/test_app/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
get "async/long", to: "async#long"
2020
get "async/empty", to: "async#empty"
2121
get "async/raise_error", to: "async#raise_error"
22+
get "async/short_sleep", to: "async#short_sleep"
2223

2324
get "before_actions/get", to: "before_actions#get"
2425

0 commit comments

Comments
 (0)