Skip to content

Commit

Permalink
test data race + fix data race in output
Browse files Browse the repository at this point in the history
  • Loading branch information
genofire committed Nov 9, 2017
1 parent ccff2ee commit 9ecfa3d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
24 changes: 17 additions & 7 deletions circle.yml
@@ -1,32 +1,42 @@
version: 2
jobs:
build:
buildenviroment:
docker:
- image: circleci/golang:latest
working_directory: /go/src/github.com/FreifunkBremen/yanic
steps:
- checkout
- run: go get -t -d -v ./...
build:
working_directory: /go/src/github.com/FreifunkBremen/yanic
steps:
- run: go install github.com/FreifunkBremen/yanic
- store_artifacts:
path: /go/bin/
destination: yanic
test:
docker:
- image: circleci/golang:latest
working_directory: /go/src/github.com/FreifunkBremen/yanic
steps:
- checkout
- run: go get -t -d -v ./...
- run: go get github.com/mattn/goveralls
- run: go get golang.org/x/tools/cmd/cover
- run: ./.test-coverage circle-ci
- store_test_results:
path: ./
destination: profile.cov

test-race:
working_directory: /go/src/github.com/FreifunkBremen/yanic
steps:
- run: go test -race ./...

workflows:
version: 2
build_and_test:
jobs:
- build
- test
- buildenviroment
- build:
requires: buildenviroment
- test:
requires: buildenviroment
- test-race:
requires: buildenviroment
17 changes: 13 additions & 4 deletions output/all/internal_test.go
Expand Up @@ -2,6 +2,7 @@ package all

import (
"errors"
"sync"
"testing"

"github.com/FreifunkBremen/yanic/output"
Expand All @@ -11,11 +12,19 @@ import (

type testOutput struct {
output.Output
CountSave int
countSave int
sync.Mutex
}

func (c *testOutput) Save(nodes *runtime.Nodes) {
c.CountSave++
c.Lock()
c.countSave++
c.Unlock()
}
func (c *testOutput) Get() int {
c.Lock()
defer c.Unlock()
return c.countSave
}

func TestStart(t *testing.T) {
Expand Down Expand Up @@ -69,9 +78,9 @@ func TestStart(t *testing.T) {
})
assert.NoError(err)

assert.Equal(0, globalOutput.CountSave)
assert.Equal(0, globalOutput.Get())
allOutput.Save(nodes)
assert.Equal(3, globalOutput.CountSave)
assert.Equal(3, globalOutput.Get())

_, err = Register(map[string]interface{}{
"e": []map[string]interface{}{
Expand Down
19 changes: 14 additions & 5 deletions output/internal_test.go
@@ -1,6 +1,7 @@
package output

import (
"sync"
"testing"
"time"

Expand All @@ -10,11 +11,19 @@ import (

type testConn struct {
Output
CountSave int
countSave int
sync.Mutex
}

func (c *testConn) Save(nodes *runtime.Nodes) {
c.CountSave++
c.Lock()
c.countSave++
c.Unlock()
}
func (c *testConn) Get() int {
c.Lock()
defer c.Unlock()
return c.countSave
}

func TestStart(t *testing.T) {
Expand All @@ -38,12 +47,12 @@ func TestStart(t *testing.T) {
Start(conn, nil, config)
assert.NotNil(quit)

assert.Equal(0, conn.CountSave)
assert.Equal(0, conn.Get())
time.Sleep(time.Millisecond * 12)
assert.Equal(1, conn.CountSave)
assert.Equal(1, conn.Get())

time.Sleep(time.Millisecond * 12)
Close()
assert.Equal(2, conn.CountSave)
assert.Equal(2, conn.Get())

}

0 comments on commit 9ecfa3d

Please sign in to comment.