Skip to content

Commit

Permalink
Fix flaky travis specs (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpComb authored Jul 21, 2017
1 parent 1eeed52 commit cf5f196
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--require spec_helper
--format documentation
--color
4 changes: 2 additions & 2 deletions lib/kontena/websocket/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,9 @@ def disconnect
# @yield [driver]
# @yieldparam driver [Websocket::Driver]
def with_driver
fail "not connected" unless @driver

@mutex.synchronize {
fail "not connected" unless @driver

yield @driver
}
end
Expand Down
2 changes: 1 addition & 1 deletion spec/kontena/websocket/client_connection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'kontena-websocket-client'

describe Kontena::Websocket::Client::Connection do
RSpec.describe Kontena::Websocket::Client::Connection do
let(:uri) { URI.parse('ws://socket.example.com') }
let(:socket) { instance_double(TCPSocket) }

Expand Down
2 changes: 1 addition & 1 deletion spec/kontena/websocket/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'kontena-websocket-client'

describe Kontena::Websocket::Client do
RSpec.describe Kontena::Websocket::Client do
subject { described_class.new('ws://socket.example.com') }

describe '#initialize' do
Expand Down
56 changes: 34 additions & 22 deletions spec/kontena/websocket/test_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require 'tempfile'
require 'kontena-websocket-client'

describe Kontena::Websocket::Client do
RSpec.describe Kontena::Websocket::Client do
let(:logger) {
Logger.new(STDERR)
logger = Logger.new(STDERR)
logger.level = (ENV['LOG_LEVEL'] || Logger::INFO).to_i
logger.progname = 'test'
logger
}
before do
Thread.abort_on_exception = true
Expand Down Expand Up @@ -66,15 +69,24 @@
end

let(:url) { "ws://127.0.0.1:#{port}" }
let(:options) { { open_timeout: 0.1 }}
let(:options) { { open_timeout: 0.5 }}

context "that immediately closes the connection" do
let(:options) { { open_timeout: 1.0 }}

let(:server_thread) do
Thread.new do
loop do
logger.debug "accept..."
client = tcp_server.accept
client.readpartial(1024)
client.close

begin
logger.debug "read..."
client.readpartial(1024)
ensure
logger.debug "close..."
client.close
end
end
end
end
Expand Down Expand Up @@ -104,7 +116,7 @@
it 'raises an open timeout' do
expect{
subject.connect
}.to raise_error(Kontena::Websocket::TimeoutError, /read timeout after 0.\d+s while waiting 0.1s for open/)
}.to raise_error(Kontena::Websocket::TimeoutError, /read timeout after 0.\d+s while waiting 0.5s for open/)
end
end

Expand All @@ -124,9 +136,14 @@
"Connection: upgrade",
].join("\r\n"))

loop do
client.write("X-Foo: bar\r\n")
Thread.pass
begin
loop do
client.write("X-Foo: bar\r\n")
Thread.pass
end
rescue Errno::ECONNRESET => exc
# expected
logger.debug "server write error: #{exc}"
end
ensure
client.close
Expand All @@ -138,7 +155,7 @@
it 'raises an open timeout' do
expect{
subject.connect
}.to raise_error(Kontena::Websocket::TimeoutError, /read (deadline expired|timeout after 0.\d+s) while waiting 0.1s for open/)
}.to raise_error(Kontena::Websocket::TimeoutError, /read (deadline expired|timeout after 0.\d+s) while waiting 0.5s for open/)
end
end

Expand Down Expand Up @@ -334,6 +351,7 @@
"",
].join("\r\n"))
socket.close
return
end
end

Expand Down Expand Up @@ -365,7 +383,6 @@
end
driver.on :close do |event|
logger.info("websocket server close: #{event}")
socket.close
end

loop do
Expand Down Expand Up @@ -423,19 +440,14 @@

context "with a full send buffer" do
it 'raises write timeout if the server blocks' do
expect{
described_class.connect(url, **options) do |subject|
# XXX: kill thread?
Thread.new do
loop do
subject.send('spam' * 1024 * 8) # ~8KB
end
end
subject.connect

# block the server for 1.0s, enough for the sender_thread to fill the socket read+write buffers
subject.send('sleep')
# block the server for 1.0s, enough for the spam loop to fill the socket read+write buffers
subject.send('sleep')

subject.read
expect{
loop do
subject.send('spam' * 1024 * 8) # ~8KB
end
}.to raise_error(Kontena::Websocket::TimeoutError, 'write timeout after 0.12s')
end
Expand Down
6 changes: 5 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "bundler/setup"
require "kontena/websocket/client"
require "kontena-websocket-client"

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand All @@ -11,4 +11,8 @@
config.expect_with :rspec do |c|
c.syntax = :expect
end

config.before :all do
Kontena::Websocket::Logging.initialize_logger(STDERR, (ENV['LOG_LEVEL'] || Logger::INFO).to_i)
end
end

0 comments on commit cf5f196

Please sign in to comment.