Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neoppod
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Levin Zimmermann
neoppod
Commits
7602542c
Commit
7602542c
authored
May 25, 2018
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
00a3c085
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
5 deletions
+92
-5
go/xcommon/xcontext/xcontext.go
go/xcommon/xcontext/xcontext.go
+47
-0
go/xcommon/xcontext/xcontext_test.go
go/xcommon/xcontext/xcontext_test.go
+45
-5
No files found.
go/xcommon/xcontext/xcontext.go
View file @
7602542c
...
...
@@ -178,6 +178,53 @@ func (mc *mergeCtx) Value(key interface{}) interface{} {
return
mc
.
ctx2
.
Value
(
key
)
}
// ----------------------------------------
// chanCtx wraps channel into context.Context interface.
type
chanCtx
struct
{
done
<-
chan
struct
{}
}
// MergeChan merges context and channel into 1 context.
//
// MergeChan, similarly to Merge, provides resulting context which:
//
// - is done when ctx1 is done or done2 is closed, or cancel called, whichever happens first,
// - has the same deadline as ctx1,
// - has the same associated values as ctx1.
//
// Canceling this context releases resources associated with it, so code should
// call cancel as soon as the operations running in this Context complete.
func
MergeChan
(
ctx1
context
.
Context
,
done2
<-
chan
struct
{})
(
context
.
Context
,
context
.
CancelFunc
)
{
return
Merge
(
ctx1
,
chanCtx
{
done2
})
}
// Done implements context.Context .
func
(
c
chanCtx
)
Done
()
<-
chan
struct
{}
{
return
c
.
done
}
// Err implements context.Context .
func
(
c
chanCtx
)
Err
()
error
{
select
{
case
<-
c
.
done
:
return
context
.
Canceled
default
:
return
nil
}
}
// Deadline implements context.Context .
func
(
c
chanCtx
)
Deadline
()
(
time
.
Time
,
bool
)
{
return
time
.
Time
{},
false
}
// Value implements context.Context .
func
(
c
chanCtx
)
Value
(
key
interface
{})
interface
{}
{
return
nil
}
// Cancelled reports whether an error is due to a canceled context.
//
...
...
go/xcommon/xcontext/xcontext_test.go
View file @
7602542c
...
...
@@ -34,7 +34,7 @@ func TestMerge(t *testing.T) {
ctx1
=
context
.
WithValue
(
ctx1
,
1
,
"hello"
)
ctx2
=
context
.
WithValue
(
ctx2
,
2
,
"world"
)
mc
,
_
:=
Merge
(
ctx1
,
ctx2
)
mc
,
_
_
:=
Merge
(
ctx1
,
ctx2
);
defer
__
(
)
assertEq
:=
func
(
a
,
b
interface
{})
{
t
.
Helper
()
...
...
@@ -67,7 +67,7 @@ func TestMerge(t *testing.T) {
assertEq
(
mc
.
Err
(),
context
.
Canceled
)
////////
mc
,
_
=
Merge
(
ctx1
,
bg
)
mc
,
_
_
=
Merge
(
ctx1
,
bg
);
defer
__
(
)
assertEq
(
mc
.
Value
(
1
),
"hello"
)
assertEq
(
mc
.
Value
(
2
),
nil
)
assertEq
(
mc
.
Value
(
3
),
nil
)
...
...
@@ -89,15 +89,55 @@ func TestMerge(t *testing.T) {
<-
mc
.
Done
()
assertEq
(
mc
.
Err
(),
context
.
Canceled
)
////////
ctx1
,
cancel1
=
context
.
WithCancel
(
bg
)
ctx1
=
context
.
WithValue
(
ctx1
,
3
,
"zzz"
)
done2
:=
make
(
chan
struct
{})
mc
,
__
=
MergeChan
(
ctx1
,
done2
);
defer
__
()
assertEq
(
mc
.
Value
(
1
),
nil
)
assertEq
(
mc
.
Value
(
2
),
nil
)
assertEq
(
mc
.
Value
(
3
),
"zzz"
)
d
,
ok
=
mc
.
Deadline
()
if
!
(
d
==
t0
&&
ok
==
false
)
{
t
.
Fatal
(
"deadline must be unset"
)
}
assertEq
(
mc
.
Err
(),
nil
)
select
{
case
<-
mc
.
Done
()
:
t
.
Fatal
(
"done before any parent done"
)
default
:
}
close
(
done2
)
<-
mc
.
Done
()
assertEq
(
mc
.
Err
(),
context
.
Canceled
)
done2
=
make
(
chan
struct
{})
mc
,
__
=
MergeChan
(
ctx1
,
done2
);
defer
__
()
select
{
case
<-
mc
.
Done
()
:
t
.
Fatal
(
"done before any parent done"
)
default
:
}
cancel1
()
<-
mc
.
Done
()
assertEq
(
mc
.
Err
(),
context
.
Canceled
)
////////
t1
:=
t0
.
AddDate
(
7777
,
1
,
1
)
t2
:=
t0
.
AddDate
(
9999
,
1
,
1
)
ctx1
,
_
=
context
.
WithDeadline
(
bg
,
t1
)
ctx2
,
_
=
context
.
WithDeadline
(
bg
,
t2
)
ctx1
,
_
_
=
context
.
WithDeadline
(
bg
,
t1
);
defer
__
(
)
ctx2
,
_
_
=
context
.
WithDeadline
(
bg
,
t2
);
defer
__
(
)
checkDeadline
:=
func
(
a
,
b
context
.
Context
,
tt
time
.
Time
)
{
t
.
Helper
()
m
,
_
:=
Merge
(
a
,
b
)
m
,
_
_
:=
Merge
(
a
,
b
);
defer
__
(
)
d
,
ok
:=
m
.
Deadline
()
if
!
ok
{
t
.
Fatal
(
"no deadline returned"
)
...
...
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