2019-12-07 08:14:10 +05:30
|
|
|
// 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 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2020-02-03 04:49:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2019-12-07 08:14:10 +05:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPullRequest_AddToTaskQueue(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-12-07 08:14:10 +05:30
|
|
|
|
2020-02-03 04:49:58 +05:30
|
|
|
idChan := make(chan int64, 10)
|
|
|
|
|
2022-01-23 02:52:14 +05:30
|
|
|
q, err := queue.NewChannelUniqueQueue(func(data ...queue.Data) []queue.Data {
|
2020-02-03 04:49:58 +05:30
|
|
|
for _, datum := range data {
|
2020-12-25 15:29:32 +05:30
|
|
|
id, _ := strconv.ParseInt(datum.(string), 10, 64)
|
2020-02-03 04:49:58 +05:30
|
|
|
idChan <- id
|
|
|
|
}
|
2022-01-23 02:52:14 +05:30
|
|
|
return nil
|
2020-02-03 04:49:58 +05:30
|
|
|
}, queue.ChannelUniqueQueueConfiguration{
|
|
|
|
WorkerPoolConfiguration: queue.WorkerPoolConfiguration{
|
|
|
|
QueueLength: 10,
|
|
|
|
BatchLength: 1,
|
2022-03-31 22:31:43 +05:30
|
|
|
Name: "temporary-queue",
|
2020-02-03 04:49:58 +05:30
|
|
|
},
|
|
|
|
Workers: 1,
|
|
|
|
}, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
queueShutdown := []func(){}
|
|
|
|
queueTerminate := []func(){}
|
|
|
|
|
2022-05-02 05:24:44 +05:30
|
|
|
prPatchCheckerQueue = q.(queue.UniqueQueue)
|
2020-02-03 04:49:58 +05:30
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}).(*issues_model.PullRequest)
|
2019-12-07 08:14:10 +05:30
|
|
|
AddToTaskQueue(pr)
|
|
|
|
|
2020-02-03 04:49:58 +05:30
|
|
|
assert.Eventually(t, func() bool {
|
2022-06-13 15:07:59 +05:30
|
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}).(*issues_model.PullRequest)
|
|
|
|
return pr.Status == issues_model.PullRequestStatusChecking
|
2020-02-03 04:49:58 +05:30
|
|
|
}, 1*time.Second, 100*time.Millisecond)
|
|
|
|
|
2022-05-02 05:24:44 +05:30
|
|
|
has, err := prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))
|
2020-02-03 04:49:58 +05:30
|
|
|
assert.True(t, has)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-05-02 05:24:44 +05:30
|
|
|
prPatchCheckerQueue.Run(func(shutdown func()) {
|
2020-02-03 04:49:58 +05:30
|
|
|
queueShutdown = append(queueShutdown, shutdown)
|
2021-05-15 19:52:26 +05:30
|
|
|
}, func(terminate func()) {
|
2020-02-03 04:49:58 +05:30
|
|
|
queueTerminate = append(queueTerminate, terminate)
|
|
|
|
})
|
|
|
|
|
2019-12-07 08:14:10 +05:30
|
|
|
select {
|
2020-02-03 04:49:58 +05:30
|
|
|
case id := <-idChan:
|
|
|
|
assert.EqualValues(t, pr.ID, id)
|
2019-12-07 08:14:10 +05:30
|
|
|
case <-time.After(time.Second):
|
|
|
|
assert.Fail(t, "Timeout: nothing was added to pullRequestQueue")
|
|
|
|
}
|
|
|
|
|
2022-05-02 05:24:44 +05:30
|
|
|
has, err = prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))
|
2020-02-03 04:49:58 +05:30
|
|
|
assert.False(t, has)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}).(*issues_model.PullRequest)
|
|
|
|
assert.Equal(t, issues_model.PullRequestStatusChecking, pr.Status)
|
2020-02-03 04:49:58 +05:30
|
|
|
|
|
|
|
for _, callback := range queueShutdown {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
for _, callback := range queueTerminate {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
|
2022-05-02 05:24:44 +05:30
|
|
|
prPatchCheckerQueue = nil
|
2019-12-07 08:14:10 +05:30
|
|
|
}
|