litmus_paper 0.9.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,8 @@
1
1
  module LitmusPaper
2
2
  module Metric
3
3
  class InternetHealth
4
- def initialize(hosts, options = {})
4
+ def initialize(weight, hosts, options = {})
5
+ @weight = weight
5
6
  @hosts = hosts
6
7
  @options = options
7
8
  @timeout = options.fetch(:timeout_seconds, 5)
@@ -22,7 +23,7 @@ module LitmusPaper
22
23
  end
23
24
 
24
25
  def current_health
25
- health = 100 * @hosts.reduce(Rational(0)) do |memo, host|
26
+ health = @weight * @hosts.reduce(Rational(0)) do |memo, host|
26
27
  if tcp_connect?(*host.split(':'))
27
28
  memo += Rational(1) / Rational(@hosts.length)
28
29
  end
@@ -32,7 +33,7 @@ module LitmusPaper
32
33
  end
33
34
 
34
35
  def to_s
35
- "Metric::InternetHealth(#{@hosts.inspect}, #{@options.inspect})"
36
+ "Metric::InternetHealth(#{@weight}, #{@hosts.inspect}, #{@options.inspect})"
36
37
  end
37
38
  end
38
39
  end
@@ -24,6 +24,10 @@ module LitmusPaper
24
24
  @checks << metric_class.new(options[:weight])
25
25
  end
26
26
 
27
+ def measure_health_with_args(metric_class, *args)
28
+ @checks << metric_class.new(*args)
29
+ end
30
+
27
31
  def depends(dependency_class, *args)
28
32
  @dependencies << dependency_class.new(*args)
29
33
  end
@@ -1,3 +1,3 @@
1
1
  module LitmusPaper
2
- VERSION = "0.9.2"
2
+ VERSION = "0.9.3"
3
3
  end
@@ -20,12 +20,12 @@ describe LitmusPaper::Dependency::TCP do
20
20
  end
21
21
 
22
22
  it "returns 100 when it's able to reach a single host" do
23
- internet_health = LitmusPaper::Metric::InternetHealth.new(["127.0.0.1:3000"])
23
+ internet_health = LitmusPaper::Metric::InternetHealth.new(100, ["127.0.0.1:3000"])
24
24
  internet_health.current_health.should == 100
25
25
  end
26
26
 
27
27
  it "returns 0 when it's unable to reach a single host" do
28
- internet_health = LitmusPaper::Metric::InternetHealth.new(["127.0.0.1:6000"])
28
+ internet_health = LitmusPaper::Metric::InternetHealth.new(100, ["127.0.0.1:6000"])
29
29
  internet_health.current_health.should == 0
30
30
  end
31
31
 
@@ -34,6 +34,7 @@ describe LitmusPaper::Dependency::TCP do
34
34
  sleep(5)
35
35
  end
36
36
  internet_health = LitmusPaper::Metric::InternetHealth.new(
37
+ 100,
37
38
  ["127.0.0.1:6000"],
38
39
  { :timeout_seconds => 2 },
39
40
  )
@@ -41,23 +42,29 @@ describe LitmusPaper::Dependency::TCP do
41
42
  end
42
43
 
43
44
  it "returns 100 when it's able to reach multiple hosts" do
44
- internet_health = LitmusPaper::Metric::InternetHealth.new([
45
- "127.0.0.1:3000",
46
- "127.0.0.1:3001",
47
- "127.0.0.1:3002",
48
- "127.0.0.1:3003",
49
- "127.0.0.1:3004",
50
- ])
45
+ internet_health = LitmusPaper::Metric::InternetHealth.new(
46
+ 100,
47
+ [
48
+ "127.0.0.1:3000",
49
+ "127.0.0.1:3001",
50
+ "127.0.0.1:3002",
51
+ "127.0.0.1:3003",
52
+ "127.0.0.1:3004",
53
+ ],
54
+ )
51
55
  internet_health.current_health.should == 100
