Module: RedisTest
- Defined in:
- lib/redis_test.rb,
lib/redis_test/version.rb
Constant Summary collapse
- VERSION =
"0.3.0"
Class Method Summary collapse
- .cache_path ⇒ Object
- .clear ⇒ Object
- .configure(*options) ⇒ Object
- .db_filename ⇒ Object
- .find_available_port ⇒ Object
- .logfile ⇒ Object
- .loglevel ⇒ Object
- .loglevel=(level) ⇒ Object
- .logs_path ⇒ Object
- .pidfile ⇒ Object
- .pids_path ⇒ Object
- .port ⇒ Object
- .server_url ⇒ Object
- .start(log_to_stdout: false) ⇒ Object
- .started? ⇒ Boolean
- .stop ⇒ Object
Class Method Details
.cache_path ⇒ Object
14 15 16 |
# File 'lib/redis_test.rb', line 14 def cache_path "#{Dir.pwd}/tmp/cache/#{port}/" end |
.clear ⇒ Object
137 138 139 140 |
# File 'lib/redis_test.rb', line 137 def clear socket.puts("flushall") socket.gets # wait for redis server to reply with "OK" end |
.configure(*options) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/redis_test.rb', line 108 def configure(*) .flatten.each do |option| case option when :default Redis.current = Redis.new RedisClassy.redis = Redis.current if defined? RedisClassy when :sidekiq Sidekiq.configure_server do |config| config.redis = { url: server_url, namespace: 'sidekiq' } end Sidekiq.configure_client do |config| config.redis = { url: server_url, namespace: 'sidekiq' } end when :ohm Ohm.redis = Redic.new(server_url) when :resque Resque.configure do |config| config.redis = "#{server_url}/resque" end else raise "Unable to configure #{option}" end end end |
.db_filename ⇒ Object
10 11 12 |
# File 'lib/redis_test.rb', line 10 def db_filename "redis-test-#{port}.rdb" end |
.find_available_port ⇒ Object
142 143 144 145 146 147 |
# File 'lib/redis_test.rb', line 142 def find_available_port server = TCPServer.new("127.0.0.1", 0) server.addr[1] ensure server.close if server end |
.logfile ⇒ Object
30 31 32 |
# File 'lib/redis_test.rb', line 30 def logfile "#{logs_path}/redis.#{port}.log" end |
.loglevel ⇒ Object
34 35 36 |
# File 'lib/redis_test.rb', line 34 def loglevel @loglevel || "debug" end |
.loglevel=(level) ⇒ Object
38 39 40 |
# File 'lib/redis_test.rb', line 38 def loglevel=(level) @loglevel = level end |
.logs_path ⇒ Object
22 23 24 |
# File 'lib/redis_test.rb', line 22 def logs_path "#{Dir.pwd}/log" end |
.pidfile ⇒ Object
26 27 28 |
# File 'lib/redis_test.rb', line 26 def pidfile "#{pids_path}/redis-test-#{port}.pid" end |
.pids_path ⇒ Object
18 19 20 |
# File 'lib/redis_test.rb', line 18 def pids_path "#{Dir.pwd}/tmp/pids" end |
.port ⇒ Object
6 7 8 |
# File 'lib/redis_test.rb', line 6 def port @port ||= (ENV['TEST_REDIS_PORT'] || find_available_port).to_i end |
.server_url ⇒ Object
104 105 106 |
# File 'lib/redis_test.rb', line 104 def server_url "redis://localhost:#{port}" end |
.start(log_to_stdout: false) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/redis_test.rb', line 42 def start(log_to_stdout: false) FileUtils.mkdir_p cache_path FileUtils.mkdir_p pids_path FileUtils.mkdir_p logs_path = { "pidfile" => pidfile, "port" => port, "timeout" => 300, "dbfilename" => db_filename, "dir" => cache_path, "loglevel" => loglevel, "databases" => 16 } unless log_to_stdout .merge!( "logfile" => logfile, ) end = .map { |k, v| "#{k} #{v}" }.join('\n') fork do system "echo '#{}' | redis-server -" end wait_time_remaining = 5 begin self.socket = TCPSocket.open("localhost", port) clear @started = true rescue Exception => e if wait_time_remaining > 0 wait_time_remaining -= 0.1 sleep 0.1 else raise "Cannot start redis server in 5 seconds. Pinging server yield\n#{e.inspect}" end end while(!@started) ENV['REDIS_URL'] = server_url end |
.started? ⇒ Boolean
86 87 88 |
# File 'lib/redis_test.rb', line 86 def started? @started end |
.stop ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/redis_test.rb', line 90 def stop if File.file?(pidfile) && File.readable?(pidfile) pid = File.read(pidfile).to_i if pid > 0 Process.kill("QUIT", pid) until (Process.getpgid(pid) rescue nil).nil? do sleep 0.01 end end end FileUtils.rm_f("#{cache_path}#{db_filename}") @started = false end |