Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 1 | package common |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | var ( |
| 10 | handler *IntervalHandler |
| 11 | iteration int = 0 |
| 12 | interval int = 2 |
| 13 | ) |
| 14 | |
| 15 | func RepeatMessage() { |
| 16 | fmt.Printf("Ran the function %d times\n", iteration) |
| 17 | iteration += 1 |
| 18 | } |
| 19 | |
| 20 | func TestNewIntervalHandler(t *testing.T) { |
| 21 | handler = NewIntervalHandler(interval, RepeatMessage) |
| 22 | |
| 23 | if handler.state != STOPPED { |
| 24 | t.Error("The handler should be in STOPPED state", handler.state) |
| 25 | } |
| 26 | if handler.Interval != interval { |
| 27 | t.Error("The handler interval doesn't match the configured value", handler.Interval) |
| 28 | } |
| 29 | if handler.function == nil { |
| 30 | t.Error("The handler does not have function configured function", handler.function) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestIntervalHandler_Start(t *testing.T) { |
| 35 | handler.Start() |
| 36 | |
| 37 | time.Sleep(5 * time.Second) |
| 38 | |
| 39 | if handler.state != STARTED { |
| 40 | t.Error("The handler should be in STARTED state", handler.state) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestIntervalHandler_Pause(t *testing.T) { |
| 45 | handler.Pause() |
| 46 | |
| 47 | if handler.state != PAUSED { |
| 48 | t.Error("The handler should be in PAUSED state", handler.state) |
| 49 | } |
| 50 | |
| 51 | time.Sleep(5 * time.Second) |
| 52 | } |
| 53 | |
| 54 | func TestIntervalHandler_Resume(t *testing.T) { |
| 55 | handler.Resume() |
| 56 | |
| 57 | time.Sleep(5 * time.Second) |
| 58 | |
| 59 | if handler.state != STARTED { |
| 60 | t.Error("The handler should be in STARTED state", handler.state) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestIntervalHandler_Stop(t *testing.T) { |
| 65 | handler.Stop() |
| 66 | |
| 67 | if handler.state != STOPPED { |
| 68 | t.Error("The handler should be in STOPPED state", handler.state) |
| 69 | } |
| 70 | |
| 71 | time.Sleep(5 * time.Second) |
| 72 | } |