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 (
|
2020-02-02 16:19:58 -07:00
|
|
|
"context"
|
2019-12-06 19:44:10 -07:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
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"
|
2020-02-02 16:19:58 -07:00
|
|
|
"github.com/unknwon/com"
|
2019-12-06 19:44:10 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPullRequest_AddToTaskQueue(t *testing.T) {
|
|
|
|
assert.NoError(t, models.PrepareTestDatabase())
|
|
|
|
|
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 {
|
|
|
|
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)
|
|
|
|
|
2020-02-09 16:09:31 -07:00
|
|
|
pr := models.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 {
|
2020-02-09 16:09:31 -07:00
|
|
|
pr = models.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)
|
|
|
|
|
|
|
|
prQueue.Run(func(_ context.Context, shutdown func()) {
|
|
|
|
queueShutdown = append(queueShutdown, shutdown)
|
|
|
|
}, func(_ context.Context, terminate func()) {
|
|
|
|
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)
|
|
|
|
|
2020-02-09 16:09:31 -07:00
|
|
|
pr = models.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
|
|
|
}
|