don't try to interrupt diff in shutdown test

Rather than relying on interrupting Diff, just make sure Stop was called
on the provider. The DiffFn is protected by a mutex in the mock
provider, which means that the tests can't rely on concurent calls to
diff working.
This commit is contained in:
James Bardin 2017-12-20 15:51:02 -05:00
parent e63a3474d5
commit d76482cd89
1 changed files with 11 additions and 8 deletions

View File

@ -7,7 +7,9 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings" "strings"
"sync"
"testing" "testing"
"time"
"github.com/hashicorp/terraform/helper/copy" "github.com/hashicorp/terraform/helper/copy"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
@ -832,8 +834,7 @@ func TestPlan_detailedExitcode_emptyDiff(t *testing.T) {
} }
func TestPlan_shutdown(t *testing.T) { func TestPlan_shutdown(t *testing.T) {
cancelled := false cancelled := make(chan struct{})
stopped := make(chan struct{})
shutdownCh := make(chan struct{}) shutdownCh := make(chan struct{})
p := testProvider() p := testProvider()
@ -847,20 +848,20 @@ func TestPlan_shutdown(t *testing.T) {
} }
p.StopFn = func() error { p.StopFn = func() error {
close(stopped) close(cancelled)
cancelled = true
return nil return nil
} }
var once sync.Once
p.DiffFn = func( p.DiffFn = func(
*terraform.InstanceInfo, *terraform.InstanceInfo,
*terraform.InstanceState, *terraform.InstanceState,
*terraform.ResourceConfig) (*terraform.InstanceDiff, error) { *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
if !cancelled { once.Do(func() {
shutdownCh <- struct{}{} shutdownCh <- struct{}{}
<-stopped })
}
return &terraform.InstanceDiff{ return &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{ Attributes: map[string]*terraform.ResourceAttrDiff{
@ -875,7 +876,9 @@ func TestPlan_shutdown(t *testing.T) {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
} }
if !cancelled { select {
case <-cancelled:
case <-time.After(5 * time.Second):
t.Fatal("command not cancelled") t.Fatal("command not cancelled")
} }
} }