Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

De-race the tests; support concurrent Mock::Add; add Mock::Stop support #13

Closed
wants to merge 6 commits into from

Conversation

jmacd
Copy link

@jmacd jmacd commented Mar 28, 2018

I'm happy to contribute several improvements.

I removed the tests for the real clock because it's basically impossible to make them not racey. The test now passes w/ Go-1.10 under the race detector.

@3onyc
Copy link

3onyc commented Apr 18, 2018

There seems to still be a race in .Reset(), minimal test case:

package main

import (
        "github.com/jmacd/clock"
        "time"
)

func main() {
        cl := clock.NewMock()
        timer := cl.Timer(1 * time.Second)
        go func() {
                <-timer.C
                timer.Reset(2 * time.Second)
        }()
        cl.Add(2 * time.Second)
        <-time.After(5 * time.Second)
}

run with go run -race should fail most of the time

==================
WARNING: DATA RACE
Read at 0x00c4200a6038 by goroutine 5:
  github.com/jmacd/clock.(*Timer).Reset()
      ~/go/src/github.com/jmacd/clock/clock.go:311 +0xe9
  main.main.func1()
      ~/main.go:13 +0x69

Previous write at 0x00c4200a6038 by main goroutine:
  github.com/jmacd/clock.(*Timer).execute()
      ~/go/src/github.com/jmacd/clock/clock.go:330 +0x81
  github.com/jmacd/clock.(*Mock).Set()
      ~/go/src/github.com/jmacd/clock/clock.go:85 +0x49
  github.com/jmacd/clock.(*Mock).Add()
      ~/go/src/github.com/jmacd/clock/clock.go:76 +0x94
  main.main()
      ~/main.go:15 +0x128

Goroutine 5 (running) created at:
  main.main()
      ~/main.go:11 +0x111
==================
Found 1 data race(s)

@jmacd jmacd closed this Nov 15, 2018
@jmacd
Copy link
Author

jmacd commented Nov 15, 2018

Recommend https://github.com/jonboulle/clockwork

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants