stop using goroutines in TestPausedProvisionerProvision_waits
to render the test less flaky. this should fix #7684
This commit is contained in:
parent
4b0f888a1c
commit
dc631530d2
|
@ -2,6 +2,7 @@ package packer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -126,30 +127,29 @@ func TestPausedProvisionerProvision(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPausedProvisionerProvision_waits(t *testing.T) {
|
func TestPausedProvisionerProvision_waits(t *testing.T) {
|
||||||
mock := new(MockProvisioner)
|
startTime := time.Now()
|
||||||
|
waitTime := 50 * time.Millisecond
|
||||||
|
|
||||||
prov := &PausedProvisioner{
|
prov := &PausedProvisioner{
|
||||||
PauseBefore: 50 * time.Millisecond,
|
PauseBefore: waitTime,
|
||||||
Provisioner: mock,
|
Provisioner: &MockProvisioner{
|
||||||
|
ProvFunc: func(context.Context) error {
|
||||||
|
timeSinceStartTime := time.Since(startTime)
|
||||||
|
if timeSinceStartTime < waitTime {
|
||||||
|
return fmt.Errorf("Spent not enough time waiting: %s", timeSinceStartTime)
|
||||||
|
}
|
||||||
|
if timeSinceStartTime > waitTime+10*time.Millisecond {
|
||||||
|
return fmt.Errorf("Spent too much time waiting: %s", timeSinceStartTime)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
dataCh := make(chan struct{})
|
err := prov.Provision(context.Background(), testUi(), new(MockCommunicator))
|
||||||
mock.ProvFunc = func(context.Context) error {
|
|
||||||
close(dataCh)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
go prov.Provision(context.Background(), testUi(), new(MockCommunicator))
|
if err != nil {
|
||||||
|
t.Fatalf("prov failed: %v", err)
|
||||||
select {
|
|
||||||
case <-time.After(10 * time.Millisecond):
|
|
||||||
case <-dataCh:
|
|
||||||
t.Fatal("should not be called")
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-time.After(100 * time.Millisecond):
|
|
||||||
t.Fatal("never called")
|
|
||||||
case <-dataCh:
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue