message_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package sarama
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. var (
  7. emptyMessage = []byte{
  8. 167, 236, 104, 3, // CRC
  9. 0x00, // magic version byte
  10. 0x00, // attribute flags
  11. 0xFF, 0xFF, 0xFF, 0xFF, // key
  12. 0xFF, 0xFF, 0xFF, 0xFF} // value
  13. emptyGzipMessage = []byte{
  14. 97, 79, 149, 90, //CRC
  15. 0x00, // magic version byte
  16. 0x01, // attribute flags
  17. 0xFF, 0xFF, 0xFF, 0xFF, // key
  18. // value
  19. 0x00, 0x00, 0x00, 0x17,
  20. 0x1f, 0x8b,
  21. 0x08,
  22. 0, 0, 9, 110, 136, 0, 255, 1, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}
  23. emptyLZ4Message = []byte{
  24. 132, 219, 238, 101, // CRC
  25. 0x01, // version byte
  26. 0x03, // attribute flags: lz4
  27. 0, 0, 1, 88, 141, 205, 89, 56, // timestamp
  28. 0xFF, 0xFF, 0xFF, 0xFF, // key
  29. 0x00, 0x00, 0x00, 0x0f, // len
  30. 0x04, 0x22, 0x4D, 0x18, // LZ4 magic number
  31. 100, // LZ4 flags: version 01, block indepedant, content checksum
  32. 112, 185, 0, 0, 0, 0, // LZ4 data
  33. 5, 93, 204, 2, // LZ4 checksum
  34. }
  35. emptyBulkSnappyMessage = []byte{
  36. 180, 47, 53, 209, //CRC
  37. 0x00, // magic version byte
  38. 0x02, // attribute flags
  39. 0xFF, 0xFF, 0xFF, 0xFF, // key
  40. 0, 0, 0, 42,
  41. 130, 83, 78, 65, 80, 80, 89, 0, // SNAPPY magic
  42. 0, 0, 0, 1, // min version
  43. 0, 0, 0, 1, // default version
  44. 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}
  45. emptyBulkGzipMessage = []byte{
  46. 139, 160, 63, 141, //CRC
  47. 0x00, // magic version byte
  48. 0x01, // attribute flags
  49. 0xFF, 0xFF, 0xFF, 0xFF, // key
  50. 0x00, 0x00, 0x00, 0x27, // len
  51. 0x1f, 0x8b, // Gzip Magic
  52. 0x08, // deflate compressed
  53. 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}
  54. emptyBulkLZ4Message = []byte{
  55. 246, 12, 188, 129, // CRC
  56. 0x01, // Version
  57. 0x03, // attribute flags (LZ4)
  58. 255, 255, 249, 209, 212, 181, 73, 201, // timestamp
  59. 0xFF, 0xFF, 0xFF, 0xFF, // key
  60. 0x00, 0x00, 0x00, 0x47, // len
  61. 0x04, 0x22, 0x4D, 0x18, // magic number lz4
  62. 100, // lz4 flags 01100100
  63. // version: 01, block indep: 1, block checksum: 0, content size: 0, content checksum: 1, reserved: 00
  64. 112, 185, 52, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 121, 87, 72, 224, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 14, 121, 87, 72, 224, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0,
  65. 71, 129, 23, 111, // LZ4 checksum
  66. }
  67. )
  68. func TestMessageEncoding(t *testing.T) {
  69. message := Message{}
  70. testEncodable(t, "empty", &message, emptyMessage)
  71. message.Value = []byte{}
  72. message.Codec = CompressionGZIP
  73. testEncodable(t, "empty gzip", &message, emptyGzipMessage)
  74. message.Value = []byte{}
  75. message.Codec = CompressionLZ4
  76. message.Timestamp = time.Unix(1479847795, 0)
  77. message.Version = 1
  78. testEncodable(t, "empty lz4", &message, emptyLZ4Message)
  79. }
  80. func TestMessageDecoding(t *testing.T) {
  81. message := Message{}
  82. testDecodable(t, "empty", &message, emptyMessage)
  83. if message.Codec != CompressionNone {
  84. t.Error("Decoding produced compression codec where there was none.")
  85. }
  86. if message.Key != nil {
  87. t.Error("Decoding produced key where there was none.")
  88. }
  89. if message.Value != nil {
  90. t.Error("Decoding produced value where there was none.")
  91. }
  92. if message.Set != nil {
  93. t.Error("Decoding produced set where there was none.")
  94. }
  95. testDecodable(t, "empty gzip", &message, emptyGzipMessage)
  96. if message.Codec != CompressionGZIP {
  97. t.Error("Decoding produced incorrect compression codec (was gzip).")
  98. }
  99. if message.Key != nil {
  100. t.Error("Decoding produced key where there was none.")
  101. }
  102. if message.Value == nil || len(message.Value) != 0 {
  103. t.Error("Decoding produced nil or content-ful value where there was an empty array.")
  104. }
  105. }
  106. func TestMessageDecodingBulkSnappy(t *testing.T) {
  107. message := Message{}
  108. testDecodable(t, "bulk snappy", &message, emptyBulkSnappyMessage)
  109. if message.Codec != CompressionSnappy {
  110. t.Errorf("Decoding produced codec %d, but expected %d.", message.Codec, CompressionSnappy)
  111. }
  112. if message.Key != nil {
  113. t.Errorf("Decoding produced key %+v, but none was expected.", message.Key)
  114. }
  115. if message.Set == nil {
  116. t.Error("Decoding produced no set, but one was expected.")
  117. } else if len(message.Set.Messages) != 2 {
  118. t.Errorf("Decoding produced a set with %d messages, but 2 were expected.", len(message.Set.Messages))
  119. }
  120. }
  121. func TestMessageDecodingBulkGzip(t *testing.T) {
  122. message := Message{}
  123. testDecodable(t, "bulk gzip", &message, emptyBulkGzipMessage)
  124. if message.Codec != CompressionGZIP {
  125. t.Errorf("Decoding produced codec %d, but expected %d.", message.Codec, CompressionGZIP)
  126. }
  127. if message.Key != nil {
  128. t.Errorf("Decoding produced key %+v, but none was expected.", message.Key)
  129. }
  130. if message.Set == nil {
  131. t.Error("Decoding produced no set, but one was expected.")
  132. } else if len(message.Set.Messages) != 2 {
  133. t.Errorf("Decoding produced a set with %d messages, but 2 were expected.", len(message.Set.Messages))
  134. }
  135. }
  136. func TestMessageDecodingBulkLZ4(t *testing.T) {
  137. message := Message{}
  138. testDecodable(t, "bulk lz4", &message, emptyBulkLZ4Message)
  139. if message.Codec != CompressionLZ4 {
  140. t.Errorf("Decoding produced codec %d, but expected %d.", message.Codec, CompressionLZ4)
  141. }
  142. if message.Key != nil {
  143. t.Errorf("Decoding produced key %+v, but none was expected.", message.Key)
  144. }
  145. if message.Set == nil {
  146. t.Error("Decoding produced no set, but one was expected.")
  147. } else if len(message.Set.Messages) != 2 {
  148. t.Errorf("Decoding produced a set with %d messages, but 2 were expected.", len(message.Set.Messages))
  149. }
  150. }