request_test.go 3.0 KB

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