Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
6312f680
Commit
6312f680
authored
Aug 30, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer/rpc: test concurrent cancel/run
parent
99ababda
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
1 deletion
+64
-1
packer/hook_mock.go
packer/hook_mock.go
+8
-1
packer/rpc/hook_test.go
packer/rpc/hook_test.go
+56
-0
No files found.
packer/hook_mock.go
View file @
6312f680
...
...
@@ -2,6 +2,8 @@ package packer
// MockHook is an implementation of Hook that can be used for tests.
type
MockHook
struct
{
RunFunc
func
()
error
RunCalled
bool
RunComm
Communicator
RunData
interface
{}
...
...
@@ -16,7 +18,12 @@ func (t *MockHook) Run(name string, ui Ui, comm Communicator, data interface{})
t
.
RunData
=
data
t
.
RunName
=
name
t
.
RunUi
=
ui
return
nil
if
t
.
RunFunc
==
nil
{
return
nil
}
return
t
.
RunFunc
()
}
func
(
t
*
MockHook
)
Cancel
()
{
...
...
packer/rpc/hook_test.go
View file @
6312f680
...
...
@@ -4,7 +4,10 @@ import (
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net/rpc"
"reflect"
"sync"
"testing"
"time"
)
func
TestHookRPC
(
t
*
testing
.
T
)
{
...
...
@@ -42,3 +45,56 @@ func TestHook_Implements(t *testing.T) {
assert
.
Implementor
(
h
,
&
r
,
"should be a Hook"
)
}
func
TestHook_cancelWhileRun
(
t
*
testing
.
T
)
{
var
finishLock
sync
.
Mutex
finishOrder
:=
make
([]
string
,
0
,
2
)
h
:=
&
packer
.
MockHook
{
RunFunc
:
func
()
error
{
time
.
Sleep
(
100
*
time
.
Millisecond
)
finishLock
.
Lock
()
finishOrder
=
append
(
finishOrder
,
"run"
)
finishLock
.
Unlock
()
return
nil
},
}
// Serve
server
:=
rpc
.
NewServer
()
RegisterHook
(
server
,
h
)
address
:=
serveSingleConn
(
server
)
// Create the client over RPC and run some methods to verify it works
client
,
err
:=
rpc
.
Dial
(
"tcp"
,
address
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
hClient
:=
Hook
(
client
)
// Start the run
finished
:=
make
(
chan
struct
{})
go
func
()
{
hClient
.
Run
(
"foo"
,
nil
,
nil
,
nil
)
close
(
finished
)
}()
// Cancel it pretty quickly.
time
.
Sleep
(
10
*
time
.
Millisecond
)
hClient
.
Cancel
()
finishLock
.
Lock
()
finishOrder
=
append
(
finishOrder
,
"cancel"
)
finishLock
.
Unlock
()
// Verify things are good
<-
finished
// Check the results
expected
:=
[]
string
{
"cancel"
,
"run"
}
if
!
reflect
.
DeepEqual
(
finishOrder
,
expected
)
{
t
.
Fatalf
(
"bad: %#v"
,
finishOrder
)
}
}
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