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

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