Zyre::Testing::

EventFactory class

A Factory for generating synthesized ZRE events for testing

Constants

DEFAULT_CONFIG

Default config values to use to construct events

DEFAULT_GROUP
DEFAULT_HEADERS

Default config values

DEFAULT_MSG
DEFAULT_NETWORK

The network to pretend events are sent over

Attributes

group RW

The name of the group set on any events created by this factory which have a group

headers RW

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

Public Class Methods

default_config()

Return the Hash of default config options.

# File lib/zyre/testing.rb, line 41
def self::default_config
        return const_get( :DEFAULT_CONFIG )
end
default_headers()

Return the Hash of default headers to use to construct events which have them.

# File lib/zyre/testing.rb, line 48
def self::default_headers
        return const_get( :DEFAULT_HEADERS ).dup
end
new( **config )

Create a new factory that will use the values from the specified config as defaults when constructing events.

# File lib/zyre/testing.rb, line 75
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
random_addr()

Return a random ZeroMQ-style address that points to an ephemeral port on the DEFAULT_NETWORK.

# File lib/zyre/testing.rb, line 68
def self::random_addr
        return "tcp://%s:%d" % [ self.random_network_address, self.random_ephemeral_port ]
end
random_ephemeral_port()

Return a port number that’s in the ephemeral range.

# File lib/zyre/testing.rb, line 61
def self::random_ephemeral_port
        return rand( 49152 ... 65535)
end
random_network_address()

Return a random network address that’s in the DEFAULT_NETWORK

# File lib/zyre/testing.rb, line 54
def self::random_network_address
        last_quad = rand( 1..254 )
        return DEFAULT_NETWORK.sub( /(?<=\.)0$/, last_quad.to_s )
end

Public Instance Methods

enter( **overrides )

Generate an ENTER event.

# File lib/zyre/testing.rb, line 129
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
evasive( **overrides )

Generate an EVASIVE event.

# File lib/zyre/testing.rb, line 189
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
exit( **overrides )

Generate an EXIT event.

# File lib/zyre/testing.rb, line 235
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
join( **overrides )

Generate a JOIN event.

# File lib/zyre/testing.rb, line 143
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
leader( **overrides )

Generate a LEADER event.

# File lib/zyre/testing.rb, line 223
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
leave( **overrides )

Generate a LEAVE event.

# File lib/zyre/testing.rb, line 211
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
normalized_headers()

Returns a Hash of the configured headers with stringified keys and values.

# File lib/zyre/testing.rb, line 123
def normalized_headers
        return Zyre.normalize_headers( self.headers )
end
shout( group=nil, *msg, **overrides )

Generate a SHOUT event.

# File lib/zyre/testing.rb, line 155
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
silent( **overrides )

Generate a SILENT event.

# File lib/zyre/testing.rb, line 200
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
stop( **overrides )

Generate a STOP event.

# File lib/zyre/testing.rb, line 246
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.

# File lib/zyre/testing.rb, line 174
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