produce_response_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package protocol
  2. import "testing"
  3. import "sarama/types"
  4. var (
  5. produceResponseNoBlocks = []byte{
  6. 0x00, 0x00, 0x00, 0x00}
  7. produceResponseManyBlocks = []byte{
  8. 0x00, 0x00, 0x00, 0x02,
  9. 0x00, 0x03, 'f', 'o', 'o',
  10. 0x00, 0x00, 0x00, 0x00,
  11. 0x00, 0x03, 'b', 'a', 'r',
  12. 0x00, 0x00, 0x00, 0x02,
  13. 0x00, 0x00, 0x00, 0x01,
  14. 0x00, 0x00,
  15. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
  16. 0x00, 0x00, 0x00, 0x02,
  17. 0x00, 0x02,
  18. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  19. )
  20. func TestProduceResponse(t *testing.T) {
  21. response := ProduceResponse{}
  22. testDecodable(t, "no blocks", &response, produceResponseNoBlocks)
  23. if len(response.Blocks) != 0 {
  24. t.Error("Decoding produced", len(response.Blocks), "topics where there were none")
  25. }
  26. testDecodable(t, "many blocks", &response, produceResponseManyBlocks)
  27. if len(response.Blocks) != 2 {
  28. t.Error("Decoding produced", len(response.Blocks), "topics where there were 2")
  29. }
  30. if len(response.Blocks["foo"]) != 0 {
  31. t.Error("Decoding produced", len(response.Blocks["foo"]), "partitions for 'foo' where there were none")
  32. }
  33. if len(response.Blocks["bar"]) != 2 {
  34. t.Error("Decoding produced", len(response.Blocks["bar"]), "partitions for 'bar' where there were two")
  35. }
  36. block := response.GetBlock("bar", 1)
  37. if block == nil {
  38. t.Error("Decoding did not produce a block for bar/1")
  39. } else {
  40. if block.Err != types.NO_ERROR {
  41. t.Error("Decoding failed for bar/1/Err, got:", int16(block.Err))
  42. }
  43. if block.Offset != 0xFF {
  44. t.Error("Decoding failed for bar/1/Offset, got:", block.Offset)
  45. }
  46. }
  47. block = response.GetBlock("bar", 2)
  48. if block == nil {
  49. t.Error("Decoding did not produce a block for bar/2")
  50. } else {
  51. if block.Err != types.INVALID_MESSAGE {
  52. t.Error("Decoding failed for bar/2/Err, got:", int16(block.Err))
  53. }
  54. if block.Offset != 0 {
  55. t.Error("Decoding failed for bar/2/Offset, got:", block.Offset)
  56. }
  57. }
  58. }