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
ec96a0b0
Commit
ec96a0b0
authored
Dec 20, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
util: fractional context
parent
cc7a869e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
159 additions
and
0 deletions
+159
-0
util/ctx/fracctx.go
util/ctx/fracctx.go
+22
-0
util/ctx/fracctx_test.go
util/ctx/fracctx_test.go
+137
-0
No files found.
util/ctx/fracctx.go
0 → 100644
View file @
ec96a0b0
package
ctxutil
import
(
"time"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
func
WithDeadlineFraction
(
ctx
context
.
Context
,
fraction
float64
)
(
context
.
Context
,
context
.
CancelFunc
)
{
d
,
found
:=
ctx
.
Deadline
()
if
!
found
{
// no deadline
return
context
.
WithCancel
(
ctx
)
}
left
:=
d
.
Sub
(
time
.
Now
())
if
left
<
0
{
// already passed...
return
context
.
WithCancel
(
ctx
)
}
left
=
time
.
Duration
(
float64
(
left
)
*
fraction
)
return
context
.
WithTimeout
(
ctx
,
left
)
}
util/ctx/fracctx_test.go
0 → 100644
View file @
ec96a0b0
package
ctxutil
import
(
"testing"
"time"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
// this test is on the context tool itself, not our stuff. it's for sanity on ours.
func
TestDeadline
(
t
*
testing
.
T
)
{
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
5
*
time
.
Millisecond
)
select
{
case
<-
ctx
.
Done
()
:
t
.
Fatal
(
"ended too early"
)
default
:
}
<-
time
.
After
(
6
*
time
.
Millisecond
)
select
{
case
<-
ctx
.
Done
()
:
default
:
t
.
Fatal
(
"ended too late"
)
}
}
func
TestDeadlineFractionForever
(
t
*
testing
.
T
)
{
ctx
,
_
:=
WithDeadlineFraction
(
context
.
Background
(),
0.5
)
_
,
found
:=
ctx
.
Deadline
()
if
found
{
t
.
Fatal
(
"should last forever"
)
}
}
func
TestDeadlineFractionHalf
(
t
*
testing
.
T
)
{
ctx1
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Millisecond
)
ctx2
,
_
:=
WithDeadlineFraction
(
ctx1
,
0.5
)
select
{
case
<-
ctx1
.
Done
()
:
t
.
Fatal
(
"ctx1 ended too early"
)
case
<-
ctx2
.
Done
()
:
t
.
Fatal
(
"ctx2 ended too early"
)
default
:
}
<-
time
.
After
(
2
*
time
.
Millisecond
)
select
{
case
<-
ctx1
.
Done
()
:
t
.
Fatal
(
"ctx1 ended too early"
)
case
<-
ctx2
.
Done
()
:
t
.
Fatal
(
"ctx2 ended too early"
)
default
:
}
<-
time
.
After
(
4
*
time
.
Millisecond
)
select
{
case
<-
ctx1
.
Done
()
:
t
.
Fatal
(
"ctx1 ended too early"
)
case
<-
ctx2
.
Done
()
:
default
:
t
.
Fatal
(
"ctx2 ended too late"
)
}
<-
time
.
After
(
6
*
time
.
Millisecond
)
select
{
case
<-
ctx1
.
Done
()
:
default
:
t
.
Fatal
(
"ctx1 ended too late"
)
}
}
func
TestDeadlineFractionCancel
(
t
*
testing
.
T
)
{
ctx1
,
cancel1
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Millisecond
)
ctx2
,
cancel2
:=
WithDeadlineFraction
(
ctx1
,
0.5
)
select
{
case
<-
ctx1
.
Done
()
:
t
.
Fatal
(
"ctx1 ended too early"
)
case
<-
ctx2
.
Done
()
:
t
.
Fatal
(
"ctx2 ended too early"
)
default
:
}
cancel2
()
select
{
case
<-
ctx1
.
Done
()
:
t
.
Fatal
(
"ctx1 should NOT be cancelled"
)
case
<-
ctx2
.
Done
()
:
default
:
t
.
Fatal
(
"ctx2 should be cancelled"
)
}
cancel1
()
select
{
case
<-
ctx1
.
Done
()
:
case
<-
ctx2
.
Done
()
:
default
:
t
.
Fatal
(
"ctx1 should be cancelled"
)
}
}
func
TestDeadlineFractionObeysParent
(
t
*
testing
.
T
)
{
ctx1
,
cancel1
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Millisecond
)
ctx2
,
_
:=
WithDeadlineFraction
(
ctx1
,
0.5
)
select
{
case
<-
ctx1
.
Done
()
:
t
.
Fatal
(
"ctx1 ended too early"
)
case
<-
ctx2
.
Done
()
:
t
.
Fatal
(
"ctx2 ended too early"
)
default
:
}
cancel1
()
select
{
case
<-
ctx2
.
Done
()
:
default
:
t
.
Fatal
(
"ctx2 should be cancelled"
)
}
}
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