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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "strconv"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/queue"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestPullRequest_AddToTaskQueue(t *testing.T) {
  15. assert.NoError(t, models.PrepareTestDatabase())
  16. idChan := make(chan int64, 10)
  17. q, err := queue.NewChannelUniqueQueue(func(data ...queue.Data) {
  18. for _, datum := range data {
  19. id, _ := strconv.ParseInt(datum.(string), 10, 64)
  20. idChan <- id
  21. }
  22. }, queue.ChannelUniqueQueueConfiguration{
  23. WorkerPoolConfiguration: queue.WorkerPoolConfiguration{
  24. QueueLength: 10,
  25. BatchLength: 1,
  26. },
  27. Workers: 1,
  28. Name: "temporary-queue",
  29. }, "")
  30. assert.NoError(t, err)
  31. queueShutdown := []func(){}
  32. queueTerminate := []func(){}
  33. prQueue = q.(queue.UniqueQueue)
  34. pr := models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest)
  35. AddToTaskQueue(pr)
  36. assert.Eventually(t, func() bool {
  37. pr = models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest)
  38. return pr.Status == models.PullRequestStatusChecking
  39. }, 1*time.Second, 100*time.Millisecond)
  40. has, err := prQueue.Has(strconv.FormatInt(pr.ID, 10))
  41. assert.True(t, has)
  42. assert.NoError(t, err)
  43. prQueue.Run(func(shutdown func()) {
  44. queueShutdown = append(queueShutdown, shutdown)
  45. }, func(terminate func()) {
  46. queueTerminate = append(queueTerminate, terminate)
  47. })
  48. select {
  49. case id := <-idChan:
  50. assert.EqualValues(t, pr.ID, id)
  51. case <-time.After(time.Second):
  52. assert.Fail(t, "Timeout: nothing was added to pullRequestQueue")
  53. }
  54. has, err = prQueue.Has(strconv.FormatInt(pr.ID, 10))
  55. assert.False(t, has)
  56. assert.NoError(t, err)
  57. pr = models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest)
  58. assert.Equal(t, models.PullRequestStatusChecking, pr.Status)
  59. for _, callback := range queueShutdown {
  60. callback()
  61. }
  62. for _, callback := range queueTerminate {
  63. callback()
  64. }
  65. prQueue = nil
  66. }