litmus_paper 0.9.1 → 0.9.2

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.
data/.travis.yml CHANGED
@@ -3,6 +3,7 @@ rvm:
3
3
  - 1.9.2
4
4
  - 1.9.3
5
5
  - 2.0.0
6
+ sudo: false
6
7
  notifications:
7
8
  email:
8
- - blue@getbraintree.com
9
+ - blue-ci-notifications@getbraintree.com
@@ -19,7 +19,7 @@ module LitmusPaper
19
19
  def memory_total
20
20
  return @memory_total unless @memory_total.nil?
21
21
 
22
- size, scale = @facter.value('memorytotal').split(' ')
22
+ size, scale = @facter.value('memorysize').split(' ')
23
23
  @memory_total = (size.to_f * MULTIPLIER[scale]).to_i
24
24
  end
25
25
 
@@ -0,0 +1,39 @@
1
+ module LitmusPaper
2
+ module Metric
3
+ class InternetHealth
4
+ def initialize(hosts, options = {})
5
+ @hosts = hosts
6
+ @options = options
7
+ @timeout = options.fetch(:timeout_seconds, 5)
8
+ end
9
+
10
+ def tcp_connect?(host, port)
11
+ Timeout.timeout(@timeout) do
12
+ socket = TCPSocket.new(host, port)
13
+ socket.close
14
+ end
15
+ true
16
+ rescue Timeout::Error
17
+ LitmusPaper.logger.info("Timeout connecting to #{host}:#{port}")
18
+ false
19
+ rescue => e
20
+ LitmusPaper.logger.info("TCP connect to #{host}:#{port} failed with #{e.message}")
21
+ false
22
+ end
23
+
24
+ def current_health
25
+ health = 100 * @hosts.reduce(Rational(0)) do |memo, host|
26
+ if tcp_connect?(*host.split(':'))
27
+ memo += Rational(1) / Rational(@hosts.length)
28
+ end
29
+ memo
30
+ end
31
+ health.to_i
32
+ end
33
+
34
+ def to_s
35
+ "Metric::InternetHealth(#{@hosts.inspect}, #{@options.inspect})"
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module LitmusPaper
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
data/lib/litmus_paper.rb CHANGED
@@ -37,6 +37,7 @@ require 'litmus_paper/metric/available_memory'
37
37
  require 'litmus_paper/metric/big_brother_service'
38
38
  require 'litmus_paper/metric/constant_metric'
39
39
  require 'litmus_paper/metric/cpu_load'
40
+ require 'litmus_paper/metric/internet_health'
40
41
  require 'litmus_paper/metric/script'
41
42
  require 'litmus_paper/service'
42
43
  require 'litmus_paper/status_file'
@@ -3,13 +3,13 @@ require 'spec_helper'
3
3
  describe LitmusPaper::Metric::AvailableMemory do
4
4
  describe "#current_health" do
5
5
  it "multiplies weight by memory available" do
6
- facter = StubFacter.new({"memorytotal" => "10 GB", "memoryfree" => "5 GB"})
6
+ facter = StubFacter.new({"memorysize" => "10 GB", "memoryfree" => "5 GB"})
7
7
  memory = LitmusPaper::Metric::AvailableMemory.new(50, facter)
8
8
  memory.current_health.should == 25
9
9
  end
10
10
 
11
11
  it "multiplies weight by memory available when handling floating point values" do
12
- facter = StubFacter.new({"memorytotal" => "2.0 GB", "memoryfree" => "1.8 GB"})
12
+ facter = StubFacter.new({"memorysize" => "2.0 GB", "memoryfree" => "1.8 GB"})
13
13
  memory = LitmusPaper::Metric::AvailableMemory.new(50, facter)
14
14
  memory.current_health.should == 44
15
15
  end
@@ -21,7 +21,7 @@ describe LitmusPaper::Metric::AvailableMemory do
21
21
  end
22
22
 
23
23
  it "handles floating point values properly" do
24
- facter = StubFacter.new("memorytotal" => "1.80 GB")
24
+ facter = StubFacter.new("memorysize" => "1.80 GB")
25
25
  memory = LitmusPaper::Metric::AvailableMemory.new(50, facter)
26
26
  memory.memory_total.should == 1932735283
