Module: RedisTest

Defined in:
lib/redis_test.rb,
lib/redis_test/version.rb

Constant Summary collapse

VERSION =
"0.0.9"

Class Method Summary collapse

Class Method Details

.cache_pathObject



13
14
15
# File 'lib/redis_test.rb', line 13

def cache_path
  "#{Dir.pwd}/tmp/cache/#{port}/"
end

.clearObject



116
117
118
119
# File 'lib/redis_test.rb', line 116

def clear
  socket.puts("flushall")
  socket.gets # wait for redis server to reply with "OK"
end

.configure(*options) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/redis_test.rb', line 87

def configure(*options)
  options.flatten.each do |option|
    case option
    when :default
      Redis.current = Redis.new

    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_filenameObject



9
10
11
# File 'lib/redis_test.rb', line 9

def db_filename
  "redis-test-#{port}.rdb"
end

.logfileObject



29
30
31
# File 'lib/redis_test.rb', line 29

def logfile
  "#{logs_path}/redis.#{port}.log"
end

.loglevelObject



33
34
35
# File 'lib/redis_test.rb', line 33

def loglevel
  @loglevel || "debug"
end

.loglevel=(level) ⇒ Object



37
38
39
# File 'lib/redis_test.rb', line 37

def loglevel=(level)
  @loglevel = level
end

.logs_pathObject



21
22
23
# File 'lib/redis_test.rb', line 21

def logs_path
  "#{Dir.pwd}/log"
end

.pidfileObject



25
26
27
# File 'lib/redis_test.rb', line 25

def pidfile
  "#{pids_path}/redis-test-#{port}.pid"
end

.pids_pathObject



17
18
19
# File 'lib/redis_test.rb', line 17

def pids_path
  "#{Dir.pwd}/tmp/pids"
end

.portObject



5
6
7
# File 'lib/redis_test.rb', line 5

def port
  (ENV['TEST_REDIS_PORT'] || 9736).to_i
end

.server_urlObject



83
84
85
# File 'lib/redis_test.rb', line 83

def server_url
  "redis://localhost:#{port}"
end

.startObject



41
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
# File 'lib/redis_test.rb', line 41

def start
  FileUtils.mkdir_p cache_path
  FileUtils.mkdir_p pids_path
  FileUtils.mkdir_p logs_path

  redis_options = {
    "daemonize"     => 'yes',
    "pidfile"       => pidfile,
    "logfile"       => logfile,
    "port"          => port,
    "timeout"       => 300,
    "dbfilename"    => db_filename,
    "dir"           => cache_path,
    "loglevel"      => loglevel,
    "databases"     => 16
  }.map { |k, v| "#{k} #{v}" }.join('\n')
  `echo '#{redis_options}' | redis-server -`

  wait_time_remaining = 5
  begin
    self.socket = TCPSocket.open("localhost", port)
    clear
    success = 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(!success)

  ENV['REDIS_URL'] = server_url
end

.stopObject



76
77
78
79
80
81
# File 'lib/redis_test.rb', line 76

def stop
  %x{
    cat #{pidfile} | xargs kill -QUIT
    rm -f #{cache_path}#{db_filename}
  }
end