You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

check_test.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2019 The Gitea Authors.
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package pull
  6. import (
  7. "context"
  8. "strconv"
  9. "testing"
  10. "time"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/queue"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/unknwon/com"
  15. )
  16. func TestPullRequest_AddToTaskQueue(t *testing.T) {
  17. assert.NoError(t, models.PrepareTestDatabase())
  18. idChan := make(chan int64, 10)
  19. q, err := queue.NewChannelUniqueQueue(func(data ...queue.Data) {
  20. for _, datum := range data {
  21. prID := datum.(string)
  22. id := com.StrTo(prID).MustInt64()
  23. idChan <- id
  24. }
  25. }, queue.ChannelUniqueQueueConfiguration{
  26. WorkerPoolConfiguration: queue.WorkerPoolConfiguration{
  27. QueueLength: 10,
  28. BatchLength: 1,
  29. },
  30. Workers: 1,
  31. Name: "temporary-queue",
  32. }, "")
  33. assert.NoError(t, err)
  34. queueShutdown := []func(){}
  35. queueTerminate := []func(){}
  36. prQueue = q.(queue.UniqueQueue)
  37. pr := models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest)
  38. AddToTaskQueue(pr)
  39. assert.Eventually(t, func() bool {
  40. pr = models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest)
  41. return pr.Status == models.PullRequestStatusChecking
  42. }, 1*time.Second, 100*time.Millisecond)
  43. has, err := prQueue.Has(strconv.FormatInt(pr.ID, 10))
  44. assert.True(t, has)
  45. assert.NoError(t, err)
  46. prQueue.Run(func(_ context.Context, shutdown func()) {
  47. queueShutdown = append(queueShutdown, shutdown)
  48. }, func(_ context.Context, terminate func()) {
  49. queueTerminate = append(queueTerminate, terminate)
  50. })
  51. select {
  52. case id := <-idChan:
  53. assert.EqualValues(t, pr.ID, id)
  54. case <-time.After(time.Second):
  55. assert.Fail(t, "Timeout: nothing was added to pullRequestQueue")
  56. }
  57. has, err = prQueue.Has(strconv.FormatInt(pr.ID, 10))
  58. assert.False(t, has)
  59. assert.NoError(t, err)
  60. pr = models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest)
  61. assert.Equal(t, models.PullRequestStatusChecking, pr.Status)
  62. for _, callback := range queueShutdown {
  63. callback()
  64. }
  65. for _, callback := range queueTerminate {
  66. callback()
  67. }
  68. prQueue = nil
  69. }