From ad1198db859682a7fcb734b1d5c98c50c21c3294 Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Fri, 22 Apr 2016 13:03:04 -0600 Subject: [PATCH] Add code coverage -Test coverage was no longer triggered due to the check for BUILD_GOOS environment variable that was removed. Removed the check. -Re-run test package with server code coverage. -Remove unused functions in test.go. -Add test for a function in test.go. -Add missing parse +OK test. --- .travis.yml | 4 ++-- scripts/cov.sh | 1 + server/parser_test.go | 28 ++++++++++++++++++++++++++++ test/test.go | 41 ----------------------------------------- test/test_test.go | 31 +++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 43 deletions(-) create mode 100644 test/test_test.go diff --git a/.travis.yml b/.travis.yml index 8e04d4c0..979d253b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,5 +16,5 @@ script: - go test -i -race ./... - go test -v -race ./... after_script: -- if [ "$TRAVIS_GO_VERSION" = "1.6" ] && [ "$BUILD_GOOS" = "linux" ]; then ./scripts/cov.sh TRAVIS; fi -- if [ "$TRAVIS_GO_VERSION" = "1.6" ] && [ "$BUILD_GOOS" = "linux" ] && [ "$TRAVIS_TAG" != "" ]; then ./scripts/cross_compile.sh $TRAVIS_TAG; ghr --username nats-io --token $GITHUB_TOKEN --replace $TRAVIS_TAG pkg/; fi +- if [ "$TRAVIS_GO_VERSION" = "1.6" ]; then ./scripts/cov.sh TRAVIS; fi +- if [ "$TRAVIS_GO_VERSION" = "1.6" ] && [ "$TRAVIS_TAG" != "" ]; then ./scripts/cross_compile.sh $TRAVIS_TAG; ghr --username nats-io --token $GITHUB_TOKEN --replace $TRAVIS_TAG pkg/; fi diff --git a/scripts/cov.sh b/scripts/cov.sh index 73f3ca9f..c4e05f22 100755 --- a/scripts/cov.sh +++ b/scripts/cov.sh @@ -8,6 +8,7 @@ go test -v -covermode=atomic -coverprofile=./cov/conf.out ./conf go test -v -covermode=atomic -coverprofile=./cov/log.out ./logger go test -v -covermode=atomic -coverprofile=./cov/server.out ./server go test -v -covermode=atomic -coverprofile=./cov/test.out ./test +go test -v -covermode=atomic -coverprofile=./cov/test2.out -coverpkg=./server ./test gocovmerge ./cov/*.out > acc.out rm -rf ./cov diff --git a/server/parser_test.go b/server/parser_test.go index 63d0403a..daaee6be 100644 --- a/server/parser_test.go +++ b/server/parser_test.go @@ -490,3 +490,31 @@ func TestProtoSnippet(t *testing.T) { } } } + +func TestParseOK(t *testing.T) { + c := dummyClient() + if c.state != OP_START { + t.Fatalf("Expected OP_START vs %d\n", c.state) + } + okProto := []byte("+OK\r\n") + err := c.parse(okProto[:1]) + if err != nil || c.state != OP_PLUS { + t.Fatalf("Unexpected: %d : %v\n", c.state, err) + } + err = c.parse(okProto[1:2]) + if err != nil || c.state != OP_PLUS_O { + t.Fatalf("Unexpected: %d : %v\n", c.state, err) + } + err = c.parse(okProto[2:3]) + if err != nil || c.state != OP_PLUS_OK { + t.Fatalf("Unexpected: %d : %v\n", c.state, err) + } + err = c.parse(okProto[3:4]) + if err != nil || c.state != OP_PLUS_OK { + t.Fatalf("Unexpected: %d : %v\n", c.state, err) + } + err = c.parse(okProto[4:5]) + if err != nil || c.state != OP_START { + t.Fatalf("Unexpected: %d : %v\n", c.state, err) + } +} diff --git a/test/test.go b/test/test.go index fac72f52..a0d7d62d 100644 --- a/test/test.go +++ b/test/test.go @@ -117,47 +117,6 @@ func RunServerWithAuth(opts *server.Options, auth server.Auth) *server.Server { panic("Unable to start NATS Server in Go Routine") } -func startServer(t tLogger, port int, other string) *natsServer { - var s natsServer - args := fmt.Sprintf("-p %d %s", port, other) - s.args = strings.Split(args, " ") - s.cmd = exec.Command(natsServerExe, s.args...) - err := s.cmd.Start() - if err != nil { - s.cmd = nil - t.Errorf("Could not start <%s> [%s], is NATS installed and in path?", natsServerExe, err) - return &s - } - // Give it time to start up - start := time.Now() - for { - addr := fmt.Sprintf("localhost:%d", port) - c, err := net.Dial("tcp", addr) - if err != nil { - time.Sleep(50 * time.Millisecond) - if time.Since(start) > (5 * time.Second) { - t.Fatalf("Timed out trying to connect to %s", natsServerExe) - return nil - } - } else { - c.Close() - // Wait a bit to give a chance to the server to remove this - // "client" from its state, which may otherwise interfere with - // some tests. - time.Sleep(25 * time.Millisecond) - break - } - } - return &s -} - -func (s *natsServer) stopServer() { - if s.cmd != nil && s.cmd.Process != nil { - s.cmd.Process.Kill() - s.cmd.Process.Wait() - } -} - func stackFatalf(t tLogger, f string, args ...interface{}) { lines := make([]string, 0, 32) msg := fmt.Sprintf(f, args...) diff --git a/test/test_test.go b/test/test_test.go new file mode 100644 index 00000000..ac813ebc --- /dev/null +++ b/test/test_test.go @@ -0,0 +1,31 @@ +// Copyright 2016 Apcera Inc. All rights reserved. + +package test + +import ( + "fmt" + "strings" + "testing" +) + +type dummyLogger struct { + msg string +} + +func (d *dummyLogger) Fatalf(format string, args ...interface{}) { + d.msg = fmt.Sprintf(format, args...) + +} +func (d *dummyLogger) Errorf(format string, args ...interface{}) { +} + +func TestStackFatal(t *testing.T) { + d := &dummyLogger{} + stackFatalf(d, "test stack %d", 1) + if !strings.HasPrefix(d.msg, "test stack 1") { + t.Fatalf("Unexpected start of stack: %v", d.msg) + } + if !strings.Contains(d.msg, "test_test.go") { + t.Fatalf("Unexpected stack: %v", d.msg) + } +}