Skip to content

Instantly share code, notes, and snippets.

@mmcloughlin
Last active May 13, 2022 18:56
Show Gist options
  • Save mmcloughlin/6ec7d19a354ff2869350432f37628663 to your computer and use it in GitHub Desktop.
Save mmcloughlin/6ec7d19a354ff2869350432f37628663 to your computer and use it in GitHub Desktop.
package overallsrace
import "fmt"
func Greeting(who string) string {
return fmt.Sprintf("Hello %s!", who)
}
package overallsrace
import _cover_atomic_ "sync/atomic"
import "fmt"
func Greeting(who string) string {
_cover_atomic_.AddUint32(&GoCover.Count[0], 1)
return fmt.Sprintf("Hello %s!", who)
}
var _ = _cover_atomic_.AddUint32
var GoCover = struct {
Count [1]uint32
Pos [3 * 1]uint32
NumStmt [1]uint16
} {
Pos: [3 * 1]uint32{
5, 7, 0x20022, // [0]
},
NumStmt: [1]uint16{
1, // 0
},
}
package overallsrace
import "fmt"
func Greeting(who string) string {
GoCover.Count[0]++
return fmt.Sprintf("Hello %s!", who)
}
var GoCover = struct {
Count [1]uint32
Pos [3 * 1]uint32
NumStmt [1]uint16
} {
Pos: [3 * 1]uint32{
5, 7, 0x20022, // [0]
},
NumStmt: [1]uint16{
1, // 0
},
}
package overallsrace
import (
"sync"
"testing"
)
func TestGreeting(t *testing.T) {
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
g := Greeting("World")
if g != "Hello World!" {
t.Error("incorrect greeting")
}
wg.Done()
}()
}
wg.Wait()
}

Testing with overalls gives a race

$ overalls -project=github.com/mmcloughlin/overallsrace -- -v -race
| github.com/mmcloughlin/overallsrace
Test args: [test -v -race -covermode=count -coverprofile=profile.coverprofile -outputdir=.../src/github.com/mmcloughlin/overallsrace/ ./.]
2017/04/25 21:33:07 === RUN   TestGreeting
2017/04/25 21:33:07 ==================
2017/04/25 21:33:07 WARNING: DATA RACE
                    ...
2017/04/25 21:33:07 ==================
2017/04/25 21:33:07 --- PASS: TestGreeting (0.00s)
2017/04/25 21:33:07 PASS
2017/04/25 21:33:07 coverage: 100.0% of statements
2017/04/25 21:33:08 Found 1 data race(s)
2017/04/25 21:33:08 exit status 66
2017/04/25 21:33:08 FAIL	github.com/mmcloughlin/overallsrace	1.032s
2017/04/25 21:33:08 ERROR:exit status 1

I believe this is because of -covermode=count

$ go test -v -race -covermode=count
=== RUN   TestGreeting
==================
WARNING: DATA RACE
...
==================
--- PASS: TestGreeting (0.00s)
PASS
coverage: 100.0% of statements
Found 1 data race(s)
exit status 66
FAIL	github.com/mmcloughlin/overallsrace	1.032s

Using atomic removes the race

$ go test -v -race -covermode=atomic
=== RUN   TestGreeting
--- PASS: TestGreeting (0.00s)
PASS
coverage: 100.0% of statements
ok  	github.com/mmcloughlin/overallsrace	1.030s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment