Skip to content

Instantly share code, notes, and snippets.

@palkan
Created November 14, 2023 02:42
Show Gist options
  • Save palkan/aa7035cebaeca7ed76e433981f90c07b to your computer and use it in GitHub Desktop.
Save palkan/aa7035cebaeca7ed76e433981f90c07b to your computer and use it in GitHub Desktop.
TestProf vs. Wisper
# frozen_string_literal: true
# Patch Wisper to disable broadcasting by default
# (to speed up tests).
# Profile Wisper publishing via EventProf
TestProf::EventProf.monitor(Wisper::Publisher, "wisper.publisher.broadcast", :broadcast)
module Wisper
module Testing
module PublisherExt
def broadcast(...)
return if Wisper::Testing.fake?
super
end
end
class << self
def fake? = @mode == :fake
def inline? = @mode == :inline
def fake!
unless block_given?
@mode = :fake
return
end
was_mode = @mode
@mode = :fake
yield
ensure
@mode = was_mode
end
def inline!
unless block_given?
@mode = :inline
return
end
was_mode = @mode
@mode = :inline
yield
ensure
@mode = was_mode
end
end
end
end
# Use inline! by default, switch to fake! when
# all specs are refactored.
Wisper::Testing.inline!
Wisper::Publisher.prepend(Wisper::Testing::PublisherExt)
RSpec.configure do |config|
# TEMP: Switch to fake! for test types that have been refactored
refactored_types = %i[model controller service].freeze
config.around do |ex|
next ex.run unless refactored_types.include?(ex.metadata[:type])
Wisper::Testing.fake! { ex.run }
end
# Use tag `wisper: true` (or `:wisper`) to enable Wisper publishing
config.around(:each, wisper: true) do |ex|
Wisper::Testing.inline! { ex.run }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment