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
7aaf76af
Commit
7aaf76af
authored
Dec 03, 2014
by
Daniel Smith
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2661 from smarterclayton/allow_loops_to_exit
Add a util.Forever variant that ends on a stop channel
parents
b1c78939
740b824a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
0 deletions
+32
-0
util.go
pkg/util/util.go
+12
-0
util_test.go
pkg/util/util_test.go
+20
-0
No files found.
pkg/util/util.go
View file @
7aaf76af
...
@@ -51,7 +51,19 @@ func HandleCrash() {
...
@@ -51,7 +51,19 @@ func HandleCrash() {
// Forever loops forever running f every d. Catches any panics, and keeps going.
// Forever loops forever running f every d. Catches any panics, and keeps going.
func
Forever
(
f
func
(),
period
time
.
Duration
)
{
func
Forever
(
f
func
(),
period
time
.
Duration
)
{
Until
(
f
,
period
,
nil
)
}
// Until loops until stop channel is closed, running f every d.
// Catches any panics, and keeps going. f may not be invoked if
// stop channel is already closed.
func
Until
(
f
func
(),
period
time
.
Duration
,
stopCh
<-
chan
struct
{})
{
for
{
for
{
select
{
case
<-
stopCh
:
return
default
:
}
func
()
{
func
()
{
defer
HandleCrash
()
defer
HandleCrash
()
f
()
f
()
...
...
pkg/util/util_test.go
View file @
7aaf76af
...
@@ -24,6 +24,26 @@ import (
...
@@ -24,6 +24,26 @@ import (
"github.com/ghodss/yaml"
"github.com/ghodss/yaml"
)
)
func
TestUntil
(
t
*
testing
.
T
)
{
ch
:=
make
(
chan
struct
{})
close
(
ch
)
Until
(
func
()
{
t
.
Fatal
(
"should not have been invoked"
)
},
0
,
ch
)
ch
=
make
(
chan
struct
{})
called
:=
make
(
chan
struct
{})
go
func
()
{
Until
(
func
()
{
called
<-
struct
{}{}
},
0
,
ch
)
close
(
called
)
}()
<-
called
close
(
ch
)
<-
called
}
func
TestHandleCrash
(
t
*
testing
.
T
)
{
func
TestHandleCrash
(
t
*
testing
.
T
)
{
count
:=
0
count
:=
0
expect
:=
10
expect
:=
10
...
...
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