-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #272 from jbenet/context-dial-peer
add context to DialPeer interface
- Loading branch information
Showing
12 changed files
with
118 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package util | ||
|
||
import "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" | ||
|
||
func ContextDo(ctx context.Context, f func() error) error { | ||
|
||
ch := make(chan error) | ||
|
||
go func() { | ||
select { | ||
case <-ctx.Done(): | ||
case ch <- f(): | ||
} | ||
}() | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case val := <-ch: | ||
return val | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package util | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" | ||
) | ||
|
||
func TestDoReturnsContextErr(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
ch := make(chan struct{}) | ||
err := ContextDo(ctx, func() error { | ||
cancel() | ||
ch <- struct{}{} // won't return | ||
return nil | ||
}) | ||
if err != ctx.Err() { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestDoReturnsFuncError(t *testing.T) { | ||
ctx := context.Background() | ||
expected := errors.New("expected to be returned by ContextDo") | ||
err := ContextDo(ctx, func() error { | ||
return expected | ||
}) | ||
if err != expected { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestDoReturnsNil(t *testing.T) { | ||
ctx := context.Background() | ||
err := ContextDo(ctx, func() error { | ||
return nil | ||
}) | ||
if err != nil { | ||
t.Fail() | ||
} | ||
} |