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"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
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)
|
|
|
|
|
|
|
|
q, err := queue.NewChannelUniqueQueue(func(data ...queue.Data) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}, queue.ChannelUniqueQueueConfiguration{
|
|
|
|
WorkerPoolConfiguration: queue.WorkerPoolConfiguration{
|
|
|
|
QueueLength: 10,
|
|
|
|
BatchLength: 1,
|
|
|
|
},
|
|
|
|
Workers: 1,
|
|
|
|
Name: "temporary-queue",
|
|
|
|
}, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
queueShutdown := []func(){}
|
|
|
|
queueTerminate := []func(){}
|
|
|
|
|
|
|
|
prQueue = q.(queue.UniqueQueue)
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
pr := db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest)
|
2019-12-06 19:44:10 -07:00
|
|
|
AddToTaskQueue(pr)
|
|
|
|
|
2020-02-02 16:19:58 -07:00
|
|
|
assert.Eventually(t, func() bool {
|
2021-09-19 05:49:59 -06:00
|
|
|
pr = db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest)
|
2020-02-02 16:19:58 -07:00
|
|
|
return pr.Status == models.PullRequestStatusChecking
|
|
|
|
}, 1*time.Second, 100*time.Millisecond)
|
|
|
|
|
|
|
|
has, err := prQueue.Has(strconv.FormatInt(pr.ID, 10))
|
|
|
|
assert.True(t, has)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-05-15 08:22:26 -06:00
|
|
|
prQueue.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")
|
|
|
|
}
|
|
|
|
|
2020-02-02 16:19:58 -07:00
|
|
|
has, err = prQueue.Has(strconv.FormatInt(pr.ID, 10))
|
|
|
|
assert.False(t, has)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
pr = db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest)
|
2019-12-06 19:44:10 -07:00
|
|
|
assert.Equal(t, models.PullRequestStatusChecking, pr.Status)
|
2020-02-02 16:19:58 -07:00
|
|
|
|
|
|
|
for _, callback := range queueShutdown {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
for _, callback := range queueTerminate {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
|
|
|
|
prQueue = nil
|
2019-12-06 19:44:10 -07:00
|
|
|
}
|