test-redis 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +10 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +5 -0
- data/lib/test-redis.rb +154 -0
- data/lib/test-redis/version.rb +5 -0
- data/test-redis.gemspec +26 -0
- data/test/test_test-redis.rb +69 -0
- metadata +109 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 miyucy
|
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,29 @@
|
|
1
|
+
# Test::Redis
|
2
|
+
|
3
|
+
redis-server runner for tests.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'test-redis'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install test-redis
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/test-redis.rb
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
require "test-redis/version"
|
2
|
+
require "fileutils"
|
3
|
+
require "tmpdir"
|
4
|
+
require "mkmf"
|
5
|
+
require "socket"
|
6
|
+
|
7
|
+
module Test
|
8
|
+
class Redis
|
9
|
+
def self.auto_start
|
10
|
+
@@auto_start ||= false
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.auto_start=(value)
|
14
|
+
@@auto_start = !!value
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(options={})
|
18
|
+
setup options
|
19
|
+
start if !!options[:auto_start] || self.class.auto_start
|
20
|
+
end
|
21
|
+
|
22
|
+
def start
|
23
|
+
return if @pid
|
24
|
+
write_conf conf
|
25
|
+
fork_redis
|
26
|
+
at_exit { stop }
|
27
|
+
wait_redis
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def stop(signal=nil)
|
32
|
+
return unless @pid
|
33
|
+
|
34
|
+
if File.exist? conf["pidfile"]
|
35
|
+
realpid = File.read(conf["pidfile"]).strip.to_i
|
36
|
+
kill realpid, signal
|
37
|
+
FileUtils.rm_f conf["pidfile"]
|
38
|
+
end
|
39
|
+
|
40
|
+
kill @pid, signal
|
41
|
+
|
42
|
+
@pid = nil
|
43
|
+
end
|
44
|
+
|
45
|
+
def restart
|
46
|
+
stop
|
47
|
+
start
|
48
|
+
end
|
49
|
+
|
50
|
+
def info
|
51
|
+
read_info
|
52
|
+
end
|
53
|
+
|
54
|
+
attr_reader :base_dir, :conf, :redis, :pid
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def read_info
|
59
|
+
info = nil
|
60
|
+
sock = TCPSocket.open("127.0.0.1", conf["port"])
|
61
|
+
begin
|
62
|
+
sock.write "INFO\r\n"
|
63
|
+
size = sock.gets("\r\n")[1..-3].to_i
|
64
|
+
info = sock.read(size + 2)
|
65
|
+
sock.write "QUIT\r\n"
|
66
|
+
ensure
|
67
|
+
sock.close
|
68
|
+
end
|
69
|
+
return nil unless info
|
70
|
+
info.each_line("\r\n").each_with_object({}) do |line, result|
|
71
|
+
key, value = line.chomp.split ":"
|
72
|
+
next if key.nil?
|
73
|
+
result[key] = value
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def write_conf(conf)
|
78
|
+
File.open(base_dir + "/redis.conf", "w") do |f|
|
79
|
+
conf.each do |key, val|
|
80
|
+
f.puts "#{key} #{val}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def fork_redis
|
86
|
+
redis_log = File.open(base_dir + "/redis-server.log", "a")
|
87
|
+
@pid = fork do
|
88
|
+
$stdout.reopen redis_log
|
89
|
+
$stderr.reopen redis_log
|
90
|
+
exec %[#{redis} "#{base_dir}/redis.conf"]
|
91
|
+
end
|
92
|
+
exit unless @pid
|
93
|
+
redis_log.close
|
94
|
+
end
|
95
|
+
|
96
|
+
def wait_redis
|
97
|
+
output = nil
|
98
|
+
begin
|
99
|
+
while !File.exist? conf["pidfile"]
|
100
|
+
if Process.waitpid pid, Process::WNOHANG
|
101
|
+
output = File.read base_dir + "/redis-server.log"
|
102
|
+
output+= File.read conf["logfile"]
|
103
|
+
end
|
104
|
+
sleep 0.1
|
105
|
+
end
|
106
|
+
rescue
|
107
|
+
raise "redis-server failed " + (output ||= "")
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def kill(pid, signal)
|
112
|
+
Process.kill Signal.list[signal || "TERM"], pid rescue nil
|
113
|
+
Process.waitpid pid rescue nil
|
114
|
+
end
|
115
|
+
|
116
|
+
def setup(options)
|
117
|
+
@base_dir = options[:base_dir] || default_base_dir
|
118
|
+
@redis = options[:redis] || find_redis
|
119
|
+
@conf = default_conf.merge(options[:conf] || {})
|
120
|
+
end
|
121
|
+
|
122
|
+
def default_conf
|
123
|
+
{
|
124
|
+
"daemonize" => "yes",
|
125
|
+
"databases" => 16,
|
126
|
+
"dbfilename" => "dump.rdb",
|
127
|
+
"dir" => base_dir,
|
128
|
+
"logfile" => base_dir + "/redis.log",
|
129
|
+
"loglevel" => "debug",
|
130
|
+
"pidfile" => base_dir + "/redis.pid",
|
131
|
+
"port" => 16379,
|
132
|
+
"rdbcompression" => "no",
|
133
|
+
"timeout" => 0,
|
134
|
+
"unixsocket" => base_dir + "/redis.sock",
|
135
|
+
}
|
136
|
+
end
|
137
|
+
|
138
|
+
def default_base_dir
|
139
|
+
Dir.mktmpdir.tap { |dir|
|
140
|
+
at_exit { FileUtils.remove_entry_secure dir if FileTest.directory? dir }
|
141
|
+
}
|
142
|
+
end
|
143
|
+
|
144
|
+
def find_redis
|
145
|
+
suppress_logging
|
146
|
+
find_executable "redis-server"
|
147
|
+
end
|
148
|
+
|
149
|
+
def suppress_logging
|
150
|
+
Logging.quiet = true
|
151
|
+
Logging.logfile base_dir + "/mkmf.log"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
data/test-redis.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/test-redis/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["miyucy"]
|
6
|
+
gem.email = ["miyucy@gmail.com"]
|
7
|
+
gem.description = %q{redis-server runner for tests.}
|
8
|
+
gem.summary = %q{redis-server runner for tests.}
|
9
|
+
gem.homepage = "http://github.com/miyucy/test-redis"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "test-redis"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Test::Redis::VERSION
|
17
|
+
|
18
|
+
if gem.respond_to? :specification_version
|
19
|
+
gem.specification_version = 3
|
20
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0')
|
21
|
+
gem.add_development_dependency(%q<rake>, [">= 0"])
|
22
|
+
gem.add_development_dependency(%q<redis>, [">= 0"])
|
23
|
+
gem.add_development_dependency(%q<minitest>, [">= 0"])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
gem "minitest"
|
2
|
+
require "minitest/autorun"
|
3
|
+
require "redis"
|
4
|
+
require "test-redis"
|
5
|
+
|
6
|
+
class TestTestRedis < MiniTest::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@server ||= Test::Redis.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
server.stop
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_start
|
16
|
+
assert_nil server.pid
|
17
|
+
server.start
|
18
|
+
refute_nil server.pid
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_ping_with_unixsocket
|
22
|
+
server.start
|
23
|
+
client = Redis.new :path => server.conf["unixsocket"]
|
24
|
+
assert_equal "PONG", client.ping
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_ping_with_port
|
28
|
+
server.start
|
29
|
+
client = Redis.new :port => server.conf["port"]
|
30
|
+
assert_equal "PONG", client.ping
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_stop
|
34
|
+
server.start
|
35
|
+
refute_nil server.pid
|
36
|
+
server.stop
|
37
|
+
assert_nil server.pid
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_auto_start_option
|
41
|
+
s = Test::Redis.new :auto_start => true
|
42
|
+
refute_nil s.pid
|
43
|
+
c = Redis.new :path => s.conf["unixsocket"]
|
44
|
+
assert_equal "PONG", c.ping
|
45
|
+
s.stop
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_auto_start
|
49
|
+
prev = Test::Redis.auto_start
|
50
|
+
|
51
|
+
Test::Redis.auto_start = true
|
52
|
+
|
53
|
+
s = Test::Redis.new
|
54
|
+
refute_nil s.pid
|
55
|
+
c = Redis.new :path => s.conf["unixsocket"]
|
56
|
+
assert_equal "PONG", c.ping
|
57
|
+
s.stop
|
58
|
+
|
59
|
+
Test::Redis.auto_start = prev
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_change_conf
|
63
|
+
s = Test::Redis.new :conf => { "unknown_key" => "unknown_value" }
|
64
|
+
e = assert_raises(RuntimeError) { s.start }
|
65
|
+
assert_match "FATAL CONFIG FILE ERROR", e.message
|
66
|
+
end
|
67
|
+
|
68
|
+
attr_reader :server
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: test-redis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- miyucy
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-09 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: redis
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
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'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: minitest
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '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: '0'
|
62
|
+
description: redis-server runner for tests.
|
63
|
+
email:
|
64
|
+
- miyucy@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/test-redis.rb
|
75
|
+
- lib/test-redis/version.rb
|
76
|
+
- test-redis.gemspec
|
77
|
+
- test/test_test-redis.rb
|
78
|
+
homepage: http://github.com/miyucy/test-redis
|
79
|
+
licenses: []
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
hash: 4510710619810826475
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
hash: 4510710619810826475
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.8.24
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: redis-server runner for tests.
|
108
|
+
test_files:
|
109
|
+
- test/test_test-redis.rb
|