Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
bc883bd0
Commit
bc883bd0
authored
Sep 24, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'ipfs-context-2'
parents
ac7404a6
63a2855a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
0 deletions
+63
-0
util/context.go
util/context.go
+35
-0
util/context_test.go
util/context_test.go
+28
-0
No files found.
util/context.go
0 → 100644
View file @
bc883bd0
package
util
import
(
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
// privateChanType protects the channel. Since this is a package-private type,
// only methods defined in this package can get the error value from the
// context.
type
privateChanType
chan
error
const
errLogKey
=
"the key used to extract the error log from the context"
// ContextWithErrorLog returns a copy of parent and an error channel that can
// be used to receive errors sent with the LogError method.
func
ContextWithErrorLog
(
parent
context
.
Context
)
(
context
.
Context
,
<-
chan
error
)
{
errs
:=
make
(
privateChanType
)
ctx
:=
context
.
WithValue
(
parent
,
errLogKey
,
errs
)
return
ctx
,
errs
}
// LogError logs the error to the owner of the context.
//
// If this context was created with ContextWithErrorLog, then this method
// passes the error to context creator over an unbuffered channel.
//
// If this context was created by other means, this method is a no-op.
func
LogError
(
ctx
context
.
Context
,
err
error
)
{
v
:=
ctx
.
Value
(
errLogKey
)
errs
,
ok
:=
v
.
(
privateChanType
)
if
!
ok
{
return
}
errs
<-
err
}
util/context_test.go
0 → 100644
View file @
bc883bd0
package
util
import
(
"errors"
"testing"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
func
TestLogErrorDoesNotBlockWhenCtxIsNotSetUpForLogging
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
LogError
(
ctx
,
errors
.
New
(
"ignore me"
))
}
func
TestLogErrorReceivedByParent
(
t
*
testing
.
T
)
{
expected
:=
errors
.
New
(
"From child to parent"
)
ctx
,
errs
:=
ContextWithErrorLog
(
context
.
Background
())
go
func
()
{
LogError
(
ctx
,
expected
)
}()
if
err
:=
<-
errs
;
err
!=
expected
{
t
.
Fatal
(
"didn't receive the expected error"
)
}
}
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