@@ -4832,3 +4832,96 @@ func TestServerIdleTimeout(t *testing.T) {
4832
4832
t .Fatal ("copy byte succeeded; want err" )
4833
4833
}
4834
4834
}
4835
+
4836
+ func get (t * testing.T , c * Client , url string ) string {
4837
+ res , err := c .Get (url )
4838
+ if err != nil {
4839
+ t .Fatal (err )
4840
+ }
4841
+ defer res .Body .Close ()
4842
+ slurp , err := ioutil .ReadAll (res .Body )
4843
+ if err != nil {
4844
+ t .Fatal (err )
4845
+ }
4846
+ return string (slurp )
4847
+ }
4848
+
4849
+ // Tests that calls to Server.SetKeepAlivesEnabled(false) closes any
4850
+ // currently-open connections.
4851
+ func TestServerSetKeepAlivesEnabledClosesConns (t * testing.T ) {
4852
+ if runtime .GOOS == "nacl" {
4853
+ t .Skip ("skipping on nacl; see golang.org/issue/17695" )
4854
+ }
4855
+ defer afterTest (t )
4856
+ ts := httptest .NewServer (HandlerFunc (func (w ResponseWriter , r * Request ) {
4857
+ io .WriteString (w , r .RemoteAddr )
4858
+ }))
4859
+ defer ts .Close ()
4860
+
4861
+ tr := & Transport {}
4862
+ defer tr .CloseIdleConnections ()
4863
+ c := & Client {Transport : tr }
4864
+
4865
+ get := func () string { return get (t , c , ts .URL ) }
4866
+
4867
+ a1 , a2 := get (), get ()
4868
+ if a1 != a2 {
4869
+ t .Fatal ("expected first two requests on same connection" )
4870
+ }
4871
+ var idle0 int
4872
+ if ! waitCondition (2 * time .Second , 10 * time .Millisecond , func () bool {
4873
+ idle0 = tr .IdleConnKeyCountForTesting ()
4874
+ return idle0 == 1
4875
+ }) {
4876
+ t .Fatalf ("idle count before SetKeepAlivesEnabled called = %v; want 1" , idle0 )
4877
+ }
4878
+
4879
+ ts .Config .SetKeepAlivesEnabled (false )
4880
+
4881
+ var idle1 int
4882
+ if ! waitCondition (2 * time .Second , 10 * time .Millisecond , func () bool {
4883
+ idle1 = tr .IdleConnKeyCountForTesting ()
4884
+ return idle1 == 0
4885
+ }) {
4886
+ t .Fatalf ("idle count after SetKeepAlivesEnabled called = %v; want 0" , idle1 )
4887
+ }
4888
+
4889
+ a3 := get ()
4890
+ if a3 == a2 {
4891
+ t .Fatal ("expected third request on new connection" )
4892
+ }
4893
+ }
4894
+
4895
+ func TestServerShutdown_h1 (t * testing.T ) { testServerShutdown (t , h1Mode ) }
4896
+ func TestServerShutdown_h2 (t * testing.T ) { testServerShutdown (t , h2Mode ) }
4897
+
4898
+ func testServerShutdown (t * testing.T , h2 bool ) {
4899
+ defer afterTest (t )
4900
+ var doShutdown func () // set later
4901
+ var shutdownRes = make (chan error , 1 )
4902
+ cst := newClientServerTest (t , h2 , HandlerFunc (func (w ResponseWriter , r * Request ) {
4903
+ go doShutdown ()
4904
+ // Shutdown is graceful, so it should not interrupt
4905
+ // this in-flight response. Add a tiny sleep here to
4906
+ // increase the odds of a failure if shutdown has
4907
+ // bugs.
4908
+ time .Sleep (20 * time .Millisecond )
4909
+ io .WriteString (w , r .RemoteAddr )
4910
+ }))
4911
+ defer cst .close ()
4912
+
4913
+ doShutdown = func () {
4914
+ shutdownRes <- cst .ts .Config .Shutdown (context .Background ())
4915
+ }
4916
+ get (t , cst .c , cst .ts .URL ) // calls t.Fail on failure
4917
+
4918
+ if err := <- shutdownRes ; err != nil {
4919
+ t .Fatalf ("Shutdown: %v" , err )
4920
+ }
4921
+
4922
+ res , err := cst .c .Get (cst .ts .URL )
4923
+ if err == nil {
4924
+ res .Body .Close ()
4925
+ t .Fatal ("second request should fail. server should be shut down" )
4926
+ }
4927
+ }
0 commit comments