2019-12-06 21:44:10 -05:00
|
|
|
// Copyright 2019 The Gitea Authors.
|
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package pull
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2020-02-02 18:19:58 -05:00
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2019-12-06 21:44:10 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPullRequest_AddToTaskQueue(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-12-06 21:44:10 -05:00
|
|
|
|
2020-02-02 18:19:58 -05:00
|
|
|
idChan := make(chan int64, 10)
|
|
|
|
|
2022-01-22 16:22:14 -05:00
|
|
|
q, err := queue.NewChannelUniqueQueue(func(data ...queue.Data) []queue.Data {
|
2020-02-02 18:19:58 -05:00
|
|
|
for _, datum := range data {
|
2020-12-25 04:59:32 -05:00
|
|
|
id, _ := strconv.ParseInt(datum.(string), 10, 64)
|
2020-02-02 18:19:58 -05:00
|
|
|
idChan <- id
|
|
|
|
}
|
2022-01-22 16:22:14 -05:00
|
|
|
return nil
|
2020-02-02 18:19:58 -05:00
|
|
|
}, queue.ChannelUniqueQueueConfiguration{
|
|
|
|
WorkerPoolConfiguration: queue.WorkerPoolConfiguration{
|
|
|
|
QueueLength: 10,
|
|
|
|
BatchLength: 1,
|
2022-03-31 13:01:43 -04:00
|
|
|
Name: "temporary-queue",
|
2020-02-02 18:19:58 -05:00
|
|
|
},
|
|
|
|
Workers: 1,
|
|
|
|
}, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
queueShutdown := []func(){}
|
|
|
|
queueTerminate := []func(){}
|
|
|
|
|
2022-05-01 19:54:44 -04:00
|
|
|
prPatchCheckerQueue = q.(queue.UniqueQueue)
|
2020-02-02 18:19:58 -05:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
2019-12-06 21:44:10 -05:00
|
|
|
AddToTaskQueue(pr)
|
|
|
|
|
2020-02-02 18:19:58 -05:00
|
|
|
assert.Eventually(t, func() bool {
|
2022-08-15 22:22:25 -04:00
|
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
2022-06-13 05:37:59 -04:00
|
|
|
return pr.Status == issues_model.PullRequestStatusChecking
|
2020-02-02 18:19:58 -05:00
|
|
|
}, 1*time.Second, 100*time.Millisecond)
|
|
|
|
|
2022-05-01 19:54:44 -04:00
|
|
|
has, err := prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))
|
2020-02-02 18:19:58 -05:00
|
|
|
assert.True(t, has)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-05-01 19:54:44 -04:00
|
|
|
prPatchCheckerQueue.Run(func(shutdown func()) {
|
2020-02-02 18:19:58 -05:00
|
|
|
queueShutdown = append(queueShutdown, shutdown)
|
2021-05-15 10:22:26 -04:00
|
|
|
}, func(terminate func()) {
|
2020-02-02 18:19:58 -05:00
|
|
|
queueTerminate = append(queueTerminate, terminate)
|
|
|
|
})
|
|
|
|
|
2019-12-06 21:44:10 -05:00
|
|
|
select {
|
2020-02-02 18:19:58 -05:00
|
|
|
case id := <-idChan:
|
|
|
|
assert.EqualValues(t, pr.ID, id)
|
2019-12-06 21:44:10 -05:00
|
|
|
case <-time.After(time.Second):
|
|
|
|
assert.Fail(t, "Timeout: nothing was added to pullRequestQueue")
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:54:44 -04:00
|
|
|
has, err = prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))
|
2020-02-02 18:19:58 -05:00
|
|
|
assert.False(t, has)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
2022-06-13 05:37:59 -04:00
|
|
|
assert.Equal(t, issues_model.PullRequestStatusChecking, pr.Status)
|
2020-02-02 18:19:58 -05:00
|
|
|
|
|
|
|
for _, callback := range queueShutdown {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
for _, callback := range queueTerminate {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:54:44 -04:00
|
|
|
prPatchCheckerQueue = nil
|
2019-12-06 21:44:10 -05:00
|
|
|
}
|