Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created March 9, 2021 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/a667d3a7faa50c89e1d44bfbc919a5ea to your computer and use it in GitHub Desktop.
Save podhmo/a667d3a7faa50c89e1d44bfbc919a5ea to your computer and use it in GitHub Desktop.
package expecterror
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestTimeout(t *testing.T) {
timeout := 100 * time.Millisecond
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("for test -- timeout forcely %v", 2*timeout)
time.Sleep(timeout)
}))
defer ts.Close()
client := &http.Client{Timeout: timeout}
_, err := client.Get(ts.URL)
if err == nil {
t.Errorf("error is expected but nil")
}
// context.DeadlineExceeded あたりになってくれれば嬉しいのだけれど。。
// &url.Error{Op:"Get", URL:"http://127.0.0.1:59233", Err:(*http.httpError)(0xc0001b2040)}
var x interface{ Timeout() bool }
if ok := errors.As(err, &x); !(ok && x.Timeout()) {
t.Errorf("timeout is expected but return error is %[1]T, %#+[1]v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment