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