// Copyright 2026 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. package queue import ( "testing" "github.com/stretchr/testify/assert" "go.woodpecker-ci.org/woodpecker/v3/server/model" store_mocks "go.woodpecker-ci.org/woodpecker/v3/server/store/mocks" "go.woodpecker-ci.org/woodpecker/v3/server/store/types" ) // A task that lingers in the in-memory queue but is already gone from the // backup store must be dropped on Poll instead of being handed to the agent, // otherwise it loops forever (re-poll, illegal-instruction, resubmit). func TestPersistentQueuePollDropsStaleTask(t *testing.T) { ctx, cancel, q := setupTestQueue(t) defer cancel(nil) store := store_mocks.NewMockStore(t) store.EXPECT().TaskDelete("1").Return(types.ErrRecordNotExist).Once() pq := &persistentQueue{Queue: q, store: store} task := genDummyTask() assert.NoError(t, q.PushAtOnce(ctx, []*model.Task{task})) got, err := pq.Poll(ctx, 1, filterFnTrue) assert.NoError(t, err) assert.Nil(t, got, "stale task must not be returned to the agent") info := q.Info(ctx) assert.Equal(t, 0, info.Stats.Pending, "stale task must be removed from pending") assert.Equal(t, 0, info.Stats.Running, "stale task must be removed from running") } // A task that is still present in the backup store is polled normally. func TestPersistentQueuePollReturnsLiveTask(t *testing.T) { ctx, cancel, q := setupTestQueue(t) defer cancel(nil) store := store_mocks.NewMockStore(t) store.EXPECT().TaskDelete("1").Return(nil).Once() pq := &persistentQueue{Queue: q, store: store} task := genDummyTask() assert.NoError(t, q.PushAtOnce(ctx, []*model.Task{task})) got, err := pq.Poll(ctx, 1, filterFnTrue) assert.NoError(t, err) assert.NotNil(t, got) assert.Equal(t, "1", got.ID) }