message_test.go 6.1 KB

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