request_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 testRequestEncode(t *testing.T, name string, rb protocolBody, expected []byte) []byte {
  38. req := &request{correlationID: 123, clientID: "foo", body: rb}
  39. packet, err := encode(req, nil)
  40. headerSize := 14 + len("foo")
  41. if err != nil {
  42. t.Error(err)
  43. } else if !bytes.Equal(packet[headerSize:], expected) {
  44. t.Error("Encoding", name, "failed\ngot ", packet[headerSize:], "\nwant", expected)
  45. }
  46. return packet
  47. }
  48. func testRequestDecode(t *testing.T, name string, rb protocolBody, packet []byte) {
  49. decoded, n, err := decodeRequest(bytes.NewReader(packet))
  50. if err != nil {
  51. t.Error("Failed to decode request", err)
  52. } else if decoded.correlationID != 123 || decoded.clientID != "foo" {
  53. t.Errorf("Decoded header %q is not valid: %+v", name, decoded)
  54. } else if !reflect.DeepEqual(rb, decoded.body) {
  55. t.Error(spew.Sprintf("Decoded request %q does not match the encoded one\nencoded: %+v\ndecoded: %+v", name, rb, decoded.body))
  56. } else if n != len(packet) {
  57. t.Errorf("Decoded request %q bytes: %d does not match the encoded one: %d\n", name, n, len(packet))
  58. } else if rb.version() != decoded.body.version() {
  59. t.Errorf("Decoded request %q version: %d does not match the encoded one: %d\n", name, decoded.body.version(), rb.version())
  60. }
  61. }
  62. func testResponse(t *testing.T, name string, res protocolBody, expected []byte) {
  63. encoded, err := encode(res, nil)
  64. if err != nil {
  65. t.Error(err)
  66. } else if expected != nil && !bytes.Equal(encoded, expected) {
  67. t.Error("Encoding", name, "failed\ngot ", encoded, "\nwant", expected)
  68. }
  69. decoded := reflect.New(reflect.TypeOf(res).Elem()).Interface().(versionedDecoder)
  70. if err := versionedDecode(encoded, decoded, res.version()); err != nil {
  71. t.Error("Decoding", name, "failed:", err)
  72. }
  73. if !reflect.DeepEqual(decoded, res) {
  74. t.Errorf("Decoded response does not match the encoded one\nencoded: %#v\ndecoded: %#v", res, decoded)
  75. }
  76. }
  77. func nullString(s string) *string { return &s }