produce_response_test.go 1.8 KB

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