> On 14 Nov 2020, at 10:28 pm, Amit Saha <amitsaha...@gmail.com> wrote:
> 
> Hi all, I am attempting to write a test for http client timeout behaviour. 
> Here’s my bad test server:
> 
> func startBadTestHTTPServer() *httptest.Server {
>       ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r 
> *http.Request) {
>               time.Sleep(60 * time.Second)
>               fmt.Fprint(w, "Hello World")
>       }))
>       return ts
> }
> 
> func TestFetchBadRemoteResource(t *testing.T) {
>       ts := startBadTestHTTPServer()
>       defer ts.Close()
> 
>       client := http.Client{Timeout: 15 * time.Second}
>       data, err := fetchRemoteResource(client, ts.URL)
>       if err != nil {
>               t.Fatal(err)
>       }
> 
>       expected := "Hello World"
>       got := string(data)
> 
>       if expected != got {
>               t.Errorf("Expected response to be: %s, Got: %s", expected, got)
>       }
> }
> 
> When I run the test, I get:
> 
> RUN   TestFetchBadRemoteResource
>    fetch_remote_resource_bad_server_test.go:27: Get "http://127.0.0.1:62721": 
> context deadline exceeded (Client.Timeout exceeded while awaiting headers)
> 
> 
> This is expected. But I also get:
> 
> 2020/11/14 22:24:58 httptest.Server blocked in Close after 5 seconds, waiting 
> for connections:
>  *net.TCPConn 0xc000124040 127.0.0.1:62722 in state active
> --- FAIL: TestFetchBadRemoteResource (60.00s)
> 
> 
> I understand why this is happening. But, my test still waits for 60 seconds 
> for the server to shutdown.
> 
> Is there a way around this behaviour where the test server can be forcibly 
> terminated and just carry on?

I didn’t succeed in finding a way for exactly what I wanted. However looking at 
https://golang.org/src/net/http/client_test.go I found a much better way to 
simulate a bad server. Instead a time.Sleep(), have it block on a channel and 
then write to the channel in the test. So my bad server now is:

func startBadTestHTTPServer(shutdownServer chan struct{}) *httptest.Server {
        ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r 
*http.Request) {
                <-shutdownServer
                fmt.Fprint(w, "Hello World")
        }))
        return ts
}

In the test I then do:

data, err := fetchRemoteResource(client, ts.URL)
        if err != nil {
                shutdownServer <- struct{}{}
                t.Fatal(err)
}



> 
> Thanks,
> Amit
> 
> 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/66CC9BF3-EB1F-4ED0-878E-680D74077AA9%40gmail.com.

Reply via email to