2013-05-11 02:15:13 -04:00
|
|
|
package packer
|
|
|
|
|
2013-05-11 13:27:07 -04:00
|
|
|
import (
|
2019-03-22 09:50:33 -04:00
|
|
|
"context"
|
2013-08-30 20:26:51 -04:00
|
|
|
"sync"
|
2013-05-11 13:27:07 -04:00
|
|
|
"testing"
|
2013-08-30 20:26:51 -04:00
|
|
|
"time"
|
2013-05-11 13:27:07 -04:00
|
|
|
)
|
|
|
|
|
2013-08-30 20:26:51 -04:00
|
|
|
// A helper Hook implementation for testing cancels.
|
|
|
|
type CancelHook struct {
|
|
|
|
sync.Mutex
|
|
|
|
cancelCh chan struct{}
|
|
|
|
doneCh chan struct{}
|
|
|
|
|
|
|
|
Cancelled bool
|
|
|
|
}
|
|
|
|
|
2019-03-22 09:50:33 -04:00
|
|
|
func (h *CancelHook) Run(ctx context.Context, _ string, _ Ui, _ Communicator, _ interface{}) error {
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-time.After(2 * time.Minute):
|
|
|
|
case <-ctx.Done():
|
|
|
|
h.cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-08-30 20:26:51 -04:00
|
|
|
h.Lock()
|
|
|
|
h.cancelCh = make(chan struct{})
|
|
|
|
h.doneCh = make(chan struct{})
|
|
|
|
h.Unlock()
|
|
|
|
|
|
|
|
defer close(h.doneCh)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-h.cancelCh:
|
|
|
|
h.Cancelled = true
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-22 09:50:33 -04:00
|
|
|
func (h *CancelHook) cancel() {
|
2013-08-30 20:26:51 -04:00
|
|
|
h.Lock()
|
|
|
|
close(h.cancelCh)
|
|
|
|
h.Unlock()
|
|
|
|
|
|
|
|
<-h.doneCh
|
|
|
|
}
|
|
|
|
|
2013-05-12 20:15:03 -04:00
|
|
|
func TestDispatchHook_Implements(t *testing.T) {
|
2013-10-17 03:09:27 -04:00
|
|
|
var _ Hook = new(DispatchHook)
|
2013-05-12 20:15:03 -04:00
|
|
|
}
|
|
|
|
|
2013-05-11 13:27:07 -04:00
|
|
|
func TestDispatchHook_Run_NoHooks(t *testing.T) {
|
|
|
|
// Just make sure nothing blows up
|
2013-08-30 20:26:51 -04:00
|
|
|
dh := &DispatchHook{}
|
2019-03-22 09:50:33 -04:00
|
|
|
dh.Run(context.Background(), "foo", nil, nil, nil)
|
2013-05-11 13:27:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDispatchHook_Run(t *testing.T) {
|
2013-08-30 20:03:55 -04:00
|
|
|
hook := &MockHook{}
|
2013-05-11 13:27:07 -04:00
|
|
|
|
|
|
|
mapping := make(map[string][]Hook)
|
|
|
|
mapping["foo"] = []Hook{hook}
|
2013-08-30 20:26:51 -04:00
|
|
|
dh := &DispatchHook{Mapping: mapping}
|
2019-03-22 09:50:33 -04:00
|
|
|
dh.Run(context.Background(), "foo", nil, nil, 42)
|
2013-05-11 13:27:07 -04:00
|
|
|
|
2013-10-17 03:09:27 -04:00
|
|
|
if !hook.RunCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
if hook.RunName != "foo" {
|
|
|
|
t.Fatalf("bad: %s", hook.RunName)
|
|
|
|
}
|
|
|
|
if hook.RunData != 42 {
|
|
|
|
t.Fatalf("bad: %#v", hook.RunData)
|
|
|
|
}
|
2013-05-11 02:15:13 -04:00
|
|
|
}
|
2013-08-30 20:26:51 -04:00
|
|
|
|
|
|
|
func TestDispatchHook_cancel(t *testing.T) {
|
|
|
|
hook := new(CancelHook)
|
|
|
|
|
|
|
|
dh := &DispatchHook{
|
|
|
|
Mapping: map[string][]Hook{
|
2016-11-01 17:08:04 -04:00
|
|
|
"foo": {hook},
|
2013-08-30 20:26:51 -04:00
|
|
|
},
|
|
|
|
}
|
2019-03-22 09:50:33 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go dh.Run(ctx, "foo", nil, nil, 42)
|
2013-08-30 20:26:51 -04:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2019-03-22 09:50:33 -04:00
|
|
|
cancel()
|
2013-08-30 20:26:51 -04:00
|
|
|
|
|
|
|
if !hook.Cancelled {
|
|
|
|
t.Fatal("hook should've cancelled")
|
|
|
|
}
|
|
|
|
}
|