queue_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package rq
  2. import (
  3. "strconv"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestQueueWithTimeout(t *testing.T) {
  9. consumer, err := wrapWithTimeout(WithHandle(func(string) error {
  10. time.Sleep(time.Minute)
  11. return nil
  12. }), 100)()
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. assert.Equal(t, ErrTimeout, consumer.Consume("any"))
  17. }
  18. func TestQueueWithoutTimeout(t *testing.T) {
  19. consumer, err := wrapWithTimeout(WithHandle(func(string) error {
  20. return nil
  21. }), 3600000)()
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. assert.Nil(t, consumer.Consume("any"))
  26. }
  27. func BenchmarkQueue(b *testing.B) {
  28. b.ReportAllocs()
  29. consumer, err := WithHandle(func(string) error {
  30. return nil
  31. })()
  32. if err != nil {
  33. b.Fatal(err)
  34. }
  35. for i := 0; i < b.N; i++ {
  36. consumer.Consume(strconv.Itoa(i))
  37. }
  38. }
  39. func BenchmarkQueueWithTimeout(b *testing.B) {
  40. b.ReportAllocs()
  41. consumer, err := wrapWithTimeout(WithHandle(func(string) error {
  42. return nil
  43. }), 1000)()
  44. if err != nil {
  45. b.Fatal(err)
  46. }
  47. for i := 0; i < b.N; i++ {
  48. consumer.Consume(strconv.Itoa(i))
  49. }
  50. }