Last active
February 1, 2016 08:31
-
-
Save erikdubbelboer/91088eeed65e7f485ae1 to your computer and use it in GitHub Desktop.
net.errClosing use case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
erik$ go version | |
go version go1.4.1 darwin/amd64 | |
erik$ go test -v usecase_test.go | |
=== RUN TestUseOfClosedNetworkConnection | |
--- PASS: TestUseOfClosedNetworkConnection (0.01s) | |
usecase_test.go:15: request start | |
usecase_test.go:34: timeout | |
usecase_test.go:17: request end | |
PASS | |
ok command-line-arguments 0.019s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testcase | |
import ( | |
"net" | |
"net/http" | |
"strings" | |
"testing" | |
"time" | |
) | |
func TestUseOfClosedNetworkConnection(t *testing.T) { | |
requestDone := make(chan int) | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
t.Log("request start") | |
time.Sleep(time.Millisecond * 10) | |
t.Log("request end") | |
close(requestDone) | |
}) | |
l, err := net.Listen("tcp", ":12345") | |
if err != nil { | |
t.Fatal(err) | |
} | |
go http.Serve(l, nil) | |
client := &http.Client{ | |
Timeout: time.Millisecond, | |
} | |
if res, err := client.Get("http://127.0.0.1:12345/"); err != nil { | |
if strings.HasSuffix(err.Error(), "use of closed network connection") { | |
t.Log("timeout") | |
} else { | |
t.Fatal(err) | |
} | |
} else { | |
t.Logf("status: %s", res.Status) | |
} | |
l.Close() | |
<-requestDone | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testcase | |
import ( | |
"net" | |
"net/http" | |
"testing" | |
"time" | |
) | |
func TestUseOfClosedNetworkConnection(t *testing.T) { | |
requestDone := make(chan int) | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
t.Log("request start") | |
time.Sleep(time.Millisecond * 10) | |
t.Log("request end") | |
close(requestDone) | |
}) | |
l, err := net.Listen("tcp", ":12345") | |
if err != nil { | |
t.Fatal(err) | |
} | |
go http.Serve(l, nil) | |
client := &http.Client{ | |
Timeout: time.Millisecond, | |
} | |
if res, err := client.Get("http://127.0.0.1:12345/"); err != nil { | |
if err.(net.Error).Timeout() { | |
t.Log("timeout") | |
} else { | |
t.Fatal(err.Error()) | |
} | |
} else { | |
t.Logf("status: %s", res.Status) | |
} | |
l.Close() | |
<-requestDone | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment