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