Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default FiberScheduler to rage console #125

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rage/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "thor"
require "rack"
require "rage/version"
require "rage/sync_fiber"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is the same as requiring the file 😞
This patch also applies to the server process, effectively making all code inside the application synchronous.

You can verify this by creating a new Rage app and adding an action that calls sleep 1. Sending however many requests in parallel to this action should only take one second, but with these changes Rage will be processing the requests one by one.

Instead, we only want these changes applied to the console method.

module Rage
class CLICodeGenerator < Thor
Expand Down
18 changes: 1 addition & 17 deletions lib/rage/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "rack/test"
require "json"
require "rage/sync_fiber"

# set up environment
ENV["RAGE_ENV"] ||= "test"
Expand All @@ -14,23 +15,6 @@
# verify the environment
abort("The test suite is running in #{Rage.env} mode instead of 'test'!") unless Rage.env.test?

# mock fiber methods as RSpec tests don't run concurrently
class Fiber
def self.schedule(&block)
fiber = Fiber.new(blocking: true) do
Fiber.current.__set_id
Fiber.current.__set_result(block.call)
end
fiber.resume

fiber
end

def self.await(fibers)
Array(fibers).map(&:__get_result)
end
end

# define request helpers
module RageRequestHelpers
include Rack::Test::Methods
Expand Down
16 changes: 16 additions & 0 deletions lib/rage/sync_fiber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# mock fiber methods as RSpec tests don't run concurrently
class Fiber
def self.schedule(&block)
fiber = Fiber.new(blocking: true) do
Fiber.current.__set_id
Fiber.current.__set_result(block.call)
end
fiber.resume

fiber
end

def self.await(fibers)
Array(fibers).map(&:__get_result)
end
end
21 changes: 21 additions & 0 deletions spec/rage/sync_fiber_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "rage/sync_fiber"

RSpec.describe Fiber do
describe ".schedule" do
let(:fiber_instance) { instance_double(Fiber) }

it "runs immediately" do
expect(Fiber).to receive(:new).with(blocking: true).and_return(fiber_instance)
expect(fiber_instance).to receive(:resume).and_return(true)
expect(described_class.schedule { rand }).to eq(fiber_instance)
end
end

describe ".await" do
let(:fiber_instance) { instance_double(Fiber, __get_result: 10) }

it "immediately returns result" do
expect(described_class.await([fiber_instance])).to eq([10])
end
end
end
Loading