shamrock 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shamrock.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Justin S. Leitgeb
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,49 @@
1
+ # Shamrock
2
+
3
+ Makes setting up stub services easy.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'shamrock'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install shamrock
18
+
19
+ ## Usage
20
+
21
+ You can then easily start the stub service either for the spec examples that need it, or
22
+ globally in your spec_helper file. You can replace the Proc below with any Rack application.
23
+ In some cases, we use Sinatra for stub services.
24
+
25
+ my_rack_app = proc {|env| [200, {"Content-Type" => "text/html"}, "Hello Rack!"]}
26
+ @service = Shamrock::Service.new(my_rack_app)
27
+ @service.start
28
+
29
+ After you are done with the service:
30
+
31
+ @service.stop
32
+
33
+ ## Credits
34
+
35
+ Thanks to Wojciech Mach (http://github.com/wojtekmach) who helped to implement something like
36
+ this for a stub service on a project for Stack Builders.
37
+
38
+ Thanks to Mike Barinek for writing up an example of this code that used Threads for starting
39
+ the rack application - this gem draws from code in his blog post at
40
+ http://pivotallabs.com/users/mbarinek/blog/articles/2043-testing-ruby-services-without-mocks
41
+
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,43 @@
1
+ require 'net/http'
2
+ require 'timeout'
3
+
4
+ module Shamrock
5
+ class Monitor
6
+
7
+ DEFAULT_TIMEOUT = 10
8
+ RE_CHECK_WAIT = 0.1
9
+
10
+ def initialize(url)
11
+ @uri = URI.parse(url)
12
+ end
13
+
14
+ def wait_until_ready
15
+ Timeout::timeout(DEFAULT_TIMEOUT) do
16
+ until server_started? do
17
+ sleep RE_CHECK_WAIT
18
+ end
19
+ end
20
+
21
+ puts "Started server #{server_name} on #{port.number}."
22
+
23
+ rescue Timeout::Error => error
24
+ abort "Server never started!"
25
+ end
26
+
27
+ def ready?
28
+ begin
29
+ response = Net::HTTP.get_response(@uri)
30
+ response.is_a?(Net::HTTPSuccess)
31
+ rescue SystemCallError => error
32
+ false
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def server_name
39
+ @rack_app.class.name
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ module Shamrock
2
+ class Port
3
+ attr_reader :number
4
+
5
+ def initialize
6
+ @number = Random.rand(9220...9239)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ module Shamrock
2
+ class Service
3
+ attr_reader :port, :url
4
+
5
+ def initialize(rack_app,
6
+ handler = Rack::Handler::WEBrick,
7
+ monitor = Shamrock::Monitor)
8
+
9
+ @rack_app = rack_app
10
+ @handler = handler
11
+ @port = Port.new
12
+ @url = "http://localhost:#{port.number}"
13
+ @monitor = monitor.new(url)
14
+ end
15
+
16
+ def start
17
+ @thread = Thread.new do
18
+ @handler.run(@rack_app, Port: port.number)
19
+ end
20
+
21
+ @monitor.wait_until_ready
22
+ end
23
+
24
+ def stop
25
+ Thread.kill(@thread)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module Shamrock
2
+ VERSION = "0.0.1"
3
+ end
data/lib/shamrock.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'rack'
2
+
3
+ require 'shamrock/version'
4
+ require 'shamrock/port'
5
+ require 'shamrock/service'
6
+ require 'shamrock/monitor'
data/shamrock.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/shamrock/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Justin S. Leitgeb"]
6
+ gem.email = ["justin@stackbuilders.com"]
7
+ gem.summary = "Facilitates setting up stub services in your application"
8
+ gem.description = "Shamrock helps you to use stub services in your application by starting a rack service in a Thread"
9
+
10
+ gem.homepage = "http://github.com/jsl/shamrock"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "shamrock"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Shamrock::VERSION
18
+
19
+ gem.add_dependency('rack')
20
+
21
+ gem.add_development_dependency 'rake', '~> 0.9.2.2'
22
+ gem.add_development_dependency 'rspec', '~> 2.10.0'
23
+ gem.add_development_dependency 'simplecov', '~> 0.6.4'
24
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shamrock::Port do
4
+ describe "initialization" do
5
+ it "should set a number" do
6
+ subject.number.should be_a(Integer)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shamrock::Service do
4
+ before do
5
+ @rack_app = proc {|env| [200, {"Content-Type" => "text/html"}, "Hello Rack!"]}
6
+ @monitor = mock('monitor', wait_until_ready: true)
7
+ @monitor_class = mock('monitor class', new: @monitor)
8
+ end
9
+
10
+ describe "#start" do
11
+ it "should start the server" do
12
+ handler = mock('handler')
13
+ service = Shamrock::Service.new(@rack_app, handler, @monitor_class)
14
+ handler.should_receive(:run).with(@rack_app, Port: service.port.number)
15
+ service.start
16
+ end
17
+
18
+ it "should wait for the server to become available" do
19
+ @monitor.should_receive(:wait_until_ready).once
20
+ handler = mock('handler', run: true)
21
+ Shamrock::Service.new(@rack_app, handler, @monitor_class).start
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+
6
+ require 'shamrock'
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shamrock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin S. Leitgeb
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.9.2.2
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.2.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.10.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.10.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.6.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.6.4
78
+ description: Shamrock helps you to use stub services in your application by starting
79
+ a rack service in a Thread
80
+ email:
81
+ - justin@stackbuilders.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - lib/shamrock.rb
92
+ - lib/shamrock/monitor.rb
93
+ - lib/shamrock/port.rb
94
+ - lib/shamrock/service.rb
95
+ - lib/shamrock/version.rb
96
+ - shamrock.gemspec
97
+ - spec/shamrock/port_spec.rb
98
+ - spec/shamrock/service_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: http://github.com/jsl/shamrock
101
+ licenses: []
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.24
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Facilitates setting up stub services in your application
124
+ test_files:
125
+ - spec/shamrock/port_spec.rb
126
+ - spec/shamrock/service_spec.rb
127
+ - spec/spec_helper.rb