2013-05-11 12:51:49 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:50:33 -04:00
|
|
|
"context"
|
2013-05-11 12:51:49 -04:00
|
|
|
"testing"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-05-11 12:51:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHook_Implements(t *testing.T) {
|
2013-10-16 23:04:57 -04:00
|
|
|
var _ packer.Hook = new(hook)
|
2013-05-11 12:51:49 -04:00
|
|
|
}
|
2013-08-31 02:10:16 -04:00
|
|
|
|
|
|
|
func TestHook_cancelWhileRun(t *testing.T) {
|
2019-03-27 07:29:09 -04:00
|
|
|
topCtx, cancelTopCtx := context.WithCancel(context.Background())
|
2013-08-31 02:10:16 -04:00
|
|
|
|
|
|
|
h := &packer.MockHook{
|
2019-03-27 07:29:09 -04:00
|
|
|
RunFunc: func(ctx context.Context) error {
|
|
|
|
cancelTopCtx()
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
2013-08-31 02:10:16 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve
|
2013-12-10 14:50:30 -05:00
|
|
|
client, server := testClientServer(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
server.RegisterHook(h)
|
|
|
|
hClient := client.Hook()
|
2013-08-31 02:10:16 -04:00
|
|
|
|
|
|
|
// Start the run
|
2019-03-27 07:29:09 -04:00
|
|
|
err := hClient.Run(topCtx, "foo", nil, nil, nil)
|
2013-08-31 02:10:16 -04:00
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have errored")
|
2013-08-31 02:10:16 -04:00
|
|
|
}
|
|
|
|
}
|