summaryrefslogtreecommitdiffstats
path: root/services/pull/check_test.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-12-12 23:57:56 +0100
commite68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch)
tree97775d6c13b0f416af55314eb6a89ef792474615 /services/pull/check_test.go
parentInitial commit. (diff)
downloadforgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz
forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'services/pull/check_test.go')
-rw-r--r--services/pull/check_test.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/services/pull/check_test.go b/services/pull/check_test.go
new file mode 100644
index 0000000..b99cf01
--- /dev/null
+++ b/services/pull/check_test.go
@@ -0,0 +1,70 @@
+// Copyright 2019 The Gitea Authors.
+// All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package pull
+
+import (
+ "context"
+ "strconv"
+ "testing"
+ "time"
+
+ "code.gitea.io/gitea/models/db"
+ issues_model "code.gitea.io/gitea/models/issues"
+ "code.gitea.io/gitea/models/unittest"
+ "code.gitea.io/gitea/modules/queue"
+ "code.gitea.io/gitea/modules/setting"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestPullRequest_AddToTaskQueue(t *testing.T) {
+ require.NoError(t, unittest.PrepareTestDatabase())
+
+ idChan := make(chan int64, 10)
+ testHandler := func(items ...string) []string {
+ for _, s := range items {
+ id, _ := strconv.ParseInt(s, 10, 64)
+ idChan <- id
+ }
+ return nil
+ }
+
+ cfg, err := setting.GetQueueSettings(setting.CfgProvider, "pr_patch_checker")
+ require.NoError(t, err)
+ prPatchCheckerQueue, err = queue.NewWorkerPoolQueueWithContext(context.Background(), "pr_patch_checker", cfg, testHandler, true)
+ require.NoError(t, err)
+
+ pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
+ AddToTaskQueue(db.DefaultContext, pr)
+
+ assert.Eventually(t, func() bool {
+ pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
+ return pr.Status == issues_model.PullRequestStatusChecking
+ }, 1*time.Second, 100*time.Millisecond)
+
+ has, err := prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))
+ assert.True(t, has)
+ require.NoError(t, err)
+
+ go prPatchCheckerQueue.Run()
+
+ select {
+ case id := <-idChan:
+ assert.EqualValues(t, pr.ID, id)
+ case <-time.After(time.Second):
+ assert.FailNow(t, "Timeout: nothing was added to pullRequestQueue")
+ }
+
+ has, err = prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))
+ assert.False(t, has)
+ require.NoError(t, err)
+
+ pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
+ assert.Equal(t, issues_model.PullRequestStatusChecking, pr.Status)
+
+ prPatchCheckerQueue.ShutdownWait(5 * time.Second)
+ prPatchCheckerQueue = nil
+}