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
2feb54ce
Commit
2feb54ce
authored
Oct 02, 2015
by
Alex Robinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add utility function for errors that runs multiple functions that errors
as goroutines, blocks until they're all done executing, and combines the results into an Aggregate error.
parent
cfdde6f7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
0 deletions
+80
-0
errors.go
pkg/util/errors/errors.go
+17
-0
errors_test.go
pkg/util/errors/errors_test.go
+63
-0
No files found.
pkg/util/errors/errors.go
View file @
2feb54ce
...
...
@@ -131,3 +131,20 @@ func Flatten(agg Aggregate) Aggregate {
}
return
NewAggregate
(
result
)
}
// AggregateGoroutines runs the provided functions in parallel, stuffing all
// non-nil errors into the returned Aggregate.
// Returns nil if all the functions complete successfully.
func
AggregateGoroutines
(
funcs
...
func
()
error
)
Aggregate
{
errChan
:=
make
(
chan
error
,
len
(
funcs
))
for
_
,
f
:=
range
funcs
{
go
func
(
f
func
()
error
)
{
errChan
<-
f
()
}(
f
)
}
errs
:=
make
([]
error
,
0
)
for
i
:=
0
;
i
<
cap
(
errChan
);
i
++
{
if
err
:=
<-
errChan
;
err
!=
nil
{
errs
=
append
(
errs
,
err
)
}
}
return
NewAggregate
(
errs
)
}
pkg/util/errors/errors_test.go
View file @
2feb54ce
...
...
@@ -221,3 +221,66 @@ func TestFlatten(t *testing.T) {
}
}
}
func
TestAggregateGoroutines
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
errs
[]
error
expected
map
[
string
]
bool
// can't compare directly to Aggregate due to non-deterministic ordering
}{
{
[]
error
{},
nil
,
},
{
[]
error
{
nil
},
nil
,
},
{
[]
error
{
nil
,
nil
},
nil
,
},
{
[]
error
{
fmt
.
Errorf
(
"1"
)},
map
[
string
]
bool
{
"1"
:
true
},
},
{
[]
error
{
fmt
.
Errorf
(
"1"
),
nil
},
map
[
string
]
bool
{
"1"
:
true
},
},
{
[]
error
{
fmt
.
Errorf
(
"1"
),
fmt
.
Errorf
(
"267"
)},
map
[
string
]
bool
{
"1"
:
true
,
"267"
:
true
},
},
{
[]
error
{
fmt
.
Errorf
(
"1"
),
nil
,
fmt
.
Errorf
(
"1234"
)},
map
[
string
]
bool
{
"1"
:
true
,
"1234"
:
true
},
},
{
[]
error
{
nil
,
fmt
.
Errorf
(
"1"
),
nil
,
fmt
.
Errorf
(
"1234"
),
fmt
.
Errorf
(
"22"
)},
map
[
string
]
bool
{
"1"
:
true
,
"1234"
:
true
,
"22"
:
true
},
},
}
for
i
,
testCase
:=
range
testCases
{
funcs
:=
make
([]
func
()
error
,
len
(
testCase
.
errs
))
for
i
:=
range
testCase
.
errs
{
err
:=
testCase
.
errs
[
i
]
funcs
[
i
]
=
func
()
error
{
return
err
}
}
agg
:=
AggregateGoroutines
(
funcs
...
)
if
agg
==
nil
{
if
len
(
testCase
.
expected
)
>
0
{
t
.
Errorf
(
"%d: expected %v, got nil"
,
i
,
testCase
.
expected
)
}
continue
}
if
len
(
agg
.
Errors
())
!=
len
(
testCase
.
expected
)
{
t
.
Errorf
(
"%d: expected %d errors in aggregate, got %v"
,
i
,
len
(
testCase
.
expected
),
agg
)
continue
}
for
_
,
err
:=
range
agg
.
Errors
()
{
if
!
testCase
.
expected
[
err
.
Error
()]
{
t
.
Errorf
(
"%d: expected %v, got aggregate containing %v"
,
i
,
testCase
.
expected
,
err
)
}
}
}
}
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