create_partitions_response_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package sarama
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. )
  7. var (
  8. createPartitionResponseSuccess = []byte{
  9. 0, 0, 0, 100, // throttleTimeMs
  10. 0, 0, 0, 1,
  11. 0, 5, 't', 'o', 'p', 'i', 'c',
  12. 0, 0, // no error
  13. 255, 255, // no error message
  14. }
  15. createPartitionResponseFail = []byte{
  16. 0, 0, 0, 100, // throttleTimeMs
  17. 0, 0, 0, 1,
  18. 0, 5, 't', 'o', 'p', 'i', 'c',
  19. 0, 37, // partition error
  20. 0, 5, 'e', 'r', 'r', 'o', 'r',
  21. }
  22. )
  23. func TestCreatePartitionsResponse(t *testing.T) {
  24. resp := &CreatePartitionsResponse{
  25. ThrottleTime: 100 * time.Millisecond,
  26. TopicPartitionErrors: map[string]*TopicPartitionError{
  27. "topic": &TopicPartitionError{},
  28. },
  29. }
  30. testResponse(t, "success", resp, createPartitionResponseSuccess)
  31. decodedresp := new(CreatePartitionsResponse)
  32. testVersionDecodable(t, "success", decodedresp, createPartitionResponseSuccess, 0)
  33. if !reflect.DeepEqual(decodedresp, resp) {
  34. t.Errorf("Decoding error: expected %v but got %v", decodedresp, resp)
  35. }
  36. errMsg := "error"
  37. resp.TopicPartitionErrors["topic"].Err = ErrInvalidPartitions
  38. resp.TopicPartitionErrors["topic"].ErrMsg = &errMsg
  39. testResponse(t, "with errors", resp, createPartitionResponseFail)
  40. decodedresp = new(CreatePartitionsResponse)
  41. testVersionDecodable(t, "with errors", decodedresp, createPartitionResponseFail, 0)
  42. if !reflect.DeepEqual(decodedresp, resp) {
  43. t.Errorf("Decoding error: expected %v but got %v", decodedresp, resp)
  44. }
  45. }
  46. func TestTopicPartitionError(t *testing.T) {
  47. // Assert that TopicPartitionError satisfies error interface
  48. var err error = &TopicPartitionError{
  49. Err: ErrTopicAuthorizationFailed,
  50. }
  51. got := err.Error()
  52. want := ErrTopicAuthorizationFailed.Error()
  53. if got != want {
  54. t.Errorf("TopicPartitionError.Error() = %v; want %v", got, want)
  55. }
  56. msg := "reason why topic authorization failed"
  57. err = &TopicPartitionError{
  58. Err: ErrTopicAuthorizationFailed,
  59. ErrMsg: &msg,
  60. }
  61. got = err.Error()
  62. want = ErrTopicAuthorizationFailed.Error() + " - " + msg
  63. if got != want {
  64. t.Errorf("TopicPartitionError.Error() = %v; want %v", got, want)
  65. }
  66. }