lib/fluffle/testing.rb in fluffle-0.2.1 vs lib/fluffle/testing.rb in fluffle-0.2.2
- old
+ new
@@ -1,22 +1,38 @@
require 'concurrent'
module Fluffle
module Testing
- def self.setup!
- # Inject our own custom `Connectable` implementation
- [Fluffle::Client, Fluffle::Server].each do |mod|
- mod.include Connectable
- end
+ class << self
+ def setup!(use_fake_thread_pool: true)
+ # Inject our own custom `Connectable` implementation
+ [Fluffle::Client, Fluffle::Server].each do |mod|
+ mod.include Connectable
+ end
- Fluffle::Server.class_eval do
- # Overwriting this so that we don't actually block waiting for signal
- def wait_for_signal
- # pass
+ Fluffle::Server.class_eval do
+ # Overwriting this so that we don't actually block waiting for signal
+ def wait_for_signal
+ # pass
+ end
end
+
+ if use_fake_thread_pool
+ Fluffle::Server.class_eval do
+ # Wrap the `initialize` implementation to switch out the handler pool
+ # to a local unthreaded one
+ alias_method :original_initialize, :initialize
+
+ def initialize(*args)
+ original_initialize *args
+
+ @handler_pool = ThreadPool.new
+ end
+ end
+ end
end
- end
+ end # class << self
# Patch in a new `#connect` method that injects the loopback
module Connectable
def self.included(klass)
klass.class_eval do
@@ -27,10 +43,18 @@
end
end
end
end
+ # Fake thread pool that executes `#post`'ed blocks immediately in the
+ # current thread
+ class ThreadPool
+ def post(&block)
+ block.call
+ end
+ end
+
# Fake RabbitMQ server presented through a subset of the `Bunny`
# library's interface
class Loopback
# Singleton server instance that lives in the process
def self.instance
@@ -128,7 +152,5 @@
end
end # class LoopbackServer
end # module Testing
end # module Fluffle
-
-Fluffle::Testing.setup!