Module: RedisTest

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

Constant Summary collapse

VERSION =
"0.4.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loglevelObject



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

def loglevel
  @loglevel || 'debug'
end

Class Method Details

.cache_pathObject



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

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

.clearObject



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



107
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 107

def configure(*options)
  options.flatten.each do |option|
    case option
    when :default
      ENV['REDIS_URL'] = server_url
      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_filenameObject



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

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

.find_available_portObject



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
end

.logfileObject



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

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

.logs_pathObject



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

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

.pidfileObject



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

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

.pids_pathObject



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

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

.portObject



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

def port
  @port ||= (ENV['TEST_REDIS_PORT'] || find_available_port).to_i
end

.server_urlObject



103
104
105
# File 'lib/redis_test.rb', line 103

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
# 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

  redis_options = {
    'pidfile' => pidfile,
    'port' => port,
    'timeout' => 300,
    'dbfilename' => db_filename,
    'dir' => cache_path,
    'loglevel' => loglevel,
    'databases' => 16
  }

  redis_options['logfile'] = logfile unless log_to_stdout

  redis_options_str = redis_options.map { |k, v| "#{k} #{v}" }.join('\n')

  fork do
    echo_command = mac? ? 'echo' : 'echo -e'
    system "#{echo_command} '#{redis_options_str}' | 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)
end

.started?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/redis_test.rb', line 81

def started?
  @started
end

.stopObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/redis_test.rb', line 85

def stop
  if File.file?(pidfile) && File.readable?(pidfile)
    pid = File.read(pidfile).to_i
    if pid > 0
      Process.kill('SIGTERM', pid)
      until (begin
               Process.getpgid(pid)
             rescue StandardError
               nil
             end).nil?
        sleep 0.01
      end
    end
  end
  FileUtils.rm_f("#{cache_path}#{db_filename}")
  @started = false
end