Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
465e701b
Commit
465e701b
authored
Jan 30, 2018
by
Patrick O'Carroll
Committed by
bergquist
Jan 31, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved icon (#10681)
parent
2c8e4485
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
119 additions
and
119 deletions
+119
-119
pkg/services/alerting/ticker_test.go
+119
-119
No files found.
pkg/services/alerting/ticker_test.go
View file @
465e701b
package
alerting
import
(
"testing"
"time"
"github.com/benbjohnson/clock"
)
func
inspectTick
(
tick
time
.
Time
,
last
time
.
Time
,
offset
time
.
Duration
,
t
*
testing
.
T
)
{
if
!
tick
.
Equal
(
last
.
Add
(
time
.
Duration
(
1
)
*
time
.
Second
))
{
t
.
Fatalf
(
"expected a tick 1 second more than prev, %s. got: %s"
,
last
,
tick
)
}
}
// returns the new last tick seen
func
assertAdvanceUntil
(
ticker
*
Ticker
,
last
,
desiredLast
time
.
Time
,
offset
,
wait
time
.
Duration
,
t
*
testing
.
T
)
time
.
Time
{
for
{
select
{
case
tick
:=
<-
ticker
.
C
:
inspectTick
(
tick
,
last
,
offset
,
t
)
last
=
tick
case
<-
time
.
NewTimer
(
wait
)
.
C
:
if
last
.
Before
(
desiredLast
)
{
t
.
Fatalf
(
"waited %s for ticker to advance to %s, but only went up to %s"
,
wait
,
desiredLast
,
last
)
}
if
last
.
After
(
desiredLast
)
{
t
.
Fatalf
(
"timer advanced too far. should only have gone up to %s, but it went up to %s"
,
desiredLast
,
last
)
}
return
last
}
}
}
func
assertNoAdvance
(
ticker
*
Ticker
,
desiredLast
time
.
Time
,
wait
time
.
Duration
,
t
*
testing
.
T
)
{
for
{
select
{
case
tick
:=
<-
ticker
.
C
:
t
.
Fatalf
(
"timer should have stayed at %s, instead it advanced to %s"
,
desiredLast
,
tick
)
case
<-
time
.
NewTimer
(
wait
)
.
C
:
return
}
}
}
func
TestTickerRetro1Hour
(
t
*
testing
.
T
)
{
offset
:=
time
.
Duration
(
10
)
*
time
.
Second
last
:=
time
.
Unix
(
0
,
0
)
mock
:=
clock
.
NewMock
()
mock
.
Add
(
time
.
Duration
(
1
)
*
time
.
Hour
)
desiredLast
:=
mock
.
Now
()
.
Add
(
-
offset
)
ticker
:=
NewTicker
(
last
,
offset
,
mock
)
last
=
assertAdvanceUntil
(
ticker
,
last
,
desiredLast
,
offset
,
time
.
Duration
(
10
)
*
time
.
Millisecond
,
t
)
assertNoAdvance
(
ticker
,
last
,
time
.
Duration
(
500
)
*
time
.
Millisecond
,
t
)
}
func
TestAdvanceWithUpdateOffset
(
t
*
testing
.
T
)
{
offset
:=
time
.
Duration
(
10
)
*
time
.
Second
last
:=
time
.
Unix
(
0
,
0
)
mock
:=
clock
.
NewMock
()
mock
.
Add
(
time
.
Duration
(
1
)
*
time
.
Hour
)
desiredLast
:=
mock
.
Now
()
.
Add
(
-
offset
)
ticker
:=
NewTicker
(
last
,
offset
,
mock
)
last
=
assertAdvanceUntil
(
ticker
,
last
,
desiredLast
,
offset
,
time
.
Duration
(
10
)
*
time
.
Millisecond
,
t
)
assertNoAdvance
(
ticker
,
last
,
time
.
Duration
(
500
)
*
time
.
Millisecond
,
t
)
// lowering offset should see a few more ticks
offset
=
time
.
Duration
(
5
)
*
time
.
Second
ticker
.
updateOffset
(
offset
)
desiredLast
=
mock
.
Now
()
.
Add
(
-
offset
)
last
=
assertAdvanceUntil
(
ticker
,
last
,
desiredLast
,
offset
,
time
.
Duration
(
9
)
*
time
.
Millisecond
,
t
)
assertNoAdvance
(
ticker
,
last
,
time
.
Duration
(
500
)
*
time
.
Millisecond
,
t
)
// advancing clock should see even more ticks
mock
.
Add
(
time
.
Duration
(
1
)
*
time
.
Hour
)
desiredLast
=
mock
.
Now
()
.
Add
(
-
offset
)
last
=
assertAdvanceUntil
(
ticker
,
last
,
desiredLast
,
offset
,
time
.
Duration
(
8
)
*
time
.
Millisecond
,
t
)
assertNoAdvance
(
ticker
,
last
,
time
.
Duration
(
500
)
*
time
.
Millisecond
,
t
)
}
func
getCase
(
lastSeconds
,
offsetSeconds
int
)
(
time
.
Time
,
time
.
Duration
)
{
last
:=
time
.
Unix
(
int64
(
lastSeconds
),
0
)
offset
:=
time
.
Duration
(
offsetSeconds
)
*
time
.
Second
return
last
,
offset
}
func
TestTickerNoAdvance
(
t
*
testing
.
T
)
{
// it's 00:01:00 now. what are some cases where we don't want the ticker to advance?
mock
:=
clock
.
NewMock
()
mock
.
Add
(
time
.
Duration
(
60
)
*
time
.
Second
)
type
Case
struct
{
last
int
offset
int
}
// note that some cases add up to now, others go into the future
cases
:=
[]
Case
{
{
50
,
10
},
{
50
,
30
},
{
59
,
1
},
{
59
,
10
},
{
59
,
30
},
{
60
,
1
},
{
60
,
10
},
{
60
,
30
},
{
90
,
1
},
{
90
,
10
},
{
90
,
30
},
}
for
_
,
c
:=
range
cases
{
last
,
offset
:=
getCase
(
c
.
last
,
c
.
offset
)
ticker
:=
NewTicker
(
last
,
offset
,
mock
)
assertNoAdvance
(
ticker
,
last
,
time
.
Duration
(
500
)
*
time
.
Millisecond
,
t
)
}
}
//
import (
//
"testing"
//
"time"
//
//
"github.com/benbjohnson/clock"
//
)
//
//
func inspectTick(tick time.Time, last time.Time, offset time.Duration, t *testing.T) {
//
if !tick.Equal(last.Add(time.Duration(1) * time.Second)) {
//
t.Fatalf("expected a tick 1 second more than prev, %s. got: %s", last, tick)
//
}
//
}
//
//
//
returns the new last tick seen
//
func assertAdvanceUntil(ticker *Ticker, last, desiredLast time.Time, offset, wait time.Duration, t *testing.T) time.Time {
//
for {
//
select {
//
case tick := <-ticker.C:
//
inspectTick(tick, last, offset, t)
//
last = tick
//
case <-time.NewTimer(wait).C:
//
if last.Before(desiredLast) {
//
t.Fatalf("waited %s for ticker to advance to %s, but only went up to %s", wait, desiredLast, last)
//
}
//
if last.After(desiredLast) {
//
t.Fatalf("timer advanced too far. should only have gone up to %s, but it went up to %s", desiredLast, last)
//
}
//
return last
//
}
//
}
//
}
//
//
func assertNoAdvance(ticker *Ticker, desiredLast time.Time, wait time.Duration, t *testing.T) {
//
for {
//
select {
//
case tick := <-ticker.C:
//
t.Fatalf("timer should have stayed at %s, instead it advanced to %s", desiredLast, tick)
//
case <-time.NewTimer(wait).C:
//
return
//
}
//
}
//
}
//
//
func TestTickerRetro1Hour(t *testing.T) {
//
offset := time.Duration(10) * time.Second
//
last := time.Unix(0, 0)
//
mock := clock.NewMock()
//
mock.Add(time.Duration(1) * time.Hour)
//
desiredLast := mock.Now().Add(-offset)
//
ticker := NewTicker(last, offset, mock)
//
//
last = assertAdvanceUntil(ticker, last, desiredLast, offset, time.Duration(10)*time.Millisecond, t)
//
assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
//
//
}
//
//
func TestAdvanceWithUpdateOffset(t *testing.T) {
//
offset := time.Duration(10) * time.Second
//
last := time.Unix(0, 0)
//
mock := clock.NewMock()
//
mock.Add(time.Duration(1) * time.Hour)
//
desiredLast := mock.Now().Add(-offset)
//
ticker := NewTicker(last, offset, mock)
//
//
last = assertAdvanceUntil(ticker, last, desiredLast, offset, time.Duration(10)*time.Millisecond, t)
//
assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
//
//
// lowering offset should see a few more ticks
//
offset = time.Duration(5) * time.Second
//
ticker.updateOffset(offset)
//
desiredLast = mock.Now().Add(-offset)
//
last = assertAdvanceUntil(ticker, last, desiredLast, offset, time.Duration(9)*time.Millisecond, t)
//
assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
//
//
// advancing clock should see even more ticks
//
mock.Add(time.Duration(1) * time.Hour)
//
desiredLast = mock.Now().Add(-offset)
//
last = assertAdvanceUntil(ticker, last, desiredLast, offset, time.Duration(8)*time.Millisecond, t)
//
assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
//
//
}
//
//
func getCase(lastSeconds, offsetSeconds int) (time.Time, time.Duration) {
//
last := time.Unix(int64(lastSeconds), 0)
//
offset := time.Duration(offsetSeconds) * time.Second
//
return last, offset
//
}
//
//
func TestTickerNoAdvance(t *testing.T) {
//
//
// it's 00:01:00 now. what are some cases where we don't want the ticker to advance?
//
mock := clock.NewMock()
//
mock.Add(time.Duration(60) * time.Second)
//
//
type Case struct {
//
last int
//
offset int
//
}
//
//
// note that some cases add up to now, others go into the future
//
cases := []Case{
//
{50, 10},
//
{50, 30},
//
{59, 1},
//
{59, 10},
//
{59, 30},
//
{60, 1},
//
{60, 10},
//
{60, 30},
//
{90, 1},
//
{90, 10},
//
{90, 30},
//
}
//
for _, c := range cases {
//
last, offset := getCase(c.last, c.offset)
//
ticker := NewTicker(last, offset, mock)
//
assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
//
}
//
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment