redis_test 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 68c259a753408811ee1e2c7c716b87f18ad897e6
4
+ data.tar.gz: 101f4f1167f1a1a0e7376c2b2da822781da0e886
5
+ SHA512:
6
+ metadata.gz: 8b1568af13475d87d9402cff828c76e5f582cda89cf04f6bdea775eaf8b6021f54f341ac774d30b239a24e7d87113269e9bc1c35146ef1a506e383d56207f136
7
+ data.tar.gz: cfaf64d531c02e590ea56d7a5efeeafde630c2aad6d165694f5baacf9fdb3bfa689da71db813c94fad7523e96f6fa240eac9c5d54e4e2c534b0cf0cdf6056b29
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 redis-test.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Phuong Nguyen
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,91 @@
1
+ # RedisTest
2
+
3
+ This is a very simple gem to help start/stop an instance of redis server
4
+ during test.
5
+
6
+ There is different option out there like fakeredis but I dislike
7
+ fakeredis approach as it require the gem to re-implement all the
8
+ functionality of redis, so every time redis is upgrading, the gem
9
+ need to be updated as well.
10
+
11
+ My approach is very simple, and may be already widely used:
12
+ Start a redis server on port 9736 (can be
13
+ customized by setting `ENV['TEST_REDIS_PORT']`) and simply change your
14
+ config so your redis client will connect there during test instead.
15
+
16
+ I just try to package it in a convenient way so I don't have to repeat
17
+ it for every project I use.
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ gem 'redis_test'
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install redis_test
32
+
33
+ ## Usage
34
+
35
+ You can use it with RSpec by putting this block under your spec/support:
36
+
37
+ ```ruby
38
+ RSpec.configure do |config|
39
+ config.before(:suite) do
40
+ RedisTest.start
41
+ RedisTest.configure(:default, :sidekiq, :ohm)
42
+ # RedisTest provide common configuration for :default (set
43
+ # Redis.current), :sidekiq, :ohm and :resque.
44
+ end
45
+
46
+ config.after(:each) do
47
+ RedisTest.clear
48
+ # notice that will flush the Redis db, so it's less
49
+ # desirable to put that in a config.before(:each) since it may clean any
50
+ # data that you try to put in redis prior to that
51
+ end
52
+
53
+ config.after(:suite) do
54
+ RedisTest.stop
55
+ end
56
+ end
57
+ ```
58
+
59
+ Or with Cucumber by putting this block under your features/support:
60
+
61
+ ```ruby
62
+ RedisTest.start # start this when cucumber load
63
+ RedisTest.configure(:default, :sidekiq, :ohm)
64
+ # available option: :default, :sidekiq, :ohm, :resque
65
+
66
+ After do
67
+ RedisTest.clear
68
+ # clear redis after every scenario to avoid interference
69
+ end
70
+
71
+ at_exit do
72
+ RedisTest.stop
73
+ end
74
+
75
+ ```
76
+
77
+ There is a `RedisTest.server_url` (which by default return
78
+ `"redis://localhost:9763"`) that you can use to configure your custom tool
79
+ that use Redis
80
+
81
+ ## For Parallel Testing
82
+ You can start multiple instances of redis in parallel by rotating
83
+ `ENV['TEST_REDIS_PORT']`
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it
88
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
89
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
90
+ 4. Push to the branch (`git push origin my-new-feature`)
91
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module RedisTest
2
+ VERSION = "0.0.1"
3
+ end
data/lib/redis_test.rb ADDED
@@ -0,0 +1,103 @@
1
+ require "redis_test/version"
2
+
3
+ module RedisTest
4
+ class << self
5
+ def port
6
+ (ENV['TEST_REDIS_PORT'] || 9736).to_i
7
+ end
8
+
9
+ def db_filename
10
+ "redis-test-#{port}.rdb"
11
+ end
12
+
13
+ def cache_path
14
+ "#{Rails.root}/tmp/cache/#{port}/"
15
+ end
16
+
17
+ def pids_path
18
+ "#{Rails.root}/tmp/pids"
19
+ end
20
+
21
+ def pidfile
22
+ "#{pids_path}/redis-test-#{port}.pid"
23
+ end
24
+
25
+ def start
26
+ FileUtils.mkdir_p cache_path
27
+ FileUtils.mkdir_p pids_path
28
+ redis_options = {
29
+ "daemonize" => 'yes',
30
+ "pidfile" => pidfile,
31
+ "port" => port,
32
+ "timeout" => 300,
33
+ "save 900" => 1,
34
+ "save 300" => 1,
35
+ "save 60" => 10000,
36
+ "dbfilename" => db_filename,
37
+ "dir" => cache_path,
38
+ "loglevel" => "debug",
39
+ "logfile" => "stdout",
40
+ "databases" => 16
41
+ }.map { |k, v| "#{k} #{v}" }.join('\n')
42
+ `echo '#{redis_options}' | redis-server -`
43
+
44
+
45
+ wait_time_remaining = 5
46
+ begin
47
+ TCPSocket.open("localhost", port)
48
+ success = true
49
+ rescue Exception => e
50
+ if wait_time_remaining > 0
51
+ wait_time_remaining -= 0.1
52
+ sleep 0.1
53
+ else
54
+ raise "Cannot start redis server in 5 seconds. Pinging server yield\n#{e.inspect}"
55
+ end
56
+ end while(!success)
57
+ end
58
+
59
+ def stop
60
+ %x{
61
+ cat #{pidfile} | xargs kill -QUIT
62
+ rm -f #{cache_path}#{db_filename}
63
+ }
64
+ end
65
+
66
+ def server_url
67
+ "redis://localhost:#{port}"
68
+ end
69
+
70
+ def configure(*options)
71
+ options.flatten.each do |option|
72
+ case option
73
+ when :default
74
+ Redis.current = Redis.new(url: server_url)
75
+
76
+ when :sidekiq
77
+ Sidekiq.configure_server do |config|
78
+ config.redis = { url: server_url, namespace: 'sidekiq' }
79
+ end
80
+
81
+ Sidekiq.configure_client do |config|
82
+ config.redis = { url: server_url, namespace: 'sidekiq' }
83
+ end
84
+
85
+ when :ohm
86
+ Ohm.redis = Redic.new(server_url)
87
+
88
+ when :resque
89
+ Resque.configure do |config|
90
+ config.redis = "#{server_url}/resque"
91
+ end
92
+
93
+ else
94
+ raise "Unable to configure #{option}"
95
+ end
96
+ end
97
+ end
98
+
99
+ def clear
100
+ Redis.current.flushdb
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_test/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redis_test"
8
+ spec.version = RedisTest::VERSION
9
+ spec.authors = ["Phuong Nguyen"]
10
+ spec.email = ["phuongnd08@gmail.com"]
11
+ spec.description = %q{A helper to launch redis server during test}
12
+ spec.summary = %q{A helper to launch redis server during test}
13
+ spec.homepage = "https://github.com/phuongnd08/redis_test"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_test
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Phuong Nguyen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A helper to launch redis server during test
42
+ email:
43
+ - phuongnd08@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/redis_test.rb
54
+ - lib/redis_test/version.rb
55
+ - redis_test.gemspec
56
+ homepage: https://github.com/phuongnd08/redis_test
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.0.3
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: A helper to launch redis server during test
80
+ test_files: []