Skip to content

Commit

Permalink
[spec] run the chat server as part of rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed Aug 19, 2017
1 parent 72ed447 commit 90fc35c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
18 changes: 0 additions & 18 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,4 @@ require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :server do
pid = 0
Bundler.with_clean_env do
nul = RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'nul' : '/dev/null'
pid = spawn "bundle exec puma 'spec/chat_server.ru' 2>#{nul}"
end

at_exit do
$stderr.puts "Killing pid #{pid}"
Process.kill('KILL', pid)
Process.wait(pid)
end

sleep 3
end

task :spec => :server

task default: :spec
26 changes: 24 additions & 2 deletions spec/chat_server.ru → spec/chat_server_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,27 @@
MessageBus.config[:backend] = :memory

# Run the Chat Sinatra app.
require "#{chat_example_path}/chat"
run Chat
require File.join(chat_example_path, 'chat')

RSpec.configure do |config|
config.before(:suite) do
Thread.abort_on_exception = true
Chat.set :port, 9292

Thread.new { Chat.run! }

retries = 0
begin
Socket.tcp('localhost', 9292, connect_timeout: 1)
rescue Errno::ECONNREFUSED
if (retries = retries.succ) < 10
sleep 1
retry
end
end
end

config.after(:suite) do
Chat.quit!
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'coverage_helper'
require 'message_bus/client'
require 'chat_server_helper'

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
Expand Down

0 comments on commit 90fc35c

Please sign in to comment.