request_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package sarama
  2. import (
  3. "bytes"
  4. "reflect"
  5. "testing"
  6. "github.com/davecgh/go-spew/spew"
  7. )
  8. type testRequestBody struct {
  9. }
  10. func (s *testRequestBody) key() int16 {
  11. return 0x666
  12. }
  13. func (s *testRequestBody) version() int16 {
  14. return 0xD2
  15. }
  16. func (s *testRequestBody) encode(pe packetEncoder) error {
  17. return pe.putString("abc")
  18. }
  19. // not specific to request tests, just helper functions for testing structures that
  20. // implement the encoder or decoder interfaces that needed somewhere to live
  21. func testEncodable(t *testing.T, name string, in encoder, expect []byte) {
  22. packet, err := encode(in, nil)
  23. if err != nil {
  24. t.Error(err)
  25. } else if !bytes.Equal(packet, expect) {
  26. t.Error("Encoding", name, "failed\ngot ", packet, "\nwant", expect)
  27. }
  28. }
  29. func testDecodable(t *testing.T, name string, out decoder, in []byte) {
  30. err := decode(in, out)
  31. if err != nil {
  32. t.Error("Decoding", name, "failed:", err)
  33. }
  34. }
  35. func testVersionDecodable(t *testing.T, name string, out versionedDecoder, in []byte, version int16) {
  36. err := versionedDecode(in, out, version)
  37. if err != nil {
  38. t.Error("Decoding", name, "version", version, "failed:", err)
  39. }
  40. }
  41. func testRequest(t *testing.T, name string, rb protocolBody, expected []byte) {
  42. if !rb.requiredVersion().IsAtLeast(MinVersion) {
  43. t.Errorf("Request %s has invalid required version", name)
  44. }
  45. packet := testRequestEncode(t, name, rb, expected)
  46. testRequestDecode(t, name, rb, packet)
  47. }
  48. func testRequestEncode(t *testing.T, name string, rb protocolBody, expected []byte) []byte {
  49. req := &request{correlationID: 123, clientID: "foo", body: rb}
  50. packet, err := encode(req, nil)
  51. headerSize := 14 + len("foo")
  52. if err != nil {
  53. t.Error(err)
  54. } else if !bytes.Equal(packet[headerSize:], expected) {
  55. t.Error("Encoding", name, "failed\ngot ", packet[headerSize:], "\nwant", expected)
  56. }
  57. return packet
  58. }
  59. func testRequestDecode(t *testing.T, name string, rb protocolBody, packet []byte) {
  60. decoded, n, err := decodeRequest(bytes.NewReader(packet))
  61. if err != nil {
  62. t.Error("Failed to decode request", err)
  63. } else if decoded.correlationID != 123 || decoded.clientID != "foo" {
  64. t.Errorf("Decoded header %q is not valid: %+v", name, decoded)
  65. } else if !reflect.DeepEqual(rb, decoded.body) {
  66. t.Error(spew.Sprintf("Decoded request %q does not match the encoded one\nencoded: %+v\ndecoded: %+v", name, rb, decoded.body))
  67. } else if n != len(packet) {
  68. t.Errorf("Decoded request %q bytes: %d does not match the encoded one: %d\n", name, n, len(packet))
  69. } else if rb.version() != decoded.body.version() {
  70. t.Errorf("Decoded request %q version: %d does not match the encoded one: %d\n", name, decoded.body.version(), rb.version())
  71. }
  72. }
  73. func testResponse(t *testing.T, name string, res protocolBody, expected []byte) {
  74. encoded, err := encode(res, nil)
  75. if err != nil {
  76. t.Error(err)
  77. } else if expected != nil && !bytes.Equal(encoded, expected) {
  78. t.Error("Encoding", name, "failed\ngot ", encoded, "\nwant", expected)
  79. }
  80. decoded := reflect.New(reflect.TypeOf(res).Elem()).Interface().(versionedDecoder)
  81. if err := versionedDecode(encoded, decoded, res.version()); err != nil {
  82. t.Error("Decoding", name, "failed:", err)
  83. }
  84. if !reflect.DeepEqual(decoded, res) {
  85. t.Errorf("Decoded response does not match the encoded one\nencoded: %#v\ndecoded: %#v", res, decoded)
  86. }
  87. }
  88. func nullString(s string) *string { return &s }