blob: e1f060fe398885792865670384409e9254ef31d0 [file] [log] [blame]
Stephane Barbarie35595062018-02-08 08:34:39 -05001package common
2
3import (
4 "fmt"
5 "testing"
6 "time"
7)
8
9var (
10 handler *IntervalHandler
11 iteration int = 0
12 interval int = 2
13)
14
15func RepeatMessage() {
16 fmt.Printf("Ran the function %d times\n", iteration)
17 iteration += 1
18}
19
20func 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
34func 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
44func 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
54func 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
64func 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}