27
27
  end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ describe LitmusPaper::Dependency::TCP do
4
+
5
+ describe "#current_health" do
6
+ before(:all) do
7
+ @server_3000 = TCPServer.new 3000
8
+ @server_3001 = TCPServer.new 3001
9
+ @server_3002 = TCPServer.new 3002
10
+ @server_3003 = TCPServer.new 3003
11
+ @server_3004 = TCPServer.new 3004
12
+ end
13
+
14
+ after(:all) do
15
+ @server_3000.close
16
+ @server_3001.close
17
+ @server_3002.close
18
+ @server_3003.close
19
+ @server_3004.close
20
+ end
21
+
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"])
24
+ internet_health.current_health.should == 100
25
+ end
26
+
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"])
29
+ internet_health.current_health.should == 0
30
+ end
31
+
32
+ it "returns 0 when it's request to a single host times out" do
33
+ TCPSocket.stub(:new) do
34
+ sleep(5)
35
+ end
36
+ internet_health = LitmusPaper::Metric::InternetHealth.new(
37
+ ["127.0.0.1:6000"],
38
+ { :timeout_seconds => 2 },
39
+ )
40
+ internet_health.current_health.should == 0
41
+ end
42
+
43
+ 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
+ ])
51
+ internet_health.current_health.should == 100
52
+ end
53
+
54
+ 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
+ ])
61
+ internet_health.current_health.should == 50
62
+ end
63
+
64
+ it "returns 50 when it's request to a single host out of two hosts times out" do
65
+ TCPSocket.stub(:new) do
66
+ TCPSocket.unstub(:new)
67
+ sleep(5)
68
+ end
69
+ internet_health = LitmusPaper::Metric::InternetHealth.new(
70
+ ["127.0.0.1:3000", "127.0.0.1:3001"],
71
+ { :timeout_seconds => 2 },
72
+ )
73
+ internet_health.current_health.should == 50
74
+ end
75
+
76
+ 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
+ ])
84
+ internet_health.current_health.should == 0
85
+ end
86
+
87
+ it "logs exceptions and returns 0" do
88
+ internet_health = LitmusPaper::Metric::InternetHealth.new(["127.0.0.1:6000"])
89
+ LitmusPaper.logger.should_receive(:info)
90
+ internet_health.current_health.should == 0
91
+ end
92
+ end
93
+
94
+ describe "to_s" do
95
+ 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\"], {})"
104
+ end
105
+ end
106
+
107
+ end
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.1
4
+ version: 0.9.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-12-08 00:00:00.000000000 Z
12
+ date: 2017-03-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
@@ -213,6 +213,7 @@ files:
213
213
  - lib/litmus_paper/metric/big_brother_service.rb
214
214
  - lib/litmus_paper/metric/constant_metric.rb
215
215
  - lib/litmus_paper/metric/cpu_load.rb
216
+ - lib/litmus_paper/metric/internet_health.rb
216
217
  - lib/litmus_paper/metric/script.rb
217
218
  - lib/litmus_paper/service.rb
218
219
  - lib/litmus_paper/status_file.rb
@@ -232,6 +233,7 @@ files:
232
233
  - spec/litmus_paper/metric/big_brother_service_spec.rb
233
234
  - spec/litmus_paper/metric/constant_metric_spec.rb
234
235
  - spec/litmus_paper/metric/cpu_load_spec.rb
236
+ - spec/litmus_paper/metric/internet_health_spec.rb
235
237
  - spec/litmus_paper/metric/script_spec.rb
236
238
  - spec/litmus_paper/service_spec.rb
237
239
  - spec/litmus_paper/status_file_spec.rb
@@ -291,6 +293,7 @@ test_files:
291
293
  - spec/litmus_paper/metric/big_brother_service_spec.rb
292
294
  - spec/litmus_paper/metric/constant_metric_spec.rb
293
295
  - spec/litmus_paper/metric/cpu_load_spec.rb
296
+ - spec/litmus_paper/metric/internet_health_spec.rb
294
297
  - spec/litmus_paper/metric/script_spec.rb
295
298
  - spec/litmus_paper/service_spec.rb
296
299
  - spec/litmus_paper/status_file_spec.rb