message_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package sarama
  2. import "testing"
  3. var (
  4. emptyMessage = []byte{
  5. 167, 236, 104, 3, // CRC
  6. 0x00, // magic version byte
  7. 0x00, // attribute flags
  8. 0xFF, 0xFF, 0xFF, 0xFF, // key
  9. 0xFF, 0xFF, 0xFF, 0xFF} // value
  10. emptyGzipMessage = []byte{
  11. 97, 79, 149, 90, //CRC
  12. 0x00, // magic version byte
  13. 0x01, // attribute flags
  14. 0xFF, 0xFF, 0xFF, 0xFF, // key
  15. // value
  16. 0x00, 0x00, 0x00, 0x17,
  17. 0x1f, 0x8b,
  18. 0x08,
  19. 0, 0, 9, 110, 136, 0, 255, 1, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}
  20. emptyBulkSnappyMessage = []byte{
  21. 180, 47, 53, 209, //CRC
  22. 0x00, // magic version byte
  23. 0x02, // attribute flags
  24. 0xFF, 0xFF, 0xFF, 0xFF, // key
  25. 0, 0, 0, 42,
  26. 130, 83, 78, 65, 80, 80, 89, 0, // SNAPPY magic
  27. 0, 0, 0, 1, // min version
  28. 0, 0, 0, 1, // default version
  29. 0, 0, 0, 22, 52, 0, 0, 25, 1, 16, 14, 227, 138, 104, 118, 25, 15, 13, 1, 8, 1, 0, 0, 62, 26, 0}
  30. emptyBulkGzipMessage = []byte{
  31. 139, 160, 63, 141, //CRC
  32. 0x00, // magic version byte
  33. 0x01, // attribute flags
  34. 0xFF, 0xFF, 0xFF, 0xFF, // key
  35. 0x00, 0x00, 0x00, 0x27, // len
  36. 0x1f, 0x8b, // Gzip Magic
  37. 0x08, // deflate compressed
  38. 0, 0, 0, 0, 0, 0, 0, 99, 96, 128, 3, 190, 202, 112, 143, 7, 12, 12, 255, 129, 0, 33, 200, 192, 136, 41, 3, 0, 199, 226, 155, 70, 52, 0, 0, 0}
  39. )
  40. func TestMessageEncoding(t *testing.T) {
  41. message := Message{}
  42. testEncodable(t, "empty", &message, emptyMessage)
  43. message.Value = []byte{}
  44. message.Codec = CompressionGZIP
  45. testEncodable(t, "empty gzip", &message, emptyGzipMessage)
  46. }
  47. func TestMessageDecoding(t *testing.T) {
  48. message := Message{}
  49. testDecodable(t, "empty", &message, emptyMessage)
  50. if message.Codec != CompressionNone {
  51. t.Error("Decoding produced compression codec where there was none.")
  52. }
  53. if message.Key != nil {
  54. t.Error("Decoding produced key where there was none.")
  55. }
  56. if message.Value != nil {
  57. t.Error("Decoding produced value where there was none.")
  58. }
  59. if message.Set != nil {
  60. t.Error("Decoding produced set where there was none.")
  61. }
  62. testDecodable(t, "empty gzip", &message, emptyGzipMessage)
  63. if message.Codec != CompressionGZIP {
  64. t.Error("Decoding produced incorrect compression codec (was gzip).")
  65. }
  66. if message.Key != nil {
  67. t.Error("Decoding produced key where there was none.")
  68. }
  69. if message.Value == nil || len(message.Value) != 0 {
  70. t.Error("Decoding produced nil or content-ful value where there was an empty array.")
  71. }
  72. }
  73. func TestMessageDecodingBulkSnappy(t *testing.T) {
  74. message := Message{}
  75. testDecodable(t, "bulk snappy", &message, emptyBulkSnappyMessage)
  76. if message.Codec != CompressionSnappy {
  77. t.Errorf("Decoding produced codec %d, but expected %d.", message.Codec, CompressionSnappy)
  78. }
  79. if message.Key != nil {
  80. t.Errorf("Decoding produced key %+v, but none was expected.", message.Key)
  81. }
  82. if message.Set == nil {
  83. t.Error("Decoding produced no set, but one was expected.")
  84. } else if len(message.Set.Messages) != 2 {
  85. t.Errorf("Decoding produced a set with %d messages, but 2 were expected.", len(message.Set.Messages))
  86. }
  87. }
  88. func TestMessageDecodingBulkGzip(t *testing.T) {
  89. message := Message{}
  90. testDecodable(t, "bulk gzip", &message, emptyBulkGzipMessage)
  91. if message.Codec != CompressionGZIP {
  92. t.Errorf("Decoding produced codec %d, but expected %d.", message.Codec, CompressionGZIP)
  93. }
  94. if message.Key != nil {
  95. t.Errorf("Decoding produced key %+v, but none was expected.", message.Key)
  96. }
  97. if message.Set == nil {
  98. t.Error("Decoding produced no set, but one was expected.")
  99. } else if len(message.Set.Messages) != 2 {
  100. t.Errorf("Decoding produced a set with %d messages, but 2 were expected.", len(message.Set.Messages))
  101. }
  102. }