Waiting docker container to continue tests

9 views
Skip to first unread message

Samuel Mutel

unread,
Apr 6, 2018, 11:55:43 AM4/6/18
to rspec
Hello,

I am currently using rspec and docker api to test docker container (@container = Docker::Container.create & @container.start).
I would like to know if it is possible to wait a condition (port available for example) to continue the test suite?
I saw this module rspec-wait ...

Thanks.

Myron Marston

unread,
Apr 6, 2018, 12:01:34 PM4/6/18
to rs...@googlegroups.com

RSpec doesn’t have any built-in features for what you’re asking for, but it’s easy enough to write it yourself:

require 'timeout'

module WaitHelpers
  def wait_until(timeout)
    timeout_time = Time.now + timeout
    loop do
      sleep 0.1
      return if yield
      raise Timeout::Error if Time.now > timeout_time
    end
  end

  def wait_until_port_available(container)
    wait_until(10) do
      container.port.available? # or whatever the logic to check port availability is
    end
  end
end

RSpec.configure do |rspec|
  rspec.include WaitHelpers
end

RSpec.describe "A docker container" do
  before do
    @container = Docker::Container.create
    @container.start
    wait_until_port_available(@container)
  end
end


HTH,
Myron

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscribe@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/1ff39192-ef6a-4dd1-830c-c120d61faafd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages