Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 1 | /* |
| 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 Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 16 | package common |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "testing" |
| 21 | "time" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | handler *IntervalHandler |
| 26 | iteration int = 0 |
| 27 | interval int = 2 |
| 28 | ) |
| 29 | |
| 30 | func RepeatMessage() { |
| 31 | fmt.Printf("Ran the function %d times\n", iteration) |
| 32 | iteration += 1 |
| 33 | } |
| 34 | |
| 35 | func 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 | |
| 49 | func 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 | |
| 59 | func 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 | |
| 69 | func 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 | |
| 79 | func 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 | } |