Skip to content

Commit

Permalink
Add code coverage
Browse files Browse the repository at this point in the history
-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.
  • Loading branch information
kozlovic committed Apr 22, 2016
1 parent 91b69f1 commit ad1198d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions scripts/cov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions server/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
41 changes: 0 additions & 41 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
31 changes: 31 additions & 0 deletions test/test_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit ad1198d

Please sign in to comment.