pool_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package worker // import "miniflux.app/v2/internal/worker"
  4. import (
  5. "testing"
  6. "time"
  7. "miniflux.app/v2/internal/model"
  8. )
  9. func TestPushAfterShutdownDiscardsJobs(t *testing.T) {
  10. pool := NewPool(nil, 2)
  11. pool.Shutdown()
  12. done := make(chan struct{})
  13. go func() {
  14. defer close(done)
  15. pool.Push(model.JobList{{FeedID: 1}, {FeedID: 2}})
  16. }()
  17. select {
  18. case <-done:
  19. case <-time.After(5 * time.Second):
  20. t.Fatal("Push blocked after Shutdown instead of discarding jobs")
  21. }
  22. }
  23. func TestShutdownUnblocksPendingPush(t *testing.T) {
  24. pool := NewPool(nil, 0)
  25. pushed := make(chan struct{})
  26. go func() {
  27. defer close(pushed)
  28. pool.Push(model.JobList{{FeedID: 1}})
  29. }()
  30. // Give Push time to block on the unbuffered queue before shutting down.
  31. time.Sleep(10 * time.Millisecond)
  32. done := make(chan struct{})
  33. go func() {
  34. defer close(done)
  35. pool.Shutdown()
  36. }()
  37. select {
  38. case <-done:
  39. case <-time.After(5 * time.Second):
  40. t.Fatal("Shutdown deadlocked while a Push was pending")
  41. }
  42. select {
  43. case <-pushed:
  44. case <-time.After(5 * time.Second):
  45. t.Fatal("Push remained blocked after Shutdown")
  46. }
  47. }
  48. func TestShutdownIsIdempotent(t *testing.T) {
  49. pool := NewPool(nil, 1)
  50. pool.Shutdown()
  51. pool.Shutdown()
  52. }