Class: Toxiproxy::ProxyCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/toxiproxy/proxy_collection.rb

Overview

ProxyCollection represents a set of proxies. This allows to easily perform actions on every proxy in the collection.

Unfortunately, it doesn’t implement all of Enumerable because there’s no way to subclass an Array or include Enumerable for the methods to return a Collection instead of an Array (see MRI). Instead, we delegate methods where it doesn’t matter and only allow the filtering methods that really make sense on a proxy collection.

Constant Summary collapse

DELEGATED_METHODS =
[:length, :size, :count, :find, :each, :map]
DEFINED_METHODS =
[:select, :reject, :grep, :down]
METHODS =
DEFINED_METHODS + DELEGATED_METHODS

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ ProxyCollection

Returns a new instance of ProxyCollection.



19
20
21
# File 'lib/toxiproxy/proxy_collection.rb', line 19

def initialize(collection)
  @collection = collection
end

Instance Method Details

#destroyObject

Destroys all toxiproxy’s in the collection



60
61
62
# File 'lib/toxiproxy/proxy_collection.rb', line 60

def destroy
  @collection.each(&:destroy)
end

#disableObject



51
52
53
# File 'lib/toxiproxy/proxy_collection.rb', line 51

def disable
  @collection.each(&:disable)
end

#down(&block) ⇒ Object

Sets every proxy in the collection as down. For example:

Toxiproxy.grep(/redis/).down { .. }

Would simulate every Redis server being down for the duration of the block.



29
30
31
32
33
# File 'lib/toxiproxy/proxy_collection.rb', line 29

def down(&block)
  @collection.inject(block) { |nested, proxy|
    -> { proxy.down(&nested) }
  }.call
end

#downstream(toxic, attrs = {}) ⇒ Object Also known as: toxicate, toxic

Set a downstream toxic.



43
44
45
46
47
# File 'lib/toxiproxy/proxy_collection.rb', line 43

def downstream(toxic, attrs = {})
  toxics = ToxicCollection.new(@collection)
  toxics.downstream(toxic, attrs)
  toxics
end

#enableObject



55
56
57
# File 'lib/toxiproxy/proxy_collection.rb', line 55

def enable
  @collection.each(&:enable)
end

#grep(regex) ⇒ Object

Grep allows easily selecting a subset of proxies, by returning a ProxyCollection with every proxy name matching the regex passed.



74
75
76
77
78
# File 'lib/toxiproxy/proxy_collection.rb', line 74

def grep(regex)
  self.class.new(@collection.select { |proxy|
    proxy.name =~ regex
  })
end

#reject(&block) ⇒ Object



68
69
70
# File 'lib/toxiproxy/proxy_collection.rb', line 68

def reject(&block)
  self.class.new(@collection.reject(&block))
end

#select(&block) ⇒ Object



64
65
66
# File 'lib/toxiproxy/proxy_collection.rb', line 64

def select(&block)
  self.class.new(@collection.select(&block))
end

#upstream(toxic, attrs = {}) ⇒ Object

Set an upstream toxic.



36
37
38
39
40
# File 'lib/toxiproxy/proxy_collection.rb', line 36

def upstream(toxic, attrs = {})
  toxics = ToxicCollection.new(@collection)
  toxics.upstream(toxic, attrs)
  toxics
end