Class: Moleculer::Transporters::Fake

Inherits:
Base
  • Object
show all
Defined in:
lib/moleculer/transporters/fake.rb

Overview

The fake transporter is designed to be used in testing. It is simply an in memory queue and should not be used in production.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Fake

Returns a new instance of Fake.



11
12
13
14
15
16
17
# File 'lib/moleculer/transporters/fake.rb', line 11

def initialize(config)
  super(config)
  # in this case we want to use a class var as this needs to behave like a singleton to mimic how a global
  # transporter functions
  @@subscriptions ||= {} # rubocop:disable Style/ClassVars
  @logger           = config.logger.get_child("[FAKE.TRANSPORTER]")
end

Instance Method Details

#publish(packet) ⇒ Object



24
25
26
27
28
# File 'lib/moleculer/transporters/fake.rb', line 24

def publish(packet)
  @logger.debug "publishing packet to '#{packet.topic}'", packet.to_h
  @logger.debug "processing #{@@subscriptions[packet.topic].length} callbacks for '#{packet.topic}'"
  @@subscriptions[packet.topic].each { |c| c.call(packet) }
end

#startObject



30
31
32
# File 'lib/moleculer/transporters/fake.rb', line 30

def start
  true
end

#stopObject



34
35
36
# File 'lib/moleculer/transporters/fake.rb', line 34

def stop
  true
end

#subscribe(channel, &block) ⇒ Object



19
20
21
22
# File 'lib/moleculer/transporters/fake.rb', line 19

def subscribe(channel, &block)
  @@subscriptions[channel] ||= []
  @@subscriptions[channel] << block
end