Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
bf097ea2
Commit
bf097ea2
authored
Apr 06, 2016
by
Jordan Liggitt
Committed by
deads2k
Apr 07, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fake util.clock tick
parent
290d9702
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
127 additions
and
5 deletions
+127
-5
clock.go
pkg/util/clock.go
+48
-4
clock_test.go
pkg/util/clock_test.go
+78
-0
delaying_queue.go
pkg/util/workqueue/delaying_queue.go
+1
-1
No files found.
pkg/util/clock.go
View file @
bf097ea2
...
@@ -28,6 +28,7 @@ type Clock interface {
...
@@ -28,6 +28,7 @@ type Clock interface {
Since
(
time
.
Time
)
time
.
Duration
Since
(
time
.
Time
)
time
.
Duration
After
(
d
time
.
Duration
)
<-
chan
time
.
Time
After
(
d
time
.
Duration
)
<-
chan
time
.
Time
Sleep
(
d
time
.
Duration
)
Sleep
(
d
time
.
Duration
)
Tick
(
d
time
.
Duration
)
<-
chan
time
.
Time
}
}
var
(
var
(
...
@@ -54,6 +55,10 @@ func (RealClock) After(d time.Duration) <-chan time.Time {
...
@@ -54,6 +55,10 @@ func (RealClock) After(d time.Duration) <-chan time.Time {
return
time
.
After
(
d
)
return
time
.
After
(
d
)
}
}
func
(
RealClock
)
Tick
(
d
time
.
Duration
)
<-
chan
time
.
Time
{
return
time
.
Tick
(
d
)
}
func
(
RealClock
)
Sleep
(
d
time
.
Duration
)
{
func
(
RealClock
)
Sleep
(
d
time
.
Duration
)
{
time
.
Sleep
(
d
)
time
.
Sleep
(
d
)
}
}
...
@@ -68,8 +73,10 @@ type FakeClock struct {
...
@@ -68,8 +73,10 @@ type FakeClock struct {
}
}
type
fakeClockWaiter
struct
{
type
fakeClockWaiter
struct
{
targetTime
time
.
Time
targetTime
time
.
Time
destChan
chan
<-
time
.
Time
stepInterval
time
.
Duration
skipIfBlocked
bool
destChan
chan
<-
time
.
Time
}
}
func
NewFakeClock
(
t
time
.
Time
)
*
FakeClock
{
func
NewFakeClock
(
t
time
.
Time
)
*
FakeClock
{
...
@@ -105,7 +112,22 @@ func (f *FakeClock) After(d time.Duration) <-chan time.Time {
...
@@ -105,7 +112,22 @@ func (f *FakeClock) After(d time.Duration) <-chan time.Time {
return
ch
return
ch
}
}
// Move clock by Duration, notify anyone that's called After
func
(
f
*
FakeClock
)
Tick
(
d
time
.
Duration
)
<-
chan
time
.
Time
{
f
.
lock
.
Lock
()
defer
f
.
lock
.
Unlock
()
tickTime
:=
f
.
time
.
Add
(
d
)
ch
:=
make
(
chan
time
.
Time
,
1
)
// hold one tick
f
.
waiters
=
append
(
f
.
waiters
,
fakeClockWaiter
{
targetTime
:
tickTime
,
stepInterval
:
d
,
skipIfBlocked
:
true
,
destChan
:
ch
,
})
return
ch
}
// Move clock by Duration, notify anyone that's called After or Tick
func
(
f
*
FakeClock
)
Step
(
d
time
.
Duration
)
{
func
(
f
*
FakeClock
)
Step
(
d
time
.
Duration
)
{
f
.
lock
.
Lock
()
f
.
lock
.
Lock
()
defer
f
.
lock
.
Unlock
()
defer
f
.
lock
.
Unlock
()
...
@@ -126,7 +148,23 @@ func (f *FakeClock) setTimeLocked(t time.Time) {
...
@@ -126,7 +148,23 @@ func (f *FakeClock) setTimeLocked(t time.Time) {
for
i
:=
range
f
.
waiters
{
for
i
:=
range
f
.
waiters
{
w
:=
&
f
.
waiters
[
i
]
w
:=
&
f
.
waiters
[
i
]
if
!
w
.
targetTime
.
After
(
t
)
{
if
!
w
.
targetTime
.
After
(
t
)
{
w
.
destChan
<-
t
if
w
.
skipIfBlocked
{
select
{
case
w
.
destChan
<-
t
:
default
:
}
}
else
{
w
.
destChan
<-
t
}
if
w
.
stepInterval
>
0
{
for
!
w
.
targetTime
.
After
(
t
)
{
w
.
targetTime
=
w
.
targetTime
.
Add
(
w
.
stepInterval
)
}
newWaiters
=
append
(
newWaiters
,
*
w
)
}
}
else
{
}
else
{
newWaiters
=
append
(
newWaiters
,
f
.
waiters
[
i
])
newWaiters
=
append
(
newWaiters
,
f
.
waiters
[
i
])
}
}
...
@@ -169,6 +207,12 @@ func (*IntervalClock) After(d time.Duration) <-chan time.Time {
...
@@ -169,6 +207,12 @@ func (*IntervalClock) After(d time.Duration) <-chan time.Time {
panic
(
"IntervalClock doesn't implement After"
)
panic
(
"IntervalClock doesn't implement After"
)
}
}
// Unimplemented, will panic.
// TODO: make interval clock use FakeClock so this can be implemented.
func
(
*
IntervalClock
)
Tick
(
d
time
.
Duration
)
<-
chan
time
.
Time
{
panic
(
"IntervalClock doesn't implement Tick"
)
}
func
(
*
IntervalClock
)
Sleep
(
d
time
.
Duration
)
{
func
(
*
IntervalClock
)
Sleep
(
d
time
.
Duration
)
{
panic
(
"IntervalClock doesn't implement Sleep"
)
panic
(
"IntervalClock doesn't implement Sleep"
)
}
}
pkg/util/clock_test.go
View file @
bf097ea2
...
@@ -104,3 +104,81 @@ func TestFakeAfter(t *testing.T) {
...
@@ -104,3 +104,81 @@ func TestFakeAfter(t *testing.T) {
t
.
Errorf
(
"unexpected non-channel read"
)
t
.
Errorf
(
"unexpected non-channel read"
)
}
}
}
}
func
TestFakeTick
(
t
*
testing
.
T
)
{
tc
:=
NewFakeClock
(
time
.
Now
())
if
tc
.
HasWaiters
()
{
t
.
Errorf
(
"unexpected waiter?"
)
}
oneSec
:=
tc
.
Tick
(
time
.
Second
)
if
!
tc
.
HasWaiters
()
{
t
.
Errorf
(
"unexpected lack of waiter?"
)
}
oneOhOneSec
:=
tc
.
Tick
(
time
.
Second
+
time
.
Millisecond
)
twoSec
:=
tc
.
Tick
(
2
*
time
.
Second
)
select
{
case
<-
oneSec
:
t
.
Errorf
(
"unexpected channel read"
)
case
<-
oneOhOneSec
:
t
.
Errorf
(
"unexpected channel read"
)
case
<-
twoSec
:
t
.
Errorf
(
"unexpected channel read"
)
default
:
}
tc
.
Step
(
999
*
time
.
Millisecond
)
// t=.999
select
{
case
<-
oneSec
:
t
.
Errorf
(
"unexpected channel read"
)
case
<-
oneOhOneSec
:
t
.
Errorf
(
"unexpected channel read"
)
case
<-
twoSec
:
t
.
Errorf
(
"unexpected channel read"
)
default
:
}
tc
.
Step
(
time
.
Millisecond
)
// t=1.000
select
{
case
<-
oneSec
:
// Expected!
case
<-
oneOhOneSec
:
t
.
Errorf
(
"unexpected channel read"
)
case
<-
twoSec
:
t
.
Errorf
(
"unexpected channel read"
)
default
:
t
.
Errorf
(
"unexpected non-channel read"
)
}
tc
.
Step
(
time
.
Millisecond
)
// t=1.001
select
{
case
<-
oneSec
:
// should not double-trigger!
t
.
Errorf
(
"unexpected channel read"
)
case
<-
oneOhOneSec
:
// Expected!
case
<-
twoSec
:
t
.
Errorf
(
"unexpected channel read"
)
default
:
t
.
Errorf
(
"unexpected non-channel read"
)
}
tc
.
Step
(
time
.
Second
)
// t=2.001
tc
.
Step
(
time
.
Second
)
// t=3.001
tc
.
Step
(
time
.
Second
)
// t=4.001
tc
.
Step
(
time
.
Second
)
// t=5.001
// The one second ticker should not accumulate ticks
accumulatedTicks
:=
0
drained
:=
false
for
!
drained
{
select
{
case
<-
oneSec
:
accumulatedTicks
++
default
:
drained
=
true
}
}
if
accumulatedTicks
!=
1
{
t
.
Errorf
(
"unexpected number of accumulated ticks: %d"
,
accumulatedTicks
)
}
}
pkg/util/workqueue/delaying_queue.go
View file @
bf097ea2
...
@@ -41,7 +41,7 @@ func newDelayingQueue(clock util.Clock) DelayingInterface {
...
@@ -41,7 +41,7 @@ func newDelayingQueue(clock util.Clock) DelayingInterface {
ret
:=
&
delayingType
{
ret
:=
&
delayingType
{
Interface
:
New
(),
Interface
:
New
(),
clock
:
clock
,
clock
:
clock
,
heartbeat
:
time
.
Tick
(
maxWait
),
heartbeat
:
clock
.
Tick
(
maxWait
),
stopCh
:
make
(
chan
struct
{}),
stopCh
:
make
(
chan
struct
{}),
waitingForAddCh
:
make
(
chan
waitFor
,
1000
),
waitingForAddCh
:
make
(
chan
waitFor
,
1000
),
}
}
...
...
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