Skip to content

Commit 5e5688a

Browse files
authored
Merge pull request #175 from wayfair/add_shutdown_to_client
Add shutdown to client
2 parents fcefc2a + 69df181 commit 5e5688a

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

lib/vault/client.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ def pool
158158

159159
private :pool
160160

161+
# Shutdown any open pool connections. Pool will be recreated upon next request.
162+
def shutdown
163+
@nhp.shutdown()
164+
@nhp = nil
165+
end
166+
161167
# Creates and yields a new client object with the given token. This may be
162168
# used safely in a threadsafe manner because the original client remains
163169
# unchanged. The value of the block is returned.

spec/integration/client_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,65 @@ def free_address
5252
}.to raise_error(MissingTokenError)
5353
end
5454
end
55+
56+
describe "#shutdown" do
57+
it "clears the pool after calling shutdown and sets nhp to nil" do
58+
TCPServer.open('localhost', 0) do |server|
59+
Thread.new do
60+
loop do
61+
client = server.accept
62+
sleep 0.25
63+
client.close
64+
end
65+
end
66+
67+
address = "http://%s:%s" % ["localhost", server.addr[1]]
68+
69+
client = described_class.new(address: address, token: "foo")
70+
71+
expect { client.request(:get, "/", {}, {}) }.to raise_error(HTTPConnectionError)
72+
73+
pool = client.instance_variable_get(:@nhp).pool
74+
75+
client.shutdown()
76+
77+
expect(pool.available.instance_variable_get(:@enqueued)).to eq(0)
78+
expect(pool.available.instance_variable_get(:@shutdown_block)).not_to be_nil
79+
expect(client.instance_variable_get(:@nhp)).to be_nil
80+
81+
server.close
82+
end
83+
end
84+
85+
it "the pool is recreated on the following request" do
86+
TCPServer.open('localhost', 0) do |server|
87+
Thread.new do
88+
loop do
89+
client = server.accept
90+
sleep 0.25
91+
client.close
92+
end
93+
end
94+
95+
address = "http://%s:%s" % ["localhost", server.addr[1]]
96+
97+
client = described_class.new(address: address, token: "foo")
98+
99+
expect { client.request(:get, "/", {}, {}) }.to raise_error(HTTPConnectionError)
100+
101+
client.shutdown()
102+
103+
expect { client.request(:get, "/", {}, {}) }.to raise_error(HTTPConnectionError)
104+
105+
pool = client.instance_variable_get(:@nhp).pool
106+
107+
expect(pool.available.instance_variable_get(:@enqueued)).to eq(1)
108+
expect(pool.available.instance_variable_get(:@shutdown_block)).to be_nil
109+
expect(client.instance_variable_get(:@nhp)).not_to be_nil
110+
111+
server.close
112+
end
113+
end
114+
end
55115
end
56116
end

0 commit comments

Comments
 (0)