From 15b1cf90b1fc30e3efa78bc0fefc6273bf9251d6 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Sun, 21 Feb 2016 13:50:28 -0500 Subject: [PATCH] TestErrorHandler: assert that the error handler runs Also fixes a race condition using an atomic and adds the race detector to Travis CI. --- .travis.yml | 2 ++ cmux_test.go | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index b0630fb..d2fab9f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,8 @@ go: - 1.5 - 1.6 +gobuild_args: -race + before_install: - go get -u github.com/golang/lint/golint - if [[ $TRAVIS_GO_VERSION == 1.5* ]]; then go get -u github.com/kisielk/errcheck; fi diff --git a/cmux_test.go b/cmux_test.go index 0df6470..cada54e 100644 --- a/cmux_test.go +++ b/cmux_test.go @@ -10,6 +10,7 @@ import ( "sort" "strings" "sync" + "sync/atomic" "testing" "time" ) @@ -212,15 +213,13 @@ func TestErrorHandler(t *testing.T) { go runTestHTTPServer(errCh, httpl) go safeServe(errCh, muxl) - firstErr := true + var errCount uint32 muxl.HandleError(func(err error) bool { - if !firstErr { - return true + if atomic.AddUint32(&errCount, 1) == 1 { + if _, ok := err.(ErrNotMatched); !ok { + t.Errorf("unexpected error: %v", err) + } } - if _, ok := err.(ErrNotMatched); !ok { - t.Errorf("unexpected error: %v", err) - } - firstErr = false return true }) @@ -229,7 +228,11 @@ func TestErrorHandler(t *testing.T) { var num int if err := c.Call("TestRPCRcvr.Test", rpcVal, &num); err == nil { - t.Error("rpc got a response") + // The connection is simply closed. + t.Errorf("unexpected rpc success after %d errors", atomic.LoadUint32(&errCount)) + } + if atomic.LoadUint32(&errCount) == 0 { + t.Errorf("expected at least 1 error(s), got none") } }