forked from topfreegames/khan
/
extensions.go
42 lines (34 loc) · 853 Bytes
/
extensions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// khan
// https://github.com/topfreegames/khan
//
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license
// Copyright © 2016 Top Free Games <backend@tfgco.com>
package testing
import (
"fmt"
"time"
"github.com/onsi/ginkgo"
)
//BeforeOnce runs the before each block only once
func BeforeOnce(beforeBlock func()) {
hasRun := false
ginkgo.BeforeEach(func() {
if !hasRun {
beforeBlock()
hasRun = true
}
})
}
//WaitForFunc waits for a given function to finish without error or a timeout
func WaitForFunc(timeout int, f func() error) error {
var err error
start := time.Now()
for err = f(); err != nil || int(time.Now().Sub(start).Seconds()) > timeout; err = f() {
time.Sleep(time.Millisecond)
}
if int(time.Now().Sub(start).Seconds()) > timeout {
return fmt.Errorf("Timeout")
}
return nil
}