Class: Riak::TestServer

Inherits:
Object show all
Defined in:
riak-client/lib/riak/test_server.rb

Constant Summary

APP_CONFIG_DEFAULTS =
{
  :riak_core => {
    :web_ip => "127.0.0.1",
    :web_port => 9000,
    :handoff_port => 9001,
    :ring_creation_size => 64
  },
  :riak_kv => {
    :storage_backend => :riak_kv_test_backend,
    :pb_ip => "127.0.0.1",
    :pb_port => 9002,
    :js_vm_count => 8,
    :js_max_vm_mem => 8,
    :js_thread_stack => 16,
    :riak_kv_stat => true,
    # Turn off map caching
    :map_cache_size => 0,     # 0.14
    :vnode_cache_entries => 0 # 0.13
  },
  :luwak => {
    :enabled => false
  }
}
VM_ARGS_DEFAULTS =
{
  "-name" => "riaktest#{rand(1000000).to_s}@127.0.0.1",
  "-setcookie" => "#{rand(1000000).to_s}_#{rand(1000000).to_s}",
  "+K" => true,
  "+A" => 64,
  "-smp" => "enable",
  "-env ERL_MAX_PORTS" => 4096,
  "-env ERL_FULLSWEEP_AFTER" => 10,
  "-pa" => File.expand_path("../../../erl_src", __FILE__)
}
DEFAULTS =
{
  :app_config => APP_CONFIG_DEFAULTS,
  :vm_args => VM_ARGS_DEFAULTS,
  :temp_dir => File.join(Dir.tmpdir,'riaktest'),
}

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (TestServer) initialize(options = {})

A new instance of TestServer



62
63
64
65
66
67
68
69
70
71
72
# File 'riak-client/lib/riak/test_server.rb', line 62

def initialize(options={})
  options   = deep_merge(DEFAULTS.dup, options)
  @temp_dir = File.expand_path(options[:temp_dir])
  @bin_dir  = File.expand_path(options[:bin_dir])
  options[:app_config][:riak_core][:ring_state_dir] ||= File.join(@temp_dir, "data", "ring")
  @app_config = options[:app_config]
  @vm_args    = options[:vm_args]
  # For synchronizing start/stop/recycle
  @mutex = Mutex.new
  cleanup # Should prevent some errors related to unclean startup
end

Instance Attribute Details

- (Object) app_config

Returns the value of attribute app_config



60
61
62
# File 'riak-client/lib/riak/test_server.rb', line 60

def app_config
  @app_config
end

- (Object) cerr

Returns the value of attribute cerr



60
61
62
# File 'riak-client/lib/riak/test_server.rb', line 60

def cerr
  @cerr
end

- (Object) cin

Returns the value of attribute cin



60
61
62
# File 'riak-client/lib/riak/test_server.rb', line 60

def cin
  @cin
end

- (Object) cout

Returns the value of attribute cout



60
61
62
# File 'riak-client/lib/riak/test_server.rb', line 60

def cout
  @cout
end

- (Object) cpid

Returns the value of attribute cpid



60
61
62
# File 'riak-client/lib/riak/test_server.rb', line 60

def cpid
  @cpid
end

- (Object) temp_dir

Returns the value of attribute temp_dir



60
61
62
# File 'riak-client/lib/riak/test_server.rb', line 60

def temp_dir
  @temp_dir
end

- (Object) vm_args

Returns the value of attribute vm_args



60
61
62
# File 'riak-client/lib/riak/test_server.rb', line 60

def vm_args
  @vm_args
end

Instance Method Details

- (Object) cleanup

Cleans up any files and directories generated by the test server.



153
154
155
156
157
# File 'riak-client/lib/riak/test_server.rb', line 153

def cleanup
  stop if @started
  FileUtils.rm_rf(@temp_dir)
  @prepared = false
end

- (Object) prepare!

Sets up the proper scripts, configuration and directories for the test server. Call at the top of your test suite (not in a setup method).



77
78
79
80
81
82
83
84
85
86
# File 'riak-client/lib/riak/test_server.rb', line 77

def prepare!
  unless @prepared
    create_temp_directories
    @riak_script = File.join(@temp_bin, 'riak')
    write_riak_script
    write_vm_args
    write_app_config
    @prepared = true
  end
end

- (Object) recycle

Causes the entire contents of the Riak in-memory backends to be dumped by performing a soft restart.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'riak-client/lib/riak/test_server.rb', line 125

def recycle
  if @started
    @mutex.synchronize do
      begin
        if @app_config[:riak_kv][:storage_backend] == :riak_kv_test_backend
          @cin.puts "riak_kv_test_backend:reset()."
          @cin.flush
          wait_for_erlang_prompt
        else
          @cin.puts "init:restart()."
          @cin.flush
          wait_for_erlang_prompt
          wait_for_startup
        end
      rescue Errno::EPIPE
        warn "Broken pipe when recycling, is Riak alive?"
        register_stop
        return false
      end
    end
    true
  else
    start
  end
end

- (Object) start

Starts the test server if it is not already running, and waits for it to respond to pings.



90
91
92
93
94
95
96
97
98
99
100
# File 'riak-client/lib/riak/test_server.rb', line 90

def start
  if @prepared && !@started
    @mutex.synchronize do
      @cin, @cout, @cerr, @cpid = Open3.popen3("#{@riak_script} console")
      @cin.puts
      @cin.flush
      wait_for_erlang_prompt
      @started = true
    end
  end
end

- (Boolean) started?

Whether the server has been started.

Returns:



119
120
121
# File 'riak-client/lib/riak/test_server.rb', line 119

def started?
  @started
end

- (Object) stop

Stops the test server if it is running.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'riak-client/lib/riak/test_server.rb', line 103

def stop
  if @started
    @mutex.synchronize do
      begin
        @cin.puts "init:stop()."
        @cin.flush
      rescue Errno::EPIPE
      ensure
        register_stop
      end
    end
    true
  end
end