2019-05-06 09:07:57 -04:00
|
|
|
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",
|
2020-05-08 11:46:33 -04:00
|
|
|
[]string{"-parallel-builds=10", filepath.Join(testFixture("parallel"), "1lock-5wg.json")},
|
2019-05-06 09:07:57 -04:00
|
|
|
5,
|
|
|
|
1,
|
|
|
|
},
|
|
|
|
{"cancel in the middle with 2 pending builds - parallel=true",
|
2020-05-08 11:46:33 -04:00
|
|
|
[]string{"-parallel-builds=10", filepath.Join(testFixture("parallel"), "2lock-4wg.json")},
|
2019-05-06 09:07:57 -04:00
|
|
|
4,
|
|
|
|
1,
|
|
|
|
},
|
2019-05-06 10:29:35 -04:00
|
|
|
{"cancel 1 locked build - debug - parallel=true",
|
2020-05-08 11:46:33 -04:00
|
|
|
[]string{"-parallel-builds=10", "-debug=true", filepath.Join(testFixture("parallel"), "1lock.json")},
|
2019-05-06 10:29:35 -04:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
},
|
|
|
|
{"cancel 2 locked builds - debug - parallel=true",
|
2020-05-08 11:46:33 -04:00
|
|
|
[]string{"-parallel-builds=10", "-debug=true", filepath.Join(testFixture("parallel"), "2lock.json")},
|
2019-05-06 10:29:35 -04:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
},
|
|
|
|
{"cancel 1 locked build - debug - parallel=false",
|
2020-05-08 11:46:33 -04:00
|
|
|
[]string{"-parallel-builds=1", "-debug=true", filepath.Join(testFixture("parallel"), "1lock.json")},
|
2019-05-06 10:29:35 -04:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
},
|
|
|
|
{"cancel 2 locked builds - debug - parallel=false",
|
2020-05-08 11:46:33 -04:00
|
|
|
[]string{"-parallel-builds=1", "-debug=true", filepath.Join(testFixture("parallel"), "2lock.json")},
|
2019-05-06 10:29:35 -04:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
},
|
2019-05-06 09:07:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-05-08 06:06:41 -04:00
|
|
|
cfg, ret := c.ParseArgs(tt.args)
|
|
|
|
if ret != 0 {
|
2021-03-02 05:43:58 -05:00
|
|
|
t.Error("ParseArgs failed.")
|
|
|
|
return
|
2020-05-08 06:06:41 -04:00
|
|
|
}
|
|
|
|
codeC <- c.RunContext(ctx, cfg)
|
2019-05-06 09:07:57 -04:00
|
|
|
}()
|
2019-05-06 10:29:35 -04:00
|
|
|
t.Logf("waiting for passing tests if any")
|
2019-05-06 09:07:57 -04:00
|
|
|
b.wg.Wait() // ran `tt.parallelPassingTests` times
|
2019-05-06 10:29:35 -04:00
|
|
|
t.Logf("cancelling context")
|
2019-05-06 09:07:57 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|