add tests for cancellation while building

This commit is contained in:
Adrien Delorme 2019-05-06 15:07:57 +02:00
parent dd2785ff08
commit 583d93790f
6 changed files with 105 additions and 14 deletions

View File

@ -0,0 +1,81 @@
package command
import (
"context"
"path/filepath"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestBuildCommand_RunContext_CtxCancel(t *testing.T) {
tests := []struct {
name string
args []string
parallelPassingTests int
expected int
}{
{"cancel 1 pending build - parallel=true",
[]string{"-parallel=true", filepath.Join(testFixture("parallel"), "1lock-5wg.json")},
5,
1,
},
{"cancel in the middle with 2 pending builds - parallel=true",
[]string{"-parallel=true", filepath.Join(testFixture("parallel"), "2lock-4wg.json")},
4,
1,
},
// {"cancel 1 locked build - debug - parallel=true",
// []string{"-parallel=true", "-debug=true", filepath.Join(testFixture("parallel"), "1lock.json")},
// 5,
// 1,
// },
// {"cancel 2 locked builds - debug - parallel=true",
// []string{"-parallel=true", "-debug=true", filepath.Join(testFixture("parallel"), "2lock.json")},
// 4,
// 1,
// },
// {"cancel 1 locked build - debug - parallel=false",
// []string{"-parallel=false", "-debug=true", filepath.Join(testFixture("parallel"), "1lock.json")},
// 5,
// 1,
// },
// {"cancel 2 locked builds - debug - parallel=false",
// []string{"-parallel=false", "-debug=true", filepath.Join(testFixture("parallel"), "2lock.json")},
// 4,
// 1,
// },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
b := NewParallelTestBuilder(tt.parallelPassingTests)
locked := &LockedBuilder{unlock: make(chan interface{})}
c := &BuildCommand{
Meta: testMetaParallel(t, b, locked),
}
ctx, cancelCtx := context.WithCancel(context.Background())
codeC := make(chan int)
go func() {
defer close(codeC)
codeC <- c.RunContext(ctx, tt.args)
}()
b.wg.Wait() // ran `tt.parallelPassingTests` times
cancelCtx()
select {
case code := <-codeC:
if code != tt.expected {
t.Logf("wrong code: %s", cmp.Diff(code, tt.expected))
fatalCommand(t, c.Meta)
}
case <-time.After(15 * time.Second):
t.Fatal("deadlock")
}
})
}
}

View File

@ -46,7 +46,6 @@ func (b *LockedBuilder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook)
select {
case <-b.unlock:
case <-ctx.Done():
panic("crap")
return nil, ctx.Err()
}
return nil, nil
@ -99,7 +98,7 @@ func TestBuildParallel_1(t *testing.T) {
args := []string{
fmt.Sprintf("-parallel=true"),
filepath.Join(testFixture("parallel"), "1lock.json"),
filepath.Join(testFixture("parallel"), "1lock-5wg.json"),
}
wg := errgroup.Group{}
@ -128,7 +127,7 @@ func TestBuildParallel_2(t *testing.T) {
args := []string{
fmt.Sprintf("-parallel-builds=3"),
filepath.Join(testFixture("parallel"), "2lock.json"),
filepath.Join(testFixture("parallel"), "2lock-4wg"),
}
wg := errgroup.Group{}

View File

@ -0,0 +1,10 @@
{
"builders": [
{"type": "lock", "name": "build0"},
{"type": "parallel-test", "name": "build1"},
{"type": "parallel-test", "name": "build2"},
{"type": "parallel-test", "name": "build3"},
{"type": "parallel-test", "name": "build4"},
{"type": "parallel-test", "name": "build5"}
]
}

View File

@ -1,10 +1,5 @@
{
"builders": [
{"type": "lock", "name": "build0"},
{"type": "parallel-test", "name": "build1"},
{"type": "parallel-test", "name": "build2"},
{"type": "parallel-test", "name": "build3"},
{"type": "parallel-test", "name": "build4"},
{"type": "parallel-test", "name": "build5"}
{"type": "lock", "name": "build0"}
]
}

View File

@ -0,0 +1,10 @@
{
"builders": [
{"type": "lock", "name": "build0"},
{"type": "parallel-test", "name": "build1"},
{"type": "parallel-test", "name": "build2"},
{"type": "lock", "name": "build3"},
{"type": "parallel-test", "name": "build4"},
{"type": "parallel-test", "name": "build5"}
]
}

View File

@ -1,10 +1,6 @@
{
"builders": [
{"type": "lock", "name": "build0"},
{"type": "parallel-test", "name": "build1"},
{"type": "parallel-test", "name": "build2"},
{"type": "lock", "name": "build3"},
{"type": "parallel-test", "name": "build4"},
{"type": "parallel-test", "name": "build5"}
{"type": "lock", "name": "build1"}
]
}