integration_test_redis 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,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in integration_test_redis.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Licensed under MIT License:
2
+
3
+ Copyright (c) 2011 Brendon Murphy
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
+ IntegrationTestRedis
2
+ ====================
3
+
4
+ Provide a non-persistent [Redis](http://redis.io/) server for use in integration tests.
5
+
6
+ Why?
7
+ ----
8
+
9
+ Many Redis test solutions work by completely mocking out the Redis client
10
+ interface. This is sometimes a good approach, especially if you want to
11
+ write pure unit tests. However, there are times you want to integrate
12
+ with Redis safely for testing purposes.
13
+
14
+ Testing against a running Redis server instance can be tricky. Since Redis
15
+ supports a few numeric database ids, knowing where it's safe to tread
16
+ during tests is iffy. This is especially true when writing tests that
17
+ integrate through to the service inside a public gem. One errant
18
+ `redis.flushdb` and you might have just dropped some cherished data on
19
+ behalf of a user. That is, not good.
20
+
21
+ How?
22
+ ----
23
+
24
+ The IntegrationTestRedis class provides `start`, `stop`, and `client` methods
25
+ to get at a Redis setup suitable for integration testing. It starts a running,
26
+ non-persistent server for you.
27
+
28
+ Usage
29
+ -----
30
+
31
+ ```ruby
32
+ require "integration_test_redis"
33
+ # Start the server
34
+ IntegrationTestRedis.start
35
+ # Get a suitable client to the server
36
+ redis_client = IntegrationTestRedis.client
37
+ # Stop the server. Also handled automatically in an at_exit callback
38
+ IntegrationTestRedis.stop
39
+ ```
40
+
41
+ Thanks
42
+ ------
43
+
44
+ This code was extracted from the work of various individuals to the
45
+ [Likeable](https://github.com/schneems/likeable) codebase.
46
+
47
+ #### Copyright
48
+
49
+ Copyright (c) (2011) Brendon Murphy. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => [:spec]
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "integration_test_redis/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "integration_test_redis"
7
+ s.version = IntegrationTestRedis::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Brendon Murphy"]
10
+ s.email = ["xternal1+github@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Redis integration test server}
13
+ s.description = %q{Control a non-persistent Redis server for use in integration tests.}
14
+
15
+ s.rubyforge_project = "integration_test_redis"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "redis"
23
+ s.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,3 @@
1
+ module IntegrationTestRedis
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,77 @@
1
+ require "singleton"
2
+ require "tempfile"
3
+ require "redis"
4
+
5
+ class IntegrationTestRedis
6
+ include ::Singleton
7
+
8
+ class DatabaseNotEmpty < RuntimeError
9
+ def initialize(msg = "Database not empty, aborting!")
10
+ super
11
+ end
12
+ end
13
+
14
+ PORT = 9737
15
+ PIDFILE = Tempfile.new('integration-test-redis-pid')
16
+ DIR = Dir.mktmpdir('integration-test-redis')
17
+
18
+ def self.start
19
+ instance.start
20
+ end
21
+
22
+ def self.stop
23
+ instance.stop
24
+ end
25
+
26
+ def self.client
27
+ instance.client
28
+ end
29
+
30
+ def start
31
+ install_at_exit_handler
32
+ system("echo '#{options}' | redis-server -")
33
+ sleep 0.1 # allow a grace period for starting
34
+ enforce_empty_db
35
+ true
36
+ end
37
+
38
+ def stop
39
+ pid = File.read(PIDFILE).to_i
40
+ begin
41
+ Process.getpgid(pid)
42
+ system("kill -QUIT #{pid}")
43
+ rescue Errno::ESRCH
44
+ false
45
+ end
46
+ end
47
+
48
+ def client
49
+ ::Redis.new(:port => PORT, :db => 15)
50
+ end
51
+
52
+ private
53
+
54
+ def options
55
+ {
56
+ 'daemonize' => 'yes',
57
+ 'pidfile' => PIDFILE.path,
58
+ 'bind' => '127.0.0.1',
59
+ 'port' => PORT,
60
+ 'timeout' => 300,
61
+ 'dir' => DIR,
62
+ 'loglevel' => 'debug',
63
+ 'logfile' => 'stdout',
64
+ 'databases' => 16
65
+ }.map { |k, v| "#{k} #{v}" }.join("\n")
66
+ end
67
+
68
+ def enforce_empty_db
69
+ raise DatabaseNotEmpty unless client.dbsize == 0
70
+ end
71
+
72
+ def install_at_exit_handler
73
+ at_exit {
74
+ IntegrationTestRedis.instance.stop
75
+ }
76
+ end
77
+ end
@@ -0,0 +1,92 @@
1
+ require 'rspec'
2
+
3
+ require 'integration_test_redis'
4
+
5
+ module ServerSpecHelpers
6
+ def assert_server_running
7
+ server_pid.should > 0
8
+ lambda {
9
+ Process.getpgid(server_pid)
10
+ }.should_not raise_error
11
+ end
12
+
13
+ def refute_server_running
14
+ lambda {
15
+ Process.getpgid(server_pid)
16
+ }.should raise_error(Errno::ESRCH)
17
+ end
18
+
19
+ def allow_server_grace
20
+ sleep 0.1
21
+ end
22
+
23
+ def start_server
24
+ IntegrationTestRedis.start
25
+ allow_server_grace
26
+ end
27
+
28
+ def stop_server
29
+ IntegrationTestRedis.stop && allow_server_grace
30
+ @server_pid = nil
31
+ end
32
+
33
+ def server_pid
34
+ @server_pid ||= File.read(IntegrationTestRedis::PIDFILE).to_i
35
+ end
36
+ end
37
+
38
+ describe IntegrationTestRedis do
39
+ include ServerSpecHelpers
40
+
41
+ after do
42
+ stop_server
43
+ end
44
+
45
+ it "acts as a singleton to protect access" do
46
+ lambda { IntegrationTestRedis.new }.should raise_error(NoMethodError)
47
+ end
48
+
49
+ it "can start a redis server" do
50
+ start_server
51
+ assert_server_running
52
+ end
53
+
54
+ it "can stop the running redis server" do
55
+ start_server
56
+ stop_server
57
+ refute_server_running
58
+ end
59
+
60
+ it "provides a working client to the server" do
61
+ start_server
62
+ client = IntegrationTestRedis.client
63
+ client.del "foo"
64
+ client.set "foo", "bar"
65
+ client.get("foo").should == "bar"
66
+ end
67
+
68
+ it "doesn't persist data after stop" do
69
+ start_server
70
+ client = IntegrationTestRedis.client
71
+ client.set "fizz", "buzz"
72
+ stop_server
73
+ start_server
74
+ client = IntegrationTestRedis.client
75
+ client.dbsize.should == 0
76
+ end
77
+
78
+ context "when the server starts and data is present" do
79
+ it "raises IntegrationTestRedis::DatabaseNotEmpty" do
80
+ redis = mock("redis")
81
+ Redis.stub(:new).and_return(redis)
82
+ redis.should_receive(:dbsize).and_return(42)
83
+ lambda {
84
+ start_server
85
+ }.should raise_error(IntegrationTestRedis::DatabaseNotEmpty)
86
+ end
87
+ end
88
+
89
+ # it "is killed off at exit" do
90
+ # pending("find a way to test this, fork it?")
91
+ # end
92
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: integration_test_redis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brendon Murphy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-18 00:00:00.000000000 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: redis
17
+ requirement: &2156189320 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2156189320
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &2156188900 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2156188900
37
+ description: Control a non-persistent Redis server for use in integration tests.
38
+ email:
39
+ - xternal1+github@gmail.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - integration_test_redis.gemspec
50
+ - lib/integration_test_redis.rb
51
+ - lib/integration_test_redis/version.rb
52
+ - spec/integration_test_redis_spec.rb
53
+ has_rdoc: true
54
+ homepage: ''
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project: integration_test_redis
74
+ rubygems_version: 1.6.2
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Redis integration test server
78
+ test_files: []