glint 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +49 -0
- data/Rakefile +10 -0
- data/glint.gemspec +25 -0
- data/lib/glint.rb +3 -0
- data/lib/glint/server.rb +36 -0
- data/lib/glint/util.rb +31 -0
- data/lib/glint/version.rb +3 -0
- data/spec/bin/server.rb +6 -0
- data/spec/lib/glint/server_spec.rb +77 -0
- data/spec/lib/glint/util_spec.rb +13 -0
- data/spec/spec_helper.rb +4 -0
- metadata +101 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kentaro Kuribayashi
|
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
|
+
# Glint [![BuildStatus](https://secure.travis-ci.org/kentaro/glint.png)](http://travis-ci.org/kentaro/glint)
|
2
|
+
|
3
|
+
Glint is a library which allows you to fire arbitrary TCP server processes programatically and ensures the processes are shutdown when your code exit.
|
4
|
+
|
5
|
+
It's useful when you want to test your code against real TCP server processes.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
server = Glint::Server.new do |port|
|
11
|
+
# Execute target server process
|
12
|
+
exec command, args
|
13
|
+
end
|
14
|
+
server.start
|
15
|
+
|
16
|
+
# Test your code against the server process
|
17
|
+
client = MyClient.new('127.0.0.1', server.port)
|
18
|
+
client.do_something
|
19
|
+
|
20
|
+
exit
|
21
|
+
# The process executed above will be shutdown here.
|
22
|
+
```
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
Add this line to your application's Gemfile:
|
27
|
+
|
28
|
+
gem 'glint'
|
29
|
+
|
30
|
+
And then execute:
|
31
|
+
|
32
|
+
$ bundle
|
33
|
+
|
34
|
+
Or install it yourself as:
|
35
|
+
|
36
|
+
$ gem install glint
|
37
|
+
|
38
|
+
## See Also
|
39
|
+
|
40
|
+
* [Test::TCP](https://metacpan.org/module/Test::TCP)
|
41
|
+
* This library is a port of Test::TCP for Perl.
|
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 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/glint.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 'glint/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "glint"
|
8
|
+
spec.version = Glint::VERSION
|
9
|
+
spec.authors = ["Kentaro Kuribayashi"]
|
10
|
+
spec.email = ["kentarok@gmail.com"]
|
11
|
+
spec.description = %q{Glint is a library which allows you to fire arbitrary server processes programatically and ensures the processes are shutdown when your code exit}
|
12
|
+
spec.summary = %q{Firebombs arbitrary server processs for tests.}
|
13
|
+
spec.homepage = "http://github.com/kentaro/glint"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.required_ruby_version = '>= 1.9.2'
|
17
|
+
|
18
|
+
spec.files = `git ls-files`.split($/)
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec", "~> 2.12"
|
25
|
+
end
|
data/lib/glint.rb
ADDED
data/lib/glint/server.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative 'util.rb'
|
2
|
+
|
3
|
+
module Glint
|
4
|
+
class Server
|
5
|
+
attr_accessor :port, :block, :pid, :child_pid
|
6
|
+
|
7
|
+
def initialize(port = nil, opts = {}, &block)
|
8
|
+
unless block_given?
|
9
|
+
raise ArgumentError.new('block is not given')
|
10
|
+
end
|
11
|
+
|
12
|
+
@port = port || Util.empty_port
|
13
|
+
@block = block
|
14
|
+
@pid = Process.pid
|
15
|
+
|
16
|
+
ObjectSpace.define_finalizer(self) { stop }
|
17
|
+
end
|
18
|
+
|
19
|
+
def start
|
20
|
+
self.child_pid = fork do
|
21
|
+
block.call(port)
|
22
|
+
exit 0
|
23
|
+
end
|
24
|
+
|
25
|
+
Util.wait_port(port)
|
26
|
+
end
|
27
|
+
|
28
|
+
def stop
|
29
|
+
if pid == Process.pid && child_pid
|
30
|
+
Process.kill(:TERM, child_pid)
|
31
|
+
Process.waitpid(child_pid)
|
32
|
+
self.child_pid = nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/glint/util.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'socket'
|
2
|
+
|
3
|
+
module Glint
|
4
|
+
module Util
|
5
|
+
def self.empty_port
|
6
|
+
s = TCPServer.open(0)
|
7
|
+
port = s.addr[1]
|
8
|
+
s.close
|
9
|
+
port
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.check_port(port)
|
13
|
+
begin
|
14
|
+
socket = TCPSocket.open('127.0.0.1', port)
|
15
|
+
socket.close
|
16
|
+
return true
|
17
|
+
rescue Errno::ECONNREFUSED
|
18
|
+
return false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.wait_port(port)
|
23
|
+
100.times do |n|
|
24
|
+
return true if check_port(port)
|
25
|
+
sleep 0.1
|
26
|
+
end
|
27
|
+
|
28
|
+
raise RuntimeError.new("Could not open the port: #{port}")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/bin/server.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
module Glint
|
4
|
+
describe Server do
|
5
|
+
describe '.new' do
|
6
|
+
context 'when block is given' do
|
7
|
+
context 'and a port is passed in' do
|
8
|
+
let(:server) { Glint::Server.new(65535) { 'dummy' } }
|
9
|
+
|
10
|
+
it {
|
11
|
+
expect(server).to be_an_instance_of(Glint::Server)
|
12
|
+
expect(server.port).to be == 65535
|
13
|
+
expect(server.block).to be_an_instance_of(Proc)
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'and a port is not passed in' do
|
18
|
+
let(:server) { Glint::Server.new { 'dummy' } }
|
19
|
+
|
20
|
+
it {
|
21
|
+
expect(server).to be_an_instance_of(Glint::Server)
|
22
|
+
expect(server.port).to be_an_instance_of(Fixnum)
|
23
|
+
expect(server.block).to be_an_instance_of(Proc)
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when block is not given' do
|
29
|
+
context 'and a port is passed in' do
|
30
|
+
it {
|
31
|
+
expect {
|
32
|
+
Glint::Server.new(65535)
|
33
|
+
}.to raise_error(ArgumentError)
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'and a port is not passed in' do
|
38
|
+
it {
|
39
|
+
expect {
|
40
|
+
Glint::Server.new
|
41
|
+
}.to raise_error(ArgumentError)
|
42
|
+
}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#start' do
|
48
|
+
let(:server) {
|
49
|
+
Glint::Server.new do |port|
|
50
|
+
exec File.expand_path("../../../bin/server.rb", __FILE__), port.to_s
|
51
|
+
end
|
52
|
+
}
|
53
|
+
before { server.start }
|
54
|
+
|
55
|
+
it {
|
56
|
+
expect(server.child_pid).to be_true
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#stop' do
|
61
|
+
let(:server) {
|
62
|
+
server = Glint::Server.new do |port|
|
63
|
+
exec File.expand_path("../../../bin/server.rb", __FILE__), port.to_s
|
64
|
+
end
|
65
|
+
server.start
|
66
|
+
server
|
67
|
+
}
|
68
|
+
let(:child_pid) { server.child_pid }
|
69
|
+
|
70
|
+
before { server.stop }
|
71
|
+
|
72
|
+
it {
|
73
|
+
expect(server.child_pid).to be_nil
|
74
|
+
}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: glint
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kentaro Kuribayashi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
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: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.12'
|
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: '2.12'
|
46
|
+
description: Glint is a library which allows you to fire arbitrary server processes
|
47
|
+
programatically and ensures the processes are shutdown when your code exit
|
48
|
+
email:
|
49
|
+
- kentarok@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .travis.yml
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- glint.gemspec
|
61
|
+
- lib/glint.rb
|
62
|
+
- lib/glint/server.rb
|
63
|
+
- lib/glint/util.rb
|
64
|
+
- lib/glint/version.rb
|
65
|
+
- spec/bin/server.rb
|
66
|
+
- spec/lib/glint/server_spec.rb
|
67
|
+
- spec/lib/glint/util_spec.rb
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
homepage: http://github.com/kentaro/glint
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 1.9.2
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
hash: 1775963593609715634
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.8.23
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Firebombs arbitrary server processs for tests.
|
97
|
+
test_files:
|
98
|
+
- spec/bin/server.rb
|
99
|
+
- spec/lib/glint/server_spec.rb
|
100
|
+
- spec/lib/glint/util_spec.rb
|
101
|
+
- spec/spec_helper.rb
|