doberman 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5acc2f3a8906a894e18baa3b6cf0b30116b3abe
4
+ data.tar.gz: 27a84df52fe8ab3234c003bcd897f1bed8aeda89
5
+ SHA512:
6
+ metadata.gz: 0fc049b31d2523ece233026b60a851269dbb1d20f1be5f5ad156721f1b81dec02a37aa8ca48aad8e913049b2e226919163879b347260921c4a28ae3857d12e62
7
+ data.tar.gz: e9e5936011a153a1f5432bb7fea6662b292823a217d9c772a5461cb92fa43af01a00547085a44d8309bab8541b219517aac58f160e464bf30707d8675614984a
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format documentation
3
+ --profile
4
+ --tty
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ midi_lyrics
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.1.1
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - ruby-head
8
+ - jruby-19mode
9
+ - jruby-head
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in doberman.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'simplecov', :require => false
8
+ gem 'coveralls', :require => false
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mateus Del Bianco
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Doberman
2
+
3
+ Doberman is your favorite watchdog timer. It makes sure your code is still alive, barking out loud if there's silence for too long.
4
+
5
+ [![Build Status](https://travis-ci.org/mateusdelbianco/doberman.png)](https://travis-ci.org/mateusdelbianco/doberman)
6
+ [![Code Climate](https://codeclimate.com/github/mateusdelbianco/doberman.png)](https://codeclimate.com/github/mateusdelbianco/doberman)
7
+ [![Coverage Status](https://coveralls.io/repos/mateusdelbianco/doberman/badge.png?branch=master)](https://coveralls.io/r/mateusdelbianco/doberman?branch=master)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'doberman'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install doberman
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ begin
27
+ watchdog = Doberman::WatchDog.new(:timeout => 3)
28
+ watchdog.start
29
+ sleep 2
30
+ watchdog.ping
31
+ sleep 2
32
+ watchdog.ping
33
+ sleep 2
34
+ watchdog.stop
35
+ puts "yay! no barking!"
36
+ rescue Doberman::WatchDog::Timeout => e
37
+ puts "woof woof!"
38
+ end
39
+ # => yay! no barking!
40
+ ```
41
+
42
+ ```ruby
43
+ begin
44
+ watchdog = Doberman::WatchDog.new(:timeout => 3)
45
+ watchdog.start
46
+ watchdog.ping
47
+ sleep 4
48
+ watchdog.stop
49
+ puts "yay! no barking!"
50
+ rescue Doberman::WatchDog::Timeout => e
51
+ puts "woof woof!"
52
+ end
53
+ # => woof woof!
54
+ ```
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it ( http://github.com/mateusdelbianco/doberman/fork )
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/doberman.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'doberman/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "doberman"
8
+ spec.version = Doberman::VERSION
9
+ spec.authors = ["Mateus Del Bianco"]
10
+ spec.email = ["mateus@delbianco.net.br"]
11
+ spec.summary = %q{Doberman is your favorite watchdog timer.}
12
+ spec.description = %q{Doberman makes sure your code is still alive, barking out loud if there's silence for too long.}
13
+ spec.homepage = "https://github.com/mateusdelbianco/doberman"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "debugger"
25
+ end
data/lib/doberman.rb ADDED
@@ -0,0 +1,63 @@
1
+ require "doberman/version"
2
+
3
+ module Doberman
4
+ class WatchDog
5
+ class Timeout < StandardError; end
6
+
7
+ attr_reader :error_message, :timeout
8
+
9
+ def initialize(options = {})
10
+ @error_message = options[:error_message] || "WatchDog timeout reached"
11
+ @timeout = options[:timeout] || 60
12
+ @resolution = options[:resolution] || 0.1
13
+ @current = Thread.current
14
+ end
15
+
16
+ def start
17
+ if @watchdog.nil?
18
+ reset_ttl
19
+ create_thread
20
+ end
21
+ end
22
+
23
+ def ping
24
+ if @watchdog.nil?
25
+ start
26
+ else
27
+ @reset = true
28
+ end
29
+ end
30
+
31
+ def stop
32
+ kill_thread
33
+ end
34
+
35
+ private
36
+
37
+ def create_thread
38
+ @watchdog = Thread.new do
39
+ while @ttl > 0
40
+ sleep @resolution
41
+ if @reset
42
+ reset_ttl
43
+ else
44
+ @ttl -= @resolution
45
+ end
46
+ end
47
+ @current.raise Timeout.new(@error_message)
48
+ end
49
+ end
50
+
51
+ def reset_ttl
52
+ @ttl = timeout.to_f
53
+ @reset = false
54
+ end
55
+
56
+ def kill_thread
57
+ unless @watchdog.nil?
58
+ @watchdog.kill
59
+ @watchdog = nil
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module Doberman
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe Doberman::WatchDog do
4
+ describe "#initialize" do
5
+ it "should set a custom error_message" do
6
+ w = Doberman::WatchDog.new(:error_message => "custom_error_message")
7
+ expect(w.error_message).to eq("custom_error_message")
8
+ end
9
+
10
+ it "should set a custom timeout" do
11
+ w = Doberman::WatchDog.new(:timeout => 1234)
12
+ expect(w.timeout).to eq(1234)
13
+ end
14
+ end
15
+
16
+ describe "using the watchdog" do
17
+ context "protection against bad users" do
18
+ before :each do
19
+ @watchdog = Doberman::WatchDog.new
20
+ end
21
+
22
+ after :each do
23
+ @watchdog.stop
24
+ end
25
+
26
+ it "should not raise error if stop is used without start" do
27
+ expect { @watchdog.stop }.not_to raise_error
28
+ end
29
+
30
+ it "should not raise error if stop is used after start" do
31
+ @watchdog.start
32
+ expect { @watchdog.stop }.not_to raise_error
33
+ end
34
+
35
+ it "should not raise error if start is used twice" do
36
+ @watchdog.start
37
+ expect { @watchdog.start }.not_to raise_error
38
+ end
39
+
40
+ it "should not raise error if start is used after stop" do
41
+ @watchdog.start
42
+ @watchdog.stop
43
+ expect { @watchdog.start }.not_to raise_error
44
+ end
45
+
46
+ it "should not raise error if ping is used without start" do
47
+ expect { @watchdog.ping }.not_to raise_error
48
+ end
49
+
50
+ it "should start watchdog if ping is used without start" do
51
+ expect(@watchdog).to receive(:start)
52
+ @watchdog.ping
53
+ end
54
+
55
+ it "should not raise error if ping is used after start" do
56
+ @watchdog.start
57
+ expect { @watchdog.ping }.not_to raise_error
58
+ end
59
+ end
60
+
61
+ context "using the watchdog" do
62
+ it "should raise error if timeout is reached" do
63
+ w = Doberman::WatchDog.new(:timeout => 0.1)
64
+ w.start
65
+ w.ping
66
+ expect { sleep 0.4 }.to raise_error(Doberman::WatchDog::Timeout)
67
+ w.stop
68
+ end
69
+
70
+ it "should not raise error if timeout is not reached" do
71
+ w = Doberman::WatchDog.new(:timeout => 1)
72
+ w.start
73
+ w.ping
74
+ expect { sleep 0.4 }.not_to raise_error
75
+ w.stop
76
+ end
77
+
78
+ it "should not raise error if timeout is reached but ping is used" do
79
+ w = Doberman::WatchDog.new(:timeout => 0.5)
80
+ w.start
81
+ w.ping
82
+ expect do
83
+ 3.times do
84
+ sleep 0.3
85
+ w.ping
86
+ end
87
+ end.not_to raise_error
88
+ w.stop
89
+ end
90
+
91
+ it "should not raise error if timeout is reached after stop" do
92
+ w = Doberman::WatchDog.new(:timeout => 0.1)
93
+ w.start
94
+ w.ping
95
+ w.stop
96
+ expect { sleep 0.4 }.not_to raise_error
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,32 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require "simplecov"
8
+ require "coveralls"
9
+
10
+ class SimpleCov::Formatter::MergedFormatter
11
+ def format(result)
12
+ SimpleCov::Formatter::HTMLFormatter.new.format(result)
13
+ Coveralls::SimpleCov::Formatter.new.format(result)
14
+ end
15
+ end
16
+ SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
17
+ SimpleCov.start do
18
+ add_filter "spec"
19
+ end
20
+
21
+ require "doberman"
22
+
23
+ RSpec.configure do |config|
24
+ config.run_all_when_everything_filtered = true
25
+ config.filter_run :focus
26
+
27
+ # Run specs in random order to surface order dependencies. If you find an
28
+ # order dependency and want to debug it, you can fix the order by providing
29
+ # the seed, which is printed after each run.
30
+ # --seed 1234
31
+ config.order = 'random'
32
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doberman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mateus Del Bianco
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: debugger
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Doberman makes sure your code is still alive, barking out loud if there's
70
+ silence for too long.
71
+ email:
72
+ - mateus@delbianco.net.br
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".ruby-gemset"
80
+ - ".ruby-version"
81
+ - ".travis.yml"
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - doberman.gemspec
87
+ - lib/doberman.rb
88
+ - lib/doberman/version.rb
89
+ - spec/doberman_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/mateusdelbianco/doberman
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Doberman is your favorite watchdog timer.
115
+ test_files:
116
+ - spec/doberman_spec.rb
117
+ - spec/spec_helper.rb