petergtz / pegomock

Pegomock is a powerful, yet simple mocking framework for the Go programming language
Apache License 2.0
253 stars 28 forks source link

Data race #55

Closed ghostiam closed 6 years ago

ghostiam commented 6 years ago

When testing a code that can call a function several times in parallel, we get a data race. Here is an example: https://github.com/GhostRussia/pegomockRaceBug

go test -race
==================
WARNING: DATA RACE
Read at 0x00c42007a210 by goroutine 8:
  runtime.mapaccess1()
      /usr/local/Cellar/go/1.9.2/libexec/src/runtime/hashmap.go:341 +0x0
  github.com/GhostRussia/pegomockRaceBug/mock.(*MockRaceBug).Test1()
      /Users/ghost_russia/projects/golang/src/github.com/petergtz/pegomock/dsl.go:419 +0x12b

Previous write at 0x00c42007a210 by goroutine 7:
  runtime.mapassign()
      /usr/local/Cellar/go/1.9.2/libexec/src/runtime/hashmap.go:521 +0x0
  github.com/GhostRussia/pegomockRaceBug/mock.(*MockRaceBug).Test1()
      /Users/ghost_russia/projects/golang/src/github.com/petergtz/pegomock/dsl.go:420 +0x2ec

Goroutine 8 (running) created at:
  github.com/GhostRussia/pegomockRaceBug.TestRaceBug1()
      /Users/ghost_russia/projects/golang/src/github.com/GhostRussia/pegomockRaceBug/racebug_test.go:15 +0xc8
  testing.tRunner()
      /usr/local/Cellar/go/1.9.2/libexec/src/testing/testing.go:746 +0x16c

Goroutine 7 (finished) created at:
  github.com/GhostRussia/pegomockRaceBug.TestRaceBug1()
      /Users/ghost_russia/projects/golang/src/github.com/GhostRussia/pegomockRaceBug/racebug_test.go:15 +0xc8
  testing.tRunner()
      /usr/local/Cellar/go/1.9.2/libexec/src/testing/testing.go:746 +0x16c
==================
ghostiam commented 6 years ago

Maybe it's worth adding mutexes to the mock generator?

fast fix: 2018-01-08 18 47 18