52
56
  end
53
57
 
54
58
  it "returns 50 when it's unable to reach half the hosts" do
55
- internet_health = LitmusPaper::Metric::InternetHealth.new([
56
- "127.0.0.1:3000",
57
- "127.0.0.1:3001",
58
- "127.0.0.1:6002",
59
- "127.0.0.1:6003",
60
- ])
59
+ internet_health = LitmusPaper::Metric::InternetHealth.new(
60
+ 100,
61
+ [
62
+ "127.0.0.1:3000",
63
+ "127.0.0.1:3001",
64
+ "127.0.0.1:6002",
65
+ "127.0.0.1:6003",
66
+ ],
67
+ )
61
68
  internet_health.current_health.should == 50
62
69
  end
63
70
 
@@ -67,6 +74,7 @@ describe LitmusPaper::Dependency::TCP do
67
74
  sleep(5)
68
75
  end
69
76
  internet_health = LitmusPaper::Metric::InternetHealth.new(
77
+ 100,
70
78
  ["127.0.0.1:3000", "127.0.0.1:3001"],
71
79
  { :timeout_seconds => 2 },
72
80
  )
@@ -74,18 +82,21 @@ describe LitmusPaper::Dependency::TCP do
74
82
  end
75
83
 
76
84
  it "returns 0 when it's unable to reach any of the hosts" do
77
- internet_health = LitmusPaper::Metric::InternetHealth.new([
78
- "127.0.0.1:6000",
79
- "127.0.0.1:6001",
80
- "127.0.0.1:6002",
81
- "127.0.0.1:6003",
82
- "127.0.0.1:6004",
83
- ])
85
+ internet_health = LitmusPaper::Metric::InternetHealth.new(
86
+ 100,
87
+ [
88
+ "127.0.0.1:6000",
89
+ "127.0.0.1:6001",
90
+ "127.0.0.1:6002",
91
+ "127.0.0.1:6003",
92
+ "127.0.0.1:6004",
93
+ ],
94
+ )
84
95
  internet_health.current_health.should == 0
85
96
  end
86
97
 
87
98
  it "logs exceptions and returns 0" do
88
- internet_health = LitmusPaper::Metric::InternetHealth.new(["127.0.0.1:6000"])
99
+ internet_health = LitmusPaper::Metric::InternetHealth.new(100, ["127.0.0.1:6000"])
89
100
  LitmusPaper.logger.should_receive(:info)
90
101
  internet_health.current_health.should == 0
91
102
  end
@@ -93,14 +104,17 @@ describe LitmusPaper::Dependency::TCP do
93
104
 
94
105
  describe "to_s" do
95
106
  it "is the name of the class and the lists of hosts" do
96
- internet_health = LitmusPaper::Metric::InternetHealth.new([
97
- "127.0.0.1:6000",
98
- "127.0.0.1:6001",
99
- "127.0.0.1:6002",
100
- "127.0.0.1:6003",
101
- "127.0.0.1:6004",
102
- ])
103
- internet_health.to_s.should == "Metric::InternetHealth([\"127.0.0.1:6000\", \"127.0.0.1:6001\", \"127.0.0.1:6002\", \"127.0.0.1:6003\", \"127.0.0.1:6004\"], {})"
107
+ internet_health = LitmusPaper::Metric::InternetHealth.new(
108
+ 100,
109
+ [
110
+ "127.0.0.1:6000",
111
+ "127.0.0.1:6001",
112
+ "127.0.0.1:6002",
113
+ "127.0.0.1:6003",
114
+ "127.0.0.1:6004",
115
+ ],
116
+ )
117
+ internet_health.to_s.should == "Metric::InternetHealth(100, [\"127.0.0.1:6000\", \"127.0.0.1:6001\", \"127.0.0.1:6002\", \"127.0.0.1:6003\", \"127.0.0.1:6004\"], {})"
104
118
  end
105
119
  end
106
120
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litmus_paper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: