glint 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.
@@ -0,0 +1,18 @@
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
18
+ vendor/
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 1.9.2
6
+ - ruby-head
7
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in glint.gemspec
4
+ gemspec
@@ -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.
@@ -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
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ spec.ruby_opts = %w[-w]
8
+ end
9
+
10
+ task :default => :spec
@@ -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
@@ -0,0 +1,3 @@
1
+ require "glint/version"
2
+ require "glint/server"
3
+ require "glint/util"
@@ -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
@@ -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
@@ -0,0 +1,3 @@
1
+ module Glint
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'socket'
4
+
5
+ server = TCPServer.new(ARGV[0])
6
+ server.accept
@@ -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
@@ -0,0 +1,13 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ module Glint
4
+ describe Util do
5
+ describe '.empty_port' do
6
+ let(:port) { Glint::Util.empty_port }
7
+
8
+ it {
9
+ expect(port).to be_an_instance_of(Fixnum)
10
+ }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ require_relative '../lib/glint'
2
+
3
+ RSpec.configure do |config|
4
+ end
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