Skip to content

Commit 931f767

Browse files
committed
Merge branch 'fix-wait-for'
2 parents 02239f8 + 200b1ae commit 931f767

File tree

16 files changed

+66
-16
lines changed

16 files changed

+66
-16
lines changed

core/Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ GEM
2525
minitest-hooks (1.5.0)
2626
minitest (> 5.3)
2727
multi_json (1.17.0)
28+
mutex_m (0.3.0)
2829
parallel (1.27.0)
2930
parser (3.3.9.0)
3031
ast (~> 2.4.1)
@@ -77,6 +78,7 @@ PLATFORMS
7778
DEPENDENCIES
7879
minitest (~> 5.0)
7980
minitest-hooks (~> 1.5)
81+
mutex_m (~> 0.3)
8082
rake (~> 13.0)
8183
standard (~> 1.3)
8284
testcontainers-core!

core/lib/testcontainers/docker_container.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def initialize(image, name: nil, command: nil, entrypoint: nil, exposed_ports: n
5555
add_labels(labels) if labels
5656
@working_dir = working_dir
5757
@healthcheck = add_healthcheck(healthcheck) if healthcheck
58+
@wait_for_user_defined = false
5859
@wait_for = add_wait_for(wait_for)
5960
@logger = logger
6061
@_container = nil
@@ -263,16 +264,19 @@ def add_wait_for(method = nil, *args, **kwargs, &block)
263264
if block
264265
if block.arity == 1
265266
@wait_for = block
267+
@wait_for_user_defined = true
266268
else
267269
raise ArgumentError, "Invalid wait_for block: #{block}"
268270
end
269271
elsif @exposed_ports && !@exposed_ports.empty?
270272
port = @exposed_ports.keys.first.split("/").first
271273
@wait_for = ->(container) { container.wait_for_tcp_port(port) }
274+
@wait_for_user_defined ||= false
272275
end
273276
elsif method.is_a?(Proc)
274277
if method.arity == 1
275278
@wait_for = method
279+
@wait_for_user_defined = true
276280
else
277281
raise ArgumentError, "Invalid wait_for method: #{method}"
278282
end
@@ -282,20 +286,26 @@ def add_wait_for(method = nil, *args, **kwargs, &block)
282286
kwargs = method[2] || {}
283287
if respond_to?(method_name)
284288
@wait_for = ->(container) { container.send(method_name, *args, **kwargs) }
289+
@wait_for_user_defined = true
285290
else
286291
raise ArgumentError, "Invalid wait_for method: #{method_name}"
287292
end
288293
else
289294
method_name = :"wait_for_#{method}"
290295
if respond_to?(method_name)
291296
@wait_for = ->(container) { container.send(method_name, *args, **kwargs) }
297+
@wait_for_user_defined = true
292298
else
293299
raise ArgumentError, "Invalid wait_for method: #{method_name}"
294300
end
295301
end
296302
@wait_for
297303
end
298304

305+
def wait_for_user_defined?
306+
@wait_for_user_defined
307+
end
308+
299309
# Set options for the container configuration using "with_" methods.
300310
#
301311
# @param options [Hash] A hash of options where keys correspond to "with_" methods and values are the arguments for those methods.

core/test/docker_container_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,25 @@ def test_it_adds_a_wait_for_with_default
281281
assert_kind_of(Proc, container.wait_for)
282282
end
283283

284+
def test_wait_for_default_from_port_bindings_is_not_user_defined
285+
container = Testcontainers::DockerContainer.new("hello-world", port_bindings: {80 => 8080})
286+
287+
refute container.wait_for_user_defined?
288+
end
289+
290+
def test_wait_for_option_marks_user_defined
291+
container = Testcontainers::DockerContainer.new("hello-world", wait_for: [:logs, /Hello from Docker!/])
292+
293+
assert container.wait_for_user_defined?
294+
end
295+
296+
def test_add_wait_for_healthcheck_marks_user_defined
297+
container = Testcontainers::DockerContainer.new("hello-world")
298+
container.add_wait_for(:healthcheck)
299+
300+
assert container.wait_for_user_defined?
301+
end
302+
284303
def test_it_adds_a_wait_for_with_proc
285304
container = Testcontainers::DockerContainer.new("hello-world")
286305
container.add_wait_for(proc { |c| sleep 1 })

elasticsearch/lib/testcontainers/elasticsearch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def initialize(image = ELASTICSEARCH_DEFAULT_IMAGE, http_port: nil, tcp_port: ni
3030
@username = username || ELASTICSEARCH_DEFAULT_USERNAME
3131
@password = password || ELASTICSEARCH_DEFAULT_PASSWORD
3232
@healthcheck ||= add_healthcheck(_default_healthcheck_options)
33-
@wait_for ||= add_wait_for(:healthcheck)
33+
add_wait_for(:healthcheck) unless wait_for_user_defined?
3434
end
3535

3636
# Starts the container

mariadb/Gemfile.lock

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ PATH
55
base64 (~> 0.3)
66
docker-api (~> 2.4)
77
java-properties (~> 0.3.0)
8+
mutex_m (~> 0.3)
89

910
PATH
1011
remote: .
@@ -18,8 +19,8 @@ GEM
1819
ast (2.4.3)
1920
base64 (0.3.0)
2021
bigdecimal (3.3.1)
21-
docker-api (2.2.0)
22-
excon (>= 0.47.0)
22+
docker-api (2.4.0)
23+
excon (>= 0.64.0)
2324
multi_json
2425
excon (0.99.0)
2526
java-properties (0.3.0)
@@ -30,6 +31,7 @@ GEM
3031
minitest-hooks (1.5.0)
3132
minitest (> 5.3)
3233
multi_json (1.15.0)
34+
mutex_m (0.3.0)
3335
mysql2 (0.5.7)
3436
bigdecimal
3537
parallel (1.27.0)

mariadb/lib/testcontainers/mariadb.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def initialize(image = MARIADB_DEFAULT_IMAGE, username: nil, password: nil, data
3636
@password = password || ENV.fetch("MARIADB_PASSWORD", MARIADB_DEFAULT_PASSWORD)
3737
@database = database || ENV.fetch("MARIADB_DATABASE", MARIADB_DEFAULT_DATABASE)
3838
@healthcheck ||= add_healthcheck(_default_healthcheck_options)
39-
@wait_for ||= add_wait_for(:healthcheck)
39+
add_wait_for(:healthcheck) unless wait_for_user_defined?
4040
end
4141

4242
# Starts the container

mongo/lib/testcontainers/mongo.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def initialize(image = MONGO_DEFAULT_IMAGE, username: nil, password: nil, databa
3535
@password = password || ENV.fetch("MONGO_PASSWORD", MONGO_DEFAULT_PASSWORD)
3636
@database = database || ENV.fetch("MONGO_DATABASE", MONGO_DEFAULT_DATABASE)
3737
@healthcheck ||= add_healthcheck(_default_healthcheck_options)
38-
@wait_for ||= add_wait_for(:healthcheck)
38+
add_wait_for(:healthcheck) unless wait_for_user_defined?
3939
end
4040

4141
# Starts the container

mongo/testcontainers-mongo.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Gem::Specification.new do |spec|
3737
spec.add_development_dependency "minitest-hooks", "~> 1.5"
3838
spec.add_development_dependency "standard", "~> 1.3"
3939
spec.add_development_dependency "mongo", "~> 2.2"
40-
spec.add_development_dependency "bigdecimal", "~> 3.1"
4140

4241
# For more information and examples about making a new gem, check out our
4342
# guide at: https://bundler.io/guides/creating_gem.html

mysql/Gemfile.lock

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ PATH
55
base64 (~> 0.3)
66
docker-api (~> 2.4)
77
java-properties (~> 0.3.0)
8+
mutex_m (~> 0.3)
89

910
PATH
1011
remote: .
@@ -18,8 +19,8 @@ GEM
1819
ast (2.4.3)
1920
base64 (0.3.0)
2021
bigdecimal (3.3.1)
21-
docker-api (2.2.0)
22-
excon (>= 0.47.0)
22+
docker-api (2.4.0)
23+
excon (>= 0.64.0)
2324
multi_json
2425
excon (0.99.0)
2526
java-properties (0.3.0)
@@ -30,6 +31,7 @@ GEM
3031
minitest-hooks (1.5.0)
3132
minitest (> 5.3)
3233
multi_json (1.15.0)
34+
mutex_m (0.3.0)
3335
mysql2 (0.5.7)
3436
bigdecimal
3537
parallel (1.27.0)

mysql/lib/testcontainers/mysql.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def initialize(image = MYSQL_DEFAULT_IMAGE, username: nil, password: nil, databa
3737
@password = password || ENV.fetch("MYSQL_PASSWORD", MYSQL_DEFAULT_PASSWORD)
3838
@database = database || ENV.fetch("MYSQL_DATABASE", MYSQL_DEFAULT_DATABASE)
3939
@healthcheck ||= add_healthcheck(_default_healthcheck_options)
40-
@wait_for ||= add_wait_for(:healthcheck)
40+
add_wait_for(:healthcheck) unless wait_for_user_defined?
4141
end
4242

4343
# Starts the container

0 commit comments

Comments
 (0)