From c43915a3b7fa43996db3419696c11de090a17561 Mon Sep 17 00:00:00 2001 From: Herwin Date: Thu, 31 Aug 2023 18:22:15 +0200 Subject: [PATCH] Add specs for Fiber.scheduler and Fiber.set_scheduler --- core/fiber/scheduler_spec.rb | 8 ++++++ core/fiber/set_scheduler_spec.rb | 8 ++++++ core/fiber/shared/scheduler.rb | 49 ++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 core/fiber/scheduler_spec.rb create mode 100644 core/fiber/set_scheduler_spec.rb create mode 100644 core/fiber/shared/scheduler.rb diff --git a/core/fiber/scheduler_spec.rb b/core/fiber/scheduler_spec.rb new file mode 100644 index 0000000000..15a03c1479 --- /dev/null +++ b/core/fiber/scheduler_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require_relative 'shared/scheduler' + +require "fiber" + +describe "Fiber.scheduler" do + it_behaves_like :scheduler, :scheduler +end diff --git a/core/fiber/set_scheduler_spec.rb b/core/fiber/set_scheduler_spec.rb new file mode 100644 index 0000000000..82f6acbe86 --- /dev/null +++ b/core/fiber/set_scheduler_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require_relative 'shared/scheduler' + +require "fiber" + +describe "Fiber.scheduler" do + it_behaves_like :scheduler, :set_scheduler +end diff --git a/core/fiber/shared/scheduler.rb b/core/fiber/shared/scheduler.rb new file mode 100644 index 0000000000..2110a5c8b4 --- /dev/null +++ b/core/fiber/shared/scheduler.rb @@ -0,0 +1,49 @@ +describe :scheduler, shared: true do + it "validates the scheduler for required methods" do + required_methods = [:block, :unblock, :kernel_sleep, :io_wait] + required_methods.each do |missing_method| + scheduler = Object.new + required_methods.difference([missing_method]).each do |method| + scheduler.define_singleton_method(method) {} + end + -> { Fiber.set_scheduler(scheduler) }.should raise_error(ArgumentError, /Scheduler must implement ##{missing_method}/) + end + end + + it "can set and get the scheduler" do + required_methods = [:block, :unblock, :kernel_sleep, :io_wait] + scheduler = Object.new + required_methods.each do |method| + scheduler.define_singleton_method(method) {} + end + Fiber.set_scheduler(scheduler) + Fiber.scheduler.should == scheduler + end + + it "returns the scheduler after setting it" do + required_methods = [:block, :unblock, :kernel_sleep, :io_wait] + scheduler = Object.new + required_methods.each do |method| + scheduler.define_singleton_method(method) {} + end + result = Fiber.set_scheduler(scheduler) + result.should == scheduler + end + + it "can remove the scheduler" do + required_methods = [:block, :unblock, :kernel_sleep, :io_wait] + scheduler = Object.new + required_methods.each do |method| + scheduler.define_singleton_method(method) {} + end + Fiber.set_scheduler(scheduler) + Fiber.set_scheduler(nil) + Fiber.scheduler.should be_nil + end + + it "can assign a nil scheduler multiple times" do + Fiber.set_scheduler(nil) + Fiber.set_scheduler(nil) + Fiber.scheduler.should be_nil + end +end