fetch_response_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package sarama
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. var (
  7. emptyFetchResponse = []byte{
  8. 0x00, 0x00, 0x00, 0x00}
  9. oneMessageFetchResponse = []byte{
  10. 0x00, 0x00, 0x00, 0x01,
  11. 0x00, 0x05, 't', 'o', 'p', 'i', 'c',
  12. 0x00, 0x00, 0x00, 0x01,
  13. 0x00, 0x00, 0x00, 0x05,
  14. 0x00, 0x01,
  15. 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x10, 0x10,
  16. 0x00, 0x00, 0x00, 0x1C,
  17. // messageSet
  18. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00,
  19. 0x00, 0x00, 0x00, 0x10,
  20. // message
  21. 0x23, 0x96, 0x4a, 0xf7, // CRC
  22. 0x00,
  23. 0x00,
  24. 0xFF, 0xFF, 0xFF, 0xFF,
  25. 0x00, 0x00, 0x00, 0x02, 0x00, 0xEE}
  26. )
  27. func TestEmptyFetchResponse(t *testing.T) {
  28. response := FetchResponse{}
  29. testDecodable(t, "empty", &response, emptyFetchResponse)
  30. if len(response.Blocks) != 0 {
  31. t.Error("Decoding produced topic blocks where there were none.")
  32. }
  33. }
  34. func TestOneMessageFetchResponse(t *testing.T) {
  35. response := FetchResponse{}
  36. testDecodable(t, "one message", &response, oneMessageFetchResponse)
  37. if len(response.Blocks) != 1 {
  38. t.Fatal("Decoding produced incorrect number of topic blocks.")
  39. }
  40. if len(response.Blocks["topic"]) != 1 {
  41. t.Fatal("Decoding produced incorrect number of partition blocks for topic.")
  42. }
  43. block := response.GetBlock("topic", 5)
  44. if block == nil {
  45. t.Fatal("GetBlock didn't return block.")
  46. }
  47. if block.Err != OffsetOutOfRange {
  48. t.Error("Decoding didn't produce correct error code.")
  49. }
  50. if block.HighWaterMarkOffset != 0x10101010 {
  51. t.Error("Decoding didn't produce correct high water mark offset.")
  52. }
  53. if block.MsgSet.PartialTrailingMessage {
  54. t.Error("Decoding detected a partial trailing message where there wasn't one.")
  55. }
  56. if len(block.MsgSet.Messages) != 1 {
  57. t.Fatal("Decoding produced incorrect number of messages.")
  58. }
  59. msgBlock := block.MsgSet.Messages[0]
  60. if msgBlock.Offset != 0x550000 {
  61. t.Error("Decoding produced incorrect message offset.")
  62. }
  63. msg := msgBlock.Msg
  64. if msg.Codec != CompressionNone {
  65. t.Error("Decoding produced incorrect message compression.")
  66. }
  67. if msg.Key != nil {
  68. t.Error("Decoding produced message key where there was none.")
  69. }
  70. if !bytes.Equal(msg.Value, []byte{0x00, 0xEE}) {
  71. t.Error("Decoding produced incorrect message value.")
  72. }
  73. }