2023-08-10 09:06:00 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-04-06 13:44:04 +00:00
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-01 23:08:02 +00:00
|
|
|
"errors"
|
2019-06-16 13:26:45 +00:00
|
|
|
"fmt"
|
2019-04-06 13:44:04 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-11-23 14:36:52 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-03-20 03:50:56 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2019-04-06 13:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var noContext = context.Background()
|
|
|
|
|
|
|
|
func TestFifo(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
want := &model.Task{ID: "1"}
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background())
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Push(noContext, want))
|
2019-04-06 13:44:04 +00:00
|
|
|
info := q.Info(noContext)
|
|
|
|
if len(info.Pending) != 1 {
|
|
|
|
t.Errorf("expect task in pending queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-04-06 13:44:04 +00:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("expect task returned form queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
info = q.Info(noContext)
|
|
|
|
if len(info.Pending) != 0 {
|
|
|
|
t.Errorf("expect task removed from pending queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(info.Running) != 1 {
|
|
|
|
t.Errorf("expect task in running queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.Done(noContext, got.ID, model.StatusSuccess))
|
2019-04-06 13:44:04 +00:00
|
|
|
info = q.Info(noContext)
|
|
|
|
if len(info.Pending) != 0 {
|
|
|
|
t.Errorf("expect task removed from pending queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(info.Running) != 0 {
|
|
|
|
t.Errorf("expect task removed from running queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFifoExpire(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
want := &model.Task{ID: "1"}
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background()).(*fifo)
|
2019-04-06 13:44:04 +00:00
|
|
|
q.extension = 0
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Push(noContext, want))
|
2019-04-06 13:44:04 +00:00
|
|
|
info := q.Info(noContext)
|
|
|
|
if len(info.Pending) != 1 {
|
|
|
|
t.Errorf("expect task in pending queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-04-06 13:44:04 +00:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("expect task returned form queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
q.process()
|
|
|
|
if len(info.Pending) != 1 {
|
|
|
|
t.Errorf("expect task re-added to pending queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFifoWait(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
want := &model.Task{ID: "1"}
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background()).(*fifo)
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Push(noContext, want))
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-04-06 13:44:04 +00:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("expect task returned form queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Wait(noContext, got.ID))
|
2019-04-06 13:44:04 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-time.After(time.Millisecond)
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.Done(noContext, got.ID, model.StatusSuccess))
|
2019-04-06 13:44:04 +00:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFifoEvict(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
t1 := &model.Task{ID: "1"}
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background())
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Push(noContext, t1))
|
2019-04-06 13:44:04 +00:00
|
|
|
info := q.Info(noContext)
|
|
|
|
if len(info.Pending) != 1 {
|
|
|
|
t.Errorf("expect task in pending queue")
|
|
|
|
}
|
|
|
|
if err := q.Evict(noContext, t1.ID); err != nil {
|
|
|
|
t.Errorf("expect task evicted from queue")
|
|
|
|
}
|
|
|
|
info = q.Info(noContext)
|
|
|
|
if len(info.Pending) != 0 {
|
|
|
|
t.Errorf("expect pending queue has zero items")
|
|
|
|
}
|
2023-02-01 23:08:02 +00:00
|
|
|
if err := q.Evict(noContext, t1.ID); !errors.Is(err, ErrNotFound) {
|
2019-04-06 13:44:04 +00:00
|
|
|
t.Errorf("expect not found error when evicting item not in queue, got %s", err)
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 15:38:19 +00:00
|
|
|
|
|
|
|
func TestFifoDependencies(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
task1 := &model.Task{
|
2019-06-13 15:38:19 +00:00
|
|
|
ID: "1",
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task2 := &model.Task{
|
2019-06-13 15:38:19 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2019-06-13 15:38:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background()).(*fifo)
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task1}))
|
2019-06-13 15:38:19 +00:00
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-06-13 15:38:19 +00:00
|
|
|
if got != task1 {
|
|
|
|
t.Errorf("expect task1 returned from queue as task2 depends on it")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.Done(noContext, got.ID, model.StatusSuccess))
|
2019-06-14 07:03:59 +00:00
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ = q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-06-13 15:38:19 +00:00
|
|
|
if got != task2 {
|
|
|
|
t.Errorf("expect task2 returned from queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 13:26:45 +00:00
|
|
|
|
|
|
|
func TestFifoErrors(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
task1 := &model.Task{
|
2019-06-16 13:26:45 +00:00
|
|
|
ID: "1",
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task2 := &model.Task{
|
2019-06-16 13:26:45 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2019-06-16 13:26:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task3 := &model.Task{
|
2019-06-16 13:26:45 +00:00
|
|
|
ID: "3",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2019-06-17 07:06:36 +00:00
|
|
|
RunOn: []string{"success", "failure"},
|
2019-06-16 13:26:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background()).(*fifo)
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))
|
2019-06-16 13:26:45 +00:00
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-06-16 13:26:45 +00:00
|
|
|
if got != task1 {
|
|
|
|
t.Errorf("expect task1 returned from queue as task2 depends on it")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Error(noContext, got.ID, fmt.Errorf("exitcode 1, there was an error")))
|
2019-06-16 13:26:45 +00:00
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ = q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-06-16 13:26:45 +00:00
|
|
|
if got != task2 {
|
|
|
|
t.Errorf("expect task2 returned from queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.ShouldRun() {
|
|
|
|
t.Errorf("expect task2 should not run, since task1 failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ = q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-06-16 13:26:45 +00:00
|
|
|
if got != task3 {
|
|
|
|
t.Errorf("expect task3 returned from queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !got.ShouldRun() {
|
|
|
|
t.Errorf("expect task3 should run, task1 failed, but task3 runs on failure too")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 07:06:36 +00:00
|
|
|
|
2021-05-21 10:50:19 +00:00
|
|
|
func TestFifoErrors2(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
task1 := &model.Task{
|
2021-05-21 10:50:19 +00:00
|
|
|
ID: "1",
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task2 := &model.Task{
|
2021-06-22 10:34:35 +00:00
|
|
|
ID: "2",
|
2021-05-21 10:50:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task3 := &model.Task{
|
2021-05-21 10:50:19 +00:00
|
|
|
ID: "3",
|
|
|
|
Dependencies: []string{"1", "2"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2021-05-21 10:50:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background()).(*fifo)
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))
|
2021-05-21 10:50:19 +00:00
|
|
|
|
2021-05-25 11:05:31 +00:00
|
|
|
for i := 0; i < 2; i++ {
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2021-06-22 10:34:35 +00:00
|
|
|
if got != task1 && got != task2 {
|
2021-05-25 11:05:31 +00:00
|
|
|
t.Errorf("expect task1 or task2 returned from queue as task3 depends on them")
|
|
|
|
return
|
|
|
|
}
|
2021-05-21 10:50:19 +00:00
|
|
|
|
2021-05-25 11:05:31 +00:00
|
|
|
if got != task1 {
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.Done(noContext, got.ID, model.StatusSuccess))
|
2021-05-25 11:05:31 +00:00
|
|
|
}
|
|
|
|
if got != task2 {
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Error(noContext, got.ID, fmt.Errorf("exitcode 1, there was an error")))
|
2021-05-25 11:05:31 +00:00
|
|
|
}
|
2021-05-21 10:50:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2021-05-21 10:50:19 +00:00
|
|
|
if got != task3 {
|
|
|
|
t.Errorf("expect task3 returned from queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.ShouldRun() {
|
|
|
|
t.Errorf("expect task3 should not run, task1 succeeded but task2 failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFifoErrorsMultiThread(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
task1 := &model.Task{
|
2021-05-21 10:50:19 +00:00
|
|
|
ID: "1",
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task2 := &model.Task{
|
2021-05-21 10:50:19 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2021-05-21 10:50:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task3 := &model.Task{
|
2021-05-21 10:50:19 +00:00
|
|
|
ID: "3",
|
|
|
|
Dependencies: []string{"1", "2"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2021-05-21 10:50:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background()).(*fifo)
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))
|
2021-05-21 10:50:19 +00:00
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
obtainedWorkCh := make(chan *model.Task)
|
2021-05-21 10:50:19 +00:00
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
for {
|
|
|
|
fmt.Printf("Worker %d started\n", i)
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2021-05-21 10:50:19 +00:00
|
|
|
obtainedWorkCh <- got
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
task1Processed := false
|
|
|
|
task2Processed := false
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case got := <-obtainedWorkCh:
|
|
|
|
fmt.Println(got.ID)
|
|
|
|
|
|
|
|
if !task1Processed {
|
|
|
|
if got != task1 {
|
|
|
|
t.Errorf("expect task1 returned from queue as task2 and task3 depends on it")
|
|
|
|
return
|
|
|
|
}
|
2021-12-01 13:22:06 +00:00
|
|
|
task1Processed = true
|
|
|
|
assert.NoError(t, q.Error(noContext, got.ID, fmt.Errorf("exitcode 1, there was an error")))
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
fmt.Printf("Worker spawned\n")
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2021-12-01 13:22:06 +00:00
|
|
|
obtainedWorkCh <- got
|
|
|
|
}
|
|
|
|
}()
|
2021-05-21 10:50:19 +00:00
|
|
|
} else if !task2Processed {
|
|
|
|
if got != task2 {
|
|
|
|
t.Errorf("expect task2 returned from queue")
|
|
|
|
return
|
|
|
|
}
|
2021-12-01 13:22:06 +00:00
|
|
|
task2Processed = true
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.Done(noContext, got.ID, model.StatusSuccess))
|
2021-12-01 13:22:06 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
fmt.Printf("Worker spawned\n")
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2021-12-01 13:22:06 +00:00
|
|
|
obtainedWorkCh <- got
|
|
|
|
}
|
|
|
|
}()
|
2021-05-21 10:50:19 +00:00
|
|
|
} else {
|
|
|
|
if got != task3 {
|
|
|
|
t.Errorf("expect task3 returned from queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.ShouldRun() {
|
|
|
|
t.Errorf("expect task3 should not run, task1 succeeded but task2 failed")
|
|
|
|
return
|
|
|
|
}
|
2021-12-01 13:22:06 +00:00
|
|
|
return
|
2021-05-21 10:50:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 10:54:55 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2021-05-25 11:05:31 +00:00
|
|
|
info := q.Info(noContext)
|
|
|
|
fmt.Println(info.String())
|
2021-05-21 10:50:19 +00:00
|
|
|
t.Errorf("test timed out")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 10:43:59 +00:00
|
|
|
func TestFifoTransitiveErrors(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
task1 := &model.Task{
|
2019-07-22 10:43:59 +00:00
|
|
|
ID: "1",
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task2 := &model.Task{
|
2019-07-22 10:43:59 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2019-07-22 10:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task3 := &model.Task{
|
2019-07-22 10:43:59 +00:00
|
|
|
ID: "3",
|
|
|
|
Dependencies: []string{"2"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2019-07-22 10:43:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background()).(*fifo)
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))
|
2019-07-22 10:43:59 +00:00
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-07-22 10:43:59 +00:00
|
|
|
if got != task1 {
|
|
|
|
t.Errorf("expect task1 returned from queue as task2 depends on it")
|
|
|
|
return
|
|
|
|
}
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Error(noContext, got.ID, fmt.Errorf("exitcode 1, there was an error")))
|
2019-07-22 10:43:59 +00:00
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ = q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-07-22 10:43:59 +00:00
|
|
|
if got != task2 {
|
|
|
|
t.Errorf("expect task2 returned from queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got.ShouldRun() {
|
|
|
|
t.Errorf("expect task2 should not run, since task1 failed")
|
|
|
|
return
|
|
|
|
}
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.Done(noContext, got.ID, model.StatusSkipped))
|
2019-07-22 10:43:59 +00:00
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ = q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-07-22 10:43:59 +00:00
|
|
|
if got != task3 {
|
|
|
|
t.Errorf("expect task3 returned from queue")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got.ShouldRun() {
|
|
|
|
t.Errorf("expect task3 should not run, task1 failed, thus task2 was skipped, task3 should be skipped too")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 06:36:13 +00:00
|
|
|
func TestFifoCancel(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
task1 := &model.Task{
|
2019-06-19 06:36:13 +00:00
|
|
|
ID: "1",
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task2 := &model.Task{
|
2019-06-19 06:36:13 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2019-06-19 06:36:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task3 := &model.Task{
|
2019-06-19 06:36:13 +00:00
|
|
|
ID: "3",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2019-06-19 06:36:13 +00:00
|
|
|
RunOn: []string{"success", "failure"},
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background()).(*fifo)
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))
|
2019-06-19 06:36:13 +00:00
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
_, _ = q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2021-11-24 01:01:12 +00:00
|
|
|
assert.NoError(t, q.Error(noContext, task1.ID, fmt.Errorf("canceled")))
|
|
|
|
assert.NoError(t, q.Error(noContext, task2.ID, fmt.Errorf("canceled")))
|
|
|
|
assert.NoError(t, q.Error(noContext, task3.ID, fmt.Errorf("canceled")))
|
2019-06-19 06:36:13 +00:00
|
|
|
|
|
|
|
info := q.Info(noContext)
|
|
|
|
if len(info.Pending) != 0 {
|
2021-11-24 01:01:12 +00:00
|
|
|
t.Errorf("All pipelines should be canceled")
|
2019-06-19 06:36:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 07:19:14 +00:00
|
|
|
func TestFifoPause(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
task1 := &model.Task{
|
2019-06-28 07:19:14 +00:00
|
|
|
ID: "1",
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background()).(*fifo)
|
2019-06-28 07:19:14 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2023-03-21 13:10:43 +00:00
|
|
|
_, _ = q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-06-28 07:19:14 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
q.Pause()
|
|
|
|
t0 := time.Now()
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Push(noContext, task1))
|
2019-06-28 07:19:14 +00:00
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
q.Resume()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
t1 := time.Now()
|
|
|
|
|
2019-07-09 14:23:56 +00:00
|
|
|
if t1.Sub(t0) < 20*time.Millisecond {
|
2019-06-28 07:19:14 +00:00
|
|
|
t.Errorf("Should have waited til resume")
|
|
|
|
}
|
|
|
|
|
|
|
|
q.Pause()
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Push(noContext, task1))
|
2019-06-28 07:19:14 +00:00
|
|
|
q.Resume()
|
2023-03-21 13:10:43 +00:00
|
|
|
_, _ = q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-06-28 07:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFifoPauseResume(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
task1 := &model.Task{
|
2019-06-28 07:19:14 +00:00
|
|
|
ID: "1",
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background()).(*fifo)
|
2019-06-28 07:19:14 +00:00
|
|
|
q.Pause()
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Push(noContext, task1))
|
2019-06-28 07:19:14 +00:00
|
|
|
q.Resume()
|
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
_, _ = q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-06-28 07:19:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 14:23:56 +00:00
|
|
|
func TestWaitingVsPending(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
task1 := &model.Task{
|
2019-07-09 14:23:56 +00:00
|
|
|
ID: "1",
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task2 := &model.Task{
|
2019-07-09 14:23:56 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2019-07-09 14:23:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task3 := &model.Task{
|
2019-07-09 14:23:56 +00:00
|
|
|
ID: "3",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: make(map[string]model.StatusValue),
|
2019-07-09 14:23:56 +00:00
|
|
|
RunOn: []string{"success", "failure"},
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:15:04 +00:00
|
|
|
q := New(context.Background()).(*fifo)
|
2023-03-20 03:50:56 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))
|
2019-07-09 14:23:56 +00:00
|
|
|
|
2023-03-21 13:10:43 +00:00
|
|
|
got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2019-07-09 14:23:56 +00:00
|
|
|
|
|
|
|
info := q.Info(noContext)
|
|
|
|
if info.Stats.WaitingOnDeps != 2 {
|
|
|
|
t.Errorf("2 should wait on deps")
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, q.Error(noContext, got.ID, fmt.Errorf("exitcode 1, there was an error")))
|
2023-03-21 13:10:43 +00:00
|
|
|
got, err := q.Poll(noContext, 1, func(*model.Task) bool { return true })
|
2021-11-25 16:15:36 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, task2, got)
|
2019-07-09 14:23:56 +00:00
|
|
|
|
|
|
|
info = q.Info(noContext)
|
|
|
|
if info.Stats.WaitingOnDeps != 0 {
|
|
|
|
t.Errorf("0 should wait on deps")
|
|
|
|
}
|
|
|
|
if info.Stats.Pending != 1 {
|
|
|
|
t.Errorf("1 should wait for worker")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 07:06:36 +00:00
|
|
|
func TestShouldRun(t *testing.T) {
|
2023-03-20 03:50:56 +00:00
|
|
|
task := &model.Task{
|
2019-06-17 07:06:36 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: map[string]model.StatusValue{
|
|
|
|
"1": model.StatusSuccess,
|
2019-06-17 07:06:36 +00:00
|
|
|
},
|
|
|
|
RunOn: []string{"failure"},
|
|
|
|
}
|
|
|
|
if task.ShouldRun() {
|
|
|
|
t.Errorf("expect task to not run, it runs on failure only")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task = &model.Task{
|
2019-06-17 07:06:36 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: map[string]model.StatusValue{
|
|
|
|
"1": model.StatusSuccess,
|
2019-06-17 07:06:36 +00:00
|
|
|
},
|
|
|
|
RunOn: []string{"failure", "success"},
|
|
|
|
}
|
|
|
|
if !task.ShouldRun() {
|
|
|
|
t.Errorf("expect task to run")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task = &model.Task{
|
2019-06-17 07:06:36 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: map[string]model.StatusValue{
|
|
|
|
"1": model.StatusFailure,
|
2019-06-17 07:06:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if task.ShouldRun() {
|
|
|
|
t.Errorf("expect task to not run")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task = &model.Task{
|
2019-06-17 07:06:36 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: map[string]model.StatusValue{
|
|
|
|
"1": model.StatusSuccess,
|
2019-06-17 07:06:36 +00:00
|
|
|
},
|
|
|
|
RunOn: []string{"success"},
|
|
|
|
}
|
|
|
|
if !task.ShouldRun() {
|
|
|
|
t.Errorf("expect task to run")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task = &model.Task{
|
2019-06-17 07:06:36 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: map[string]model.StatusValue{
|
|
|
|
"1": model.StatusFailure,
|
2019-06-17 07:06:36 +00:00
|
|
|
},
|
|
|
|
RunOn: []string{"failure"},
|
|
|
|
}
|
|
|
|
if !task.ShouldRun() {
|
|
|
|
t.Errorf("expect task to run")
|
|
|
|
return
|
|
|
|
}
|
2019-07-22 10:43:59 +00:00
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task = &model.Task{
|
2019-07-22 10:43:59 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: map[string]model.StatusValue{
|
|
|
|
"1": model.StatusSkipped,
|
2019-07-22 10:43:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if task.ShouldRun() {
|
2023-03-20 03:50:56 +00:00
|
|
|
t.Errorf("model.Tasked should not run if dependency is skipped")
|
2019-07-22 10:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-20 03:50:56 +00:00
|
|
|
task = &model.Task{
|
2019-07-22 10:43:59 +00:00
|
|
|
ID: "2",
|
|
|
|
Dependencies: []string{"1"},
|
2023-03-20 03:50:56 +00:00
|
|
|
DepStatus: map[string]model.StatusValue{
|
|
|
|
"1": model.StatusSkipped,
|
2019-07-22 10:43:59 +00:00
|
|
|
},
|
|
|
|
RunOn: []string{"failure"},
|
|
|
|
}
|
|
|
|
if !task.ShouldRun() {
|
|
|
|
t.Errorf("On Failure tasks should run on skipped deps, something failed higher up the chain")
|
|
|
|
return
|
|
|
|
}
|
2019-06-17 07:06:36 +00:00
|
|
|
}
|