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"
|
2024-11-18 15:50:17 +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"
|
2024-01-14 17:22:06 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2019-04-06 13:44:04 +00:00
|
|
|
)
|
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
var (
|
|
|
|
filterFnTrue = func(*model.Task) (bool, int) { return true, 1 }
|
|
|
|
genDummyTask = func() *model.Task {
|
|
|
|
return &model.Task{
|
|
|
|
ID: "1",
|
|
|
|
Data: []byte("{}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
waitForProcess = func() { time.Sleep(processTimeInterval + 10*time.Millisecond) }
|
|
|
|
)
|
2019-04-06 13:44:04 +00:00
|
|
|
|
|
|
|
func TestFifo(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q := NewMemoryQueue(ctx)
|
2024-11-18 15:50:17 +00:00
|
|
|
dummyTask := genDummyTask()
|
|
|
|
|
|
|
|
assert.NoError(t, q.Push(ctx, dummyTask))
|
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
info := q.Info(ctx)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Len(t, info.Pending, 1, "expect task in pending queue")
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.Equal(t, dummyTask, got)
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
info = q.Info(ctx)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Len(t, info.Pending, 0, "expect task removed from pending queue")
|
|
|
|
assert.Len(t, info.Running, 1, "expect task in running queue")
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Done(ctx, got.ID, model.StatusSuccess))
|
2024-11-18 15:50:17 +00:00
|
|
|
|
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
info = q.Info(ctx)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Len(t, info.Pending, 0, "expect task removed from pending queue")
|
|
|
|
assert.Len(t, info.Running, 0, "expect task removed from running queue")
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFifoExpire(t *testing.T) {
|
2024-09-20 07:40:48 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
2024-11-18 15:50:17 +00:00
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q, _ := NewMemoryQueue(ctx).(*fifo)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NotNil(t, q)
|
|
|
|
|
|
|
|
dummyTask := genDummyTask()
|
|
|
|
|
2019-04-06 13:44:04 +00:00
|
|
|
q.extension = 0
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NoError(t, q.Push(ctx, dummyTask))
|
|
|
|
waitForProcess()
|
2024-09-20 07:40:48 +00:00
|
|
|
info := q.Info(ctx)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Len(t, info.Pending, 1, "expect task in pending queue")
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.Equal(t, dummyTask, got)
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
info = q.Info(ctx)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Len(t, info.Pending, 1, "expect task re-added to pending queue")
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFifoWait(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q, _ := NewMemoryQueue(ctx).(*fifo)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NotNil(t, q)
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
dummyTask := genDummyTask()
|
|
|
|
|
|
|
|
assert.NoError(t, q.Push(ctx, dummyTask))
|
|
|
|
|
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.Equal(t, dummyTask, got)
|
2019-04-06 13:44:04 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Wait(ctx, got.ID))
|
2019-04-06 13:44:04 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-time.After(time.Millisecond)
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Done(ctx, got.ID, model.StatusSuccess))
|
2019-04-06 13:44:04 +00:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFifoEvict(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2019-04-06 13:44:04 +00:00
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q := NewMemoryQueue(ctx)
|
2024-11-18 15:50:17 +00:00
|
|
|
dummyTask := genDummyTask()
|
|
|
|
|
|
|
|
assert.NoError(t, q.Push(ctx, dummyTask))
|
|
|
|
|
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
info := q.Info(ctx)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Len(t, info.Pending, 1, "expect task in pending queue")
|
2024-11-18 15:50:17 +00:00
|
|
|
|
|
|
|
err := q.Evict(ctx, dummyTask.ID)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
2024-11-18 15:50:17 +00:00
|
|
|
|
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
info = q.Info(ctx)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Len(t, info.Pending, 0)
|
2024-11-18 15:50:17 +00:00
|
|
|
|
|
|
|
err = q.Evict(ctx, dummyTask.ID)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.ErrorIs(t, err, ErrNotFound)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
2019-06-13 15:38:19 +00:00
|
|
|
|
|
|
|
func TestFifoDependencies(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2019-06-13 15:38:19 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
task1 := genDummyTask()
|
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
|
|
|
}
|
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q, _ := NewMemoryQueue(ctx).(*fifo)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NotNil(t, q)
|
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(ctx, []*model.Task{task2, task1}))
|
2019-06-13 15:38:19 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, task1, got)
|
2019-06-13 15:38:19 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Done(ctx, got.ID, model.StatusSuccess))
|
2019-06-14 07:03:59 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err = q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, task2, got)
|
2019-06-13 15:38:19 +00:00
|
|
|
}
|
2019-06-16 13:26:45 +00:00
|
|
|
|
|
|
|
func TestFifoErrors(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2019-06-16 13:26:45 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
task1 := genDummyTask()
|
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
|
|
|
}
|
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q, _ := NewMemoryQueue(ctx).(*fifo)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NotNil(t, q)
|
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(ctx, []*model.Task{task2, task3, task1}))
|
2019-06-16 13:26:45 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, task1, got)
|
2019-06-16 13:26:45 +00:00
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Error(ctx, got.ID, fmt.Errorf("exit code 1, there was an error")))
|
2019-06-16 13:26:45 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err = q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, task2, got)
|
|
|
|
assert.False(t, got.ShouldRun())
|
2019-06-16 13:26:45 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err = q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, task3, got)
|
|
|
|
assert.True(t, got.ShouldRun())
|
2019-06-16 13:26:45 +00:00
|
|
|
}
|
2019-06-17 07:06:36 +00:00
|
|
|
|
2021-05-21 10:50:19 +00:00
|
|
|
func TestFifoErrors2(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2021-05-21 10:50:19 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
task1 := genDummyTask()
|
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
|
|
|
}
|
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q, _ := NewMemoryQueue(ctx).(*fifo)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NotNil(t, q)
|
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(ctx, []*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++ {
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, got != task1 && got != task2, "expect task1 or task2 returned from queue as task3 depends on them")
|
2021-05-21 10:50:19 +00:00
|
|
|
|
2021-05-25 11:05:31 +00:00
|
|
|
if got != task1 {
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Done(ctx, got.ID, model.StatusSuccess))
|
2021-05-25 11:05:31 +00:00
|
|
|
}
|
|
|
|
if got != task2 {
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Error(ctx, got.ID, fmt.Errorf("exit code 1, there was an error")))
|
2021-05-25 11:05:31 +00:00
|
|
|
}
|
2021-05-21 10:50:19 +00:00
|
|
|
}
|
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, task3, got)
|
|
|
|
assert.False(t, got.ShouldRun())
|
2021-05-21 10:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFifoErrorsMultiThread(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2021-05-21 10:50:19 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
task1 := genDummyTask()
|
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
|
|
|
}
|
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q, _ := NewMemoryQueue(ctx).(*fifo)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NotNil(t, q)
|
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(ctx, []*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)
|
2024-11-18 15:50:17 +00:00
|
|
|
defer func() { close(obtainedWorkCh) }()
|
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)
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
2024-11-18 15:50:17 +00:00
|
|
|
if err != nil && errors.Is(err, context.Canceled) {
|
|
|
|
return
|
|
|
|
}
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
2021-05-21 10:50:19 +00:00
|
|
|
obtainedWorkCh <- got
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
task1Processed := false
|
|
|
|
task2Processed := false
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case got := <-obtainedWorkCh:
|
|
|
|
fmt.Println(got.ID)
|
|
|
|
|
2024-01-10 14:34:44 +00:00
|
|
|
switch {
|
|
|
|
case !task1Processed:
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, task1, got)
|
2021-12-01 13:22:06 +00:00
|
|
|
task1Processed = true
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Error(ctx, got.ID, fmt.Errorf("exit code 1, there was an error")))
|
2021-12-01 13:22:06 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
fmt.Printf("Worker spawned\n")
|
2024-11-18 15:50:17 +00:00
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
|
|
|
if err != nil && errors.Is(err, context.Canceled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
2021-12-01 13:22:06 +00:00
|
|
|
obtainedWorkCh <- got
|
|
|
|
}
|
|
|
|
}()
|
2024-01-10 14:34:44 +00:00
|
|
|
case !task2Processed:
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, task2, got)
|
2021-12-01 13:22:06 +00:00
|
|
|
task2Processed = true
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Done(ctx, got.ID, model.StatusSuccess))
|
2021-12-01 13:22:06 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
fmt.Printf("Worker spawned\n")
|
2024-11-18 15:50:17 +00:00
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
|
|
|
if err != nil && errors.Is(err, context.Canceled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
2021-12-01 13:22:06 +00:00
|
|
|
obtainedWorkCh <- got
|
|
|
|
}
|
|
|
|
}()
|
2024-01-10 14:34:44 +00:00
|
|
|
default:
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, task3, got)
|
|
|
|
assert.False(t, got.ShouldRun(), "expect task3 should not run, task1 succeeded but task2 failed")
|
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):
|
2024-10-08 21:06:58 +00:00
|
|
|
info := q.Info(ctx)
|
2021-05-25 11:05:31 +00:00
|
|
|
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) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2019-07-22 10:43:59 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
task1 := genDummyTask()
|
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
|
|
|
}
|
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q, _ := NewMemoryQueue(ctx).(*fifo)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NotNil(t, q)
|
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(ctx, []*model.Task{task2, task3, task1}))
|
2019-07-22 10:43:59 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, task1, got)
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Error(ctx, got.ID, fmt.Errorf("exit code 1, there was an error")))
|
2019-07-22 10:43:59 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err = q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, task2, got)
|
|
|
|
assert.False(t, got.ShouldRun(), "expect task2 should not run, since task1 failed")
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Done(ctx, got.ID, model.StatusSkipped))
|
2019-07-22 10:43:59 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
got, err = q.Poll(ctx, 1, filterFnTrue)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, task3, got)
|
|
|
|
assert.False(t, got.ShouldRun(), "expect task3 should not run, task1 failed, thus task2 was skipped, task3 should be skipped too")
|
2019-07-22 10:43:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 06:36:13 +00:00
|
|
|
func TestFifoCancel(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2019-06-19 06:36:13 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
task1 := genDummyTask()
|
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"},
|
|
|
|
}
|
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q, _ := NewMemoryQueue(ctx).(*fifo)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NotNil(t, q)
|
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(ctx, []*model.Task{task2, task3, task1}))
|
2019-06-19 06:36:13 +00:00
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
_, _ = q.Poll(ctx, 1, filterFnTrue)
|
|
|
|
assert.NoError(t, q.Error(ctx, task1.ID, fmt.Errorf("canceled")))
|
|
|
|
assert.NoError(t, q.Error(ctx, task2.ID, fmt.Errorf("canceled")))
|
|
|
|
assert.NoError(t, q.Error(ctx, task3.ID, fmt.Errorf("canceled")))
|
|
|
|
info := q.Info(ctx)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Len(t, info.Pending, 0, "all pipelines should be canceled")
|
2024-11-18 15:50:17 +00:00
|
|
|
|
|
|
|
time.Sleep(processTimeInterval * 2)
|
|
|
|
info = q.Info(ctx)
|
|
|
|
assert.Len(t, info.Pending, 2, "canceled are rescheduled")
|
|
|
|
assert.Len(t, info.Running, 0, "canceled are rescheduled")
|
|
|
|
assert.Len(t, info.WaitingOnDeps, 0, "canceled are rescheduled")
|
2019-06-19 06:36:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 07:19:14 +00:00
|
|
|
func TestFifoPause(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2019-06-28 07:19:14 +00:00
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q, _ := NewMemoryQueue(ctx).(*fifo)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NotNil(t, q)
|
|
|
|
|
|
|
|
dummyTask := genDummyTask()
|
|
|
|
|
2019-06-28 07:19:14 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2024-10-08 21:06:58 +00:00
|
|
|
_, _ = q.Poll(ctx, 1, filterFnTrue)
|
2019-06-28 07:19:14 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
q.Pause()
|
|
|
|
t0 := time.Now()
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NoError(t, q.Push(ctx, dummyTask))
|
|
|
|
waitForProcess()
|
2019-06-28 07:19:14 +00:00
|
|
|
q.Resume()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
t1 := time.Now()
|
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Greater(t, t1.Sub(t0), 20*time.Millisecond, "should have waited til resume")
|
2019-06-28 07:19:14 +00:00
|
|
|
|
|
|
|
q.Pause()
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NoError(t, q.Push(ctx, dummyTask))
|
2019-06-28 07:19:14 +00:00
|
|
|
q.Resume()
|
2024-10-08 21:06:58 +00:00
|
|
|
_, _ = q.Poll(ctx, 1, filterFnTrue)
|
2019-06-28 07:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFifoPauseResume(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2019-06-28 07:19:14 +00:00
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q, _ := NewMemoryQueue(ctx).(*fifo)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NotNil(t, q)
|
|
|
|
|
|
|
|
dummyTask := genDummyTask()
|
|
|
|
|
2019-06-28 07:19:14 +00:00
|
|
|
q.Pause()
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NoError(t, q.Push(ctx, dummyTask))
|
2019-06-28 07:19:14 +00:00
|
|
|
q.Resume()
|
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
_, _ = q.Poll(ctx, 1, filterFnTrue)
|
2019-06-28 07:19:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 14:23:56 +00:00
|
|
|
func TestWaitingVsPending(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
2019-07-09 14:23:56 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
task1 := genDummyTask()
|
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"},
|
|
|
|
}
|
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q, _ := NewMemoryQueue(ctx).(*fifo)
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NotNil(t, q)
|
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(ctx, []*model.Task{task2, task3, task1}))
|
2019-07-09 14:23:56 +00:00
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
got, _ := q.Poll(ctx, 1, filterFnTrue)
|
2019-07-09 14:23:56 +00:00
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
info := q.Info(ctx)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, 2, info.Stats.WaitingOnDeps)
|
2019-07-09 14:23:56 +00:00
|
|
|
|
2024-10-08 21:06:58 +00:00
|
|
|
assert.NoError(t, q.Error(ctx, got.ID, fmt.Errorf("exit code 1, there was an error")))
|
|
|
|
got, err := q.Poll(ctx, 1, filterFnTrue)
|
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
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
waitForProcess()
|
2024-10-08 21:06:58 +00:00
|
|
|
info = q.Info(ctx)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, 0, info.Stats.WaitingOnDeps)
|
|
|
|
assert.Equal(t, 1, info.Stats.Pending)
|
2019-07-09 14:23:56 +00:00
|
|
|
}
|
|
|
|
|
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"},
|
|
|
|
}
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.False(t, task.ShouldRun(), "expect task to not run, it runs on failure only")
|
2019-06-17 07:06:36 +00:00
|
|
|
|
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"},
|
|
|
|
}
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.True(t, task.ShouldRun())
|
2019-06-17 07:06:36 +00:00
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.False(t, task.ShouldRun())
|
2019-06-17 07:06:36 +00:00
|
|
|
|
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"},
|
|
|
|
}
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.True(t, task.ShouldRun())
|
2019-06-17 07:06:36 +00:00
|
|
|
|
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"},
|
|
|
|
}
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.True(t, task.ShouldRun())
|
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
|
|
|
},
|
|
|
|
}
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.False(t, task.ShouldRun(), "task should not run if dependency is skipped")
|
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
|
|
|
},
|
|
|
|
RunOn: []string{"failure"},
|
|
|
|
}
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.True(t, task.ShouldRun(), "on failure, tasks should run on skipped deps, something failed higher up the chain")
|
2019-06-17 07:06:36 +00:00
|
|
|
}
|
2024-10-08 21:06:58 +00:00
|
|
|
|
|
|
|
func TestFifoWithScoring(t *testing.T) {
|
2024-11-18 15:50:17 +00:00
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
t.Cleanup(func() { cancel(nil) })
|
|
|
|
|
2024-11-05 03:03:40 +00:00
|
|
|
q := NewMemoryQueue(ctx)
|
2024-10-08 21:06:58 +00:00
|
|
|
|
|
|
|
// Create tasks with different labels
|
|
|
|
tasks := []*model.Task{
|
|
|
|
{ID: "1", Labels: map[string]string{"org-id": "123", "platform": "linux"}},
|
|
|
|
{ID: "2", Labels: map[string]string{"org-id": "456", "platform": "linux"}},
|
|
|
|
{ID: "3", Labels: map[string]string{"org-id": "789", "platform": "windows"}},
|
|
|
|
{ID: "4", Labels: map[string]string{"org-id": "123", "platform": "linux"}},
|
|
|
|
{ID: "5", Labels: map[string]string{"org-id": "*", "platform": "linux"}},
|
|
|
|
}
|
|
|
|
|
2024-11-18 15:50:17 +00:00
|
|
|
assert.NoError(t, q.PushAtOnce(ctx, tasks))
|
2024-10-08 21:06:58 +00:00
|
|
|
|
|
|
|
// Create filter functions for different workers
|
|
|
|
filters := map[int]FilterFn{
|
|
|
|
1: func(task *model.Task) (bool, int) {
|
|
|
|
if task.Labels["org-id"] == "123" {
|
|
|
|
return true, 20
|
|
|
|
}
|
|
|
|
if task.Labels["platform"] == "linux" {
|
|
|
|
return true, 10
|
|
|
|
}
|
|
|
|
return true, 1
|
|
|
|
},
|
|
|
|
2: func(task *model.Task) (bool, int) {
|
|
|
|
if task.Labels["org-id"] == "456" {
|
|
|
|
return true, 20
|
|
|
|
}
|
|
|
|
if task.Labels["platform"] == "linux" {
|
|
|
|
return true, 10
|
|
|
|
}
|
|
|
|
return true, 1
|
|
|
|
},
|
|
|
|
3: func(task *model.Task) (bool, int) {
|
|
|
|
if task.Labels["platform"] == "windows" {
|
|
|
|
return true, 20
|
|
|
|
}
|
|
|
|
return true, 1
|
|
|
|
},
|
|
|
|
4: func(task *model.Task) (bool, int) {
|
|
|
|
if task.Labels["org-id"] == "123" {
|
|
|
|
return true, 20
|
|
|
|
}
|
|
|
|
if task.Labels["platform"] == "linux" {
|
|
|
|
return true, 10
|
|
|
|
}
|
|
|
|
return true, 1
|
|
|
|
},
|
|
|
|
5: func(task *model.Task) (bool, int) {
|
|
|
|
if task.Labels["org-id"] == "*" {
|
|
|
|
return true, 15
|
|
|
|
}
|
|
|
|
return true, 1
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start polling in separate goroutines
|
|
|
|
results := make(chan *model.Task, 5)
|
|
|
|
for i := 1; i <= 5; i++ {
|
|
|
|
go func(n int) {
|
|
|
|
task, err := q.Poll(ctx, int64(n), filters[n])
|
|
|
|
assert.NoError(t, err)
|
|
|
|
results <- task
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect results
|
|
|
|
receivedTasks := make(map[string]int64)
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
select {
|
|
|
|
case task := <-results:
|
|
|
|
receivedTasks[task.ID] = task.AgentID
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("Timeout waiting for tasks")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, receivedTasks, 5, "All tasks should be assigned")
|
|
|
|
|
|
|
|
// Define expected agent assignments
|
|
|
|
// Map structure: {taskID: []possible agentIDs}
|
|
|
|
// - taskID "1" and "4" can be assigned to agents 1 or 4 (org-id "123")
|
|
|
|
// - taskID "2" should be assigned to agent 2 (org-id "456")
|
|
|
|
// - taskID "3" should be assigned to agent 3 (platform "windows")
|
|
|
|
// - taskID "5" should be assigned to agent 5 (org-id "*")
|
|
|
|
expectedAssignments := map[string][]int64{
|
|
|
|
"1": {1, 4},
|
|
|
|
"2": {2},
|
|
|
|
"3": {3},
|
|
|
|
"4": {1, 4},
|
|
|
|
"5": {5},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if tasks are assigned as expected
|
|
|
|
for taskID, expectedAgents := range expectedAssignments {
|
|
|
|
agentID, ok := receivedTasks[taskID]
|
|
|
|
assert.True(t, ok, "Task %s should be assigned", taskID)
|
|
|
|
assert.Contains(t, expectedAgents, agentID, "Task %s should be assigned to one of the expected agents", taskID)
|
|
|
|
}
|
|
|
|
}
|