@@ -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
56116end
0 commit comments