blob: c28cb4200d3f089225db980885fc118b3cb84b7b [file] [log] [blame]
Zack Williams41513bf2018-07-07 20:08:35 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Stephane Barbarie35595062018-02-08 08:34:39 -050016package common
17
18import (
19 "fmt"
20 "testing"
21 "time"
22)
23
24var (
25 handler *IntervalHandler
26 iteration int = 0
27 interval int = 2
28)
29
30func RepeatMessage() {
31 fmt.Printf("Ran the function %d times\n", iteration)
32 iteration += 1
33}
34
35func TestNewIntervalHandler(t *testing.T) {
36 handler = NewIntervalHandler(interval, RepeatMessage)
37
38 if handler.state != STOPPED {
39 t.Error("The handler should be in STOPPED state", handler.state)
40 }
41 if handler.Interval != interval {
42 t.Error("The handler interval doesn't match the configured value", handler.Interval)
43 }
44 if handler.function == nil {
45 t.Error("The handler does not have function configured function", handler.function)
46 }
47}
48
49func TestIntervalHandler_Start(t *testing.T) {
50 handler.Start()
51
52 time.Sleep(5 * time.Second)
53
54 if handler.state != STARTED {
55 t.Error("The handler should be in STARTED state", handler.state)
56 }
57}
58
59func TestIntervalHandler_Pause(t *testing.T) {
60 handler.Pause()
61
62 if handler.state != PAUSED {
63 t.Error("The handler should be in PAUSED state", handler.state)
64 }
65
66 time.Sleep(5 * time.Second)
67}
68
69func TestIntervalHandler_Resume(t *testing.T) {
70 handler.Resume()
71
72 time.Sleep(5 * time.Second)
73
74 if handler.state != STARTED {
75 t.Error("The handler should be in STARTED state", handler.state)
76 }
77}
78
79func TestIntervalHandler_Stop(t *testing.T) {
80 handler.Stop()
81
82 if handler.state != STOPPED {
83 t.Error("The handler should be in STOPPED state", handler.state)
84 }
85
86 time.Sleep(5 * time.Second)
87}