aboutsummaryrefslogtreecommitdiffstats
path: root/services/pull/check_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'services/pull/check_test.go')
-rw-r--r--services/pull/check_test.go59
1 files changed, 56 insertions, 3 deletions
diff --git a/services/pull/check_test.go b/services/pull/check_test.go
index 48a7774a61..4591edd7aa 100644
--- a/services/pull/check_test.go
+++ b/services/pull/check_test.go
@@ -6,29 +6,82 @@
package pull
import (
+ "context"
"strconv"
"testing"
"time"
"code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/queue"
"github.com/stretchr/testify/assert"
+ "github.com/unknwon/com"
)
func TestPullRequest_AddToTaskQueue(t *testing.T) {
assert.NoError(t, models.PrepareTestDatabase())
+ idChan := make(chan int64, 10)
+
+ q, err := queue.NewChannelUniqueQueue(func(data ...queue.Data) {
+ for _, datum := range data {
+ prID := datum.(string)
+ id := com.StrTo(prID).MustInt64()
+ 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)
+
pr := models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest)
AddToTaskQueue(pr)
+ assert.Eventually(t, func() bool {
+ pr = models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest)
+ 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)
+
+ prQueue.Run(func(_ context.Context, shutdown func()) {
+ queueShutdown = append(queueShutdown, shutdown)
+ }, func(_ context.Context, terminate func()) {
+ queueTerminate = append(queueTerminate, terminate)
+ })
+
select {
- case id := <-pullRequestQueue.Queue():
- assert.EqualValues(t, strconv.FormatInt(pr.ID, 10), id)
+ case id := <-idChan:
+ assert.EqualValues(t, pr.ID, id)
case <-time.After(time.Second):
assert.Fail(t, "Timeout: nothing was added to pullRequestQueue")
}
- assert.True(t, pullRequestQueue.Exist(pr.ID))
+ has, err = prQueue.Has(strconv.FormatInt(pr.ID, 10))
+ assert.False(t, has)
+ assert.NoError(t, err)
+
pr = models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest)
assert.Equal(t, models.PullRequestStatusChecking, pr.Status)
+
+ for _, callback := range queueShutdown {
+ callback()
+ }
+ for _, callback := range queueTerminate {
+ callback()
+ }
+
+ prQueue = nil
}