Skip to content

Commit ad76ba9

Browse files
author
Ethan
committed
add connection test to dynamolocal
1 parent 4c9b331 commit ad76ba9

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

lib/dynamo-local-ruby/dynamo_db_local.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class DynamoDBLocal
88

99
PATH_TO_JAR = '../../../lib/jars/dynamodb_local'
1010

11+
RETRIES = 5
12+
1113
def initialize(pid)
1214
@pid = pid
1315
end
@@ -21,12 +23,25 @@ def up
2123
"-sharedDb -inMemory -port #{PORT}")
2224
@instance = DynamoDBLocal.new(pid)
2325

26+
test_connection
27+
2428
@instance
2529
end
2630

2731
def down
2832
@instance.down if defined? @instance
2933
end
34+
35+
def test_connection
36+
RETRIES.times do
37+
begin
38+
Net::HTTP.get_response(URI.parse(ENDPOINT))
39+
break
40+
rescue Errno::ECONNREFUSED
41+
sleep(0.5)
42+
end
43+
end
44+
end
3045
end
3146

3247
def down

spec/dynamo-local-ruby/dynamo_db_local_spec.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
describe DynamoLocalRuby::DynamoDBLocal do
55
let(:dynamo_instance) { described_class.up }
66

7-
describe '.up' do
8-
before do
9-
stub_const("#{described_class}::PATH_TO_JAR", SPEC_JAR_DIR)
10-
end
7+
before do
8+
stub_const("#{described_class}::PATH_TO_JAR", SPEC_JAR_DIR)
9+
end
1110

11+
describe '.up' do
1212
after(:each) do
1313
described_class.down
1414
end
@@ -26,10 +26,31 @@
2626
let(:uri) { URI.parse(described_class::ENDPOINT) }
2727
let(:response) { Net::HTTP.get_response(uri) }
2828

29+
context 'but is not accepting connections' do
30+
context 'retries and is now accepting connections' do
31+
it 'succeeds' do
32+
call_count = 0
33+
expect(Net::HTTP).to receive(:get_response).twice do
34+
call_count += 1
35+
fail Errno::ECONNREFUSED if call_count == 1
36+
end
37+
38+
dynamo_instance
39+
end
40+
end
41+
42+
context 'retries and never accepts connections' do
43+
it 'tries 5 times' do
44+
expect(Net::HTTP).to receive(:get_response).exactly(5).times \
45+
.and_raise(Errno::ECONNREFUSED.new)
46+
dynamo_instance
47+
end
48+
end
49+
end
50+
2951
it 'responds to requests' do
3052
# I'd like this to be in a before block but couldn't get it working
3153
dynamo_instance
32-
sleep(2) # remove this once retry is enabled
3354
expect { response }.to_not raise_error
3455
end
3556
end

spec/dynamo-local-ruby/schema_loader_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
before(:each) do
1717
DynamoLocalRuby::DynamoDBLocal.up
18-
sleep(2) # remove once retries are enabled
1918
end
2019

2120
after(:each) do

0 commit comments

Comments
 (0)