request_test.go 3.6 KB

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