message_store_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package redriver
  2. import (
  3. "log"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. func TestMessageStore_TTL(t *testing.T) {
  10. const (
  11. rh = "foo"
  12. ttl = 1 * time.Millisecond
  13. )
  14. m1 := Message{ReceiptHandle: rh}
  15. actual := newMessageStore(ttl)
  16. _, ok := actual.Get(rh)
  17. if ok {
  18. t.Errorf("message found, but not yet stored")
  19. }
  20. actual.Set(m1)
  21. m2, ok := actual.Get(rh)
  22. if !ok {
  23. t.Errorf("message not found, but expected to find it")
  24. }
  25. if m2.ReceiptHandle != rh {
  26. t.Errorf("message RH is %q but key is %q", m2.ReceiptHandle, rh)
  27. }
  28. time.Sleep(3 * ttl)
  29. _, ok = actual.Get(rh)
  30. if ok {
  31. t.Errorf("message found, but expected it to be expired")
  32. }
  33. }
  34. func TestMessageStore_Flush(t *testing.T) {
  35. len := func(ms *messageStore) int {
  36. count := 0
  37. ms.Range(func(_, _ any) bool {
  38. count++
  39. return true
  40. })
  41. return count
  42. }
  43. const limit = 10
  44. ms := newMessageStore(1 * time.Minute)
  45. defer ms.Close()
  46. for i := 0; i < limit; i++ {
  47. ms.Set(Message{ReceiptHandle: strconv.Itoa(i)})
  48. }
  49. if actualLen := len(ms); actualLen != limit {
  50. t.Fatalf("expected %d items, got %d", len(ms), actualLen)
  51. }
  52. ms.Flush()
  53. if actualLen := len(ms); actualLen != 0 {
  54. t.Fatalf("expected no items, got %d", actualLen)
  55. }
  56. }
  57. func TestMessageStore_Set(t *testing.T) {
  58. for _, test := range [...]struct {
  59. name string
  60. input string
  61. expectErr bool
  62. }{
  63. {"valid RH", "foo", false},
  64. {"empty RH", "", true},
  65. } {
  66. t.Run(test.name, func(t *testing.T) {
  67. ms := newMessageStore(time.Minute)
  68. defer ms.Close()
  69. actual := ms.Set(Message{ReceiptHandle: test.input})
  70. if actual != nil != test.expectErr {
  71. t.Errorf("expected error %t, but got error %t", test.expectErr, actual)
  72. }
  73. })
  74. }
  75. }
  76. func TestMessageStore_Get_sad(t *testing.T) {
  77. ms := newMessageStore(time.Minute)
  78. defer ms.Close()
  79. ms.Map.Store("foo", "sad")
  80. _, ok := ms.Get("foo")
  81. if ok {
  82. t.Fatalf("expected failure, but got success")
  83. }
  84. }
  85. func TestMessageStore_expire(t *testing.T) {
  86. const ttl = 1 * time.Millisecond
  87. ms := newMessageStore(ttl)
  88. w := &strings.Builder{}
  89. log.SetOutput(w)
  90. ms.Map.Store(13, Message{ReceiptHandle: "13"})
  91. ms.Map.Store("13", "vendredi")
  92. time.Sleep(3 * ttl)
  93. ms.Close()
  94. s := w.String()
  95. if !strings.Contains(s, "map contains a non-string key") {
  96. t.Errorf("expected expiry to have logged a key warning, but found none")
  97. }
  98. if !strings.Contains(s, "map contains a non-Messsage entry") {
  99. t.Errorf("expected expiry to have logged a data warning, but found none")
  100. }
  101. }