Skip to content
This repository has been archived by the owner on Sep 15, 2020. It is now read-only.

Commit

Permalink
safer TestTicker without async problems
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Apr 30, 2018
1 parent 623c5b8 commit 7dc65e5
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions utils_test.go
Expand Up @@ -80,15 +80,31 @@ func TestUtilsDecodeFile(t *testing.T) {
}

func TestTicker(t *testing.T) {
counter := 0
counter := make(chan int)
i := 0
stopper := Ticker(10*time.Millisecond, func() {
counter += 1
})
time.Sleep(25 * time.Millisecond)
stopper <- true
Convey("it should have ticked twice", t, func() {
So(counter, ShouldEqual, 2)
counter <- i + 1
})

go func() {
c := <-counter

if c == 1 {
t.Log("it ticked once")
}

if c == 2 {
stopper <- true
t.Log("it ticked twice")
return
}

if c > 2 {
stopper <- true
t.Error("it ticked more than twice without ticking twice")
return
}
}()
}

func TestEncodingFormat(t *testing.T) {
Expand Down

0 comments on commit 7dc65e5

Please sign in to comment.