Zyre::Testing::
EventFactory
class
A Factory for generating synthesized ZRE events for testing
- DEFAULT_CONFIG
Default config values to use to construct events
- DEFAULT_GROUP
Default config values
- DEFAULT_MSG
- DEFAULT_NETWORK
The network to pretend events are sent over
- group RW
The name of the group set on any events created by this factory which have a group
The Hash of headers to set on any events created by this factory which have headers
- msg RW
The message data to set on any events create by this factory that have a msg
.
- peer_addr RW
The peer_addr
that’s assigned to any events created by this factory
- peer_name RW
The peer_name
that’s assigned to any events created by this factory
- peer_uuid RW
The peer_uuid
that’s assigned to any events created by this factory
Return the Hash of default config options.
def self::default_config
return const_get( :DEFAULT_CONFIG )
end
Create a new factory that will use the values from the specified config
as defaults when constructing events.
def initialize( **config )
config = self.class.default_config.merge( config )
self.log.debug "Setting up a factory with the config: %p" % [ config ]
@peer_uuid = config[:peer_uuid] || SecureRandom.uuid
@peer_name = config[:peer_name] || "S-%s" % [ @peer_uuid[0, 6] ]
@peer_addr = config[:peer_addr] || self.class.random_addr
@headers = config[:headers]
@group = config[:group]
@msg = config[:msg]
self.log.debug( self )
end
Return a random ZeroMQ-style address that points to an ephemeral port on the DEFAULT_NETWORK
.
def self::random_addr
return "tcp://%s:%d" % [ self.random_network_address, self.random_ephemeral_port ]
end
Return a port number that’s in the ephemeral range.
def self::random_ephemeral_port
return rand( 49152 ... 65535)
end
Return a random network address that’s in the DEFAULT_NETWORK
def self::random_network_address
last_quad = rand( 1..254 )
return DEFAULT_NETWORK.sub( /(?<=\.)0$/, last_quad.to_s )
end
Generate an ENTER event.
def enter( **overrides )
headers = overrides.delete( :headers ) || self.headers
uuid = overrides.delete( :peer_uuid ) || self.peer_uuid
config = {
peer_name: self.peer_name,
peer_addr: self.peer_addr,
headers: Zyre.normalize_headers( headers )
}.merge( overrides )
return Zyre::Event.synthesize( :enter, uuid, **config )
end
Generate an EVASIVE event.
def evasive( **overrides )
uuid = overrides.delete( :peer_uuid ) || self.peer_uuid
config = {
peer_name: self.peer_name
}.merge( overrides )
return Zyre::Event.synthesize( :evasive, uuid, **config )
end
Generate an EXIT event.
def exit( **overrides )
uuid = overrides.delete( :peer_uuid ) || self.peer_uuid
config = {
peer_name: self.peer_name
}.merge( overrides )
return Zyre::Event.synthesize( :exit, uuid, **config )
end
Generate a JOIN event.
def join( **overrides )
uuid = overrides.delete( :peer_uuid ) || self.peer_uuid
config = {
peer_name: self.peer_name,
group: self.group
}.merge( overrides )
return Zyre::Event.synthesize( :join, uuid, **config )
end
Generate a LEADER event.
def leader( **overrides )
uuid = overrides.delete( :peer_uuid ) || self.peer_uuid
config = {
peer_name: self.peer_name,
group: self.group
}.merge( overrides )
return Zyre::Event.synthesize( :leader, uuid, **config )
end
Generate a LEAVE event.
def leave( **overrides )
uuid = overrides.delete( :peer_uuid ) || self.peer_uuid
config = {
peer_name: self.peer_name,
group: self.group
}.merge( overrides )
return Zyre::Event.synthesize( :leave, uuid, **config )
end
shout( group=nil, *msg, **overrides )
Generate a SHOUT event.
def shout( group=nil, *msg, **overrides )
uuid = overrides.delete( :peer_uuid ) || self.peer_uuid
overrides[:group] = group if group
overrides[:msg] = msg if !msg.empty?
config = {
peer_name: self.peer_name,
group: self.group,
msg: self.msg
}.merge( overrides )
return Zyre::Event.synthesize( :shout, uuid, **config )
end
Generate a SILENT event.
def silent( **overrides )
uuid = overrides.delete( :peer_uuid ) || self.peer_uuid
config = {
peer_name: self.peer_name
}.merge( overrides )
return Zyre::Event.synthesize( :silent, uuid, **config )
end
Generate a STOP event.
def stop( **overrides )
uuid = overrides.delete( :peer_uuid ) || self.peer_uuid
config = {
peer_name: self.peer_name
}.merge( overrides )
return Zyre::Event.synthesize( :stop, uuid, **config )
end
whisper( _ignored=nil, *msg, **overrides )
Generate a WHISPER event. The first positional argument, which would normally be the UUID of the peer to WHISPER to is ignored, since the generated event’s peer_uuid
is the sending node’s not the receiving one’s.
def whisper( _ignored=nil, *msg, **overrides )
uuid = overrides.delete( :peer_uuid ) || self.peer_uuid
overrides[:msg] = msg if !msg.empty?
config = {
peer_name: self.peer_name,
msg: self.msg
}.merge( overrides )
return Zyre::Event.synthesize( :whisper, uuid, **config )
end