create_topics_response_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package sarama
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. var (
  7. createTopicsResponseV0 = []byte{
  8. 0, 0, 0, 1,
  9. 0, 5, 't', 'o', 'p', 'i', 'c',
  10. 0, 42,
  11. }
  12. createTopicsResponseV1 = []byte{
  13. 0, 0, 0, 1,
  14. 0, 5, 't', 'o', 'p', 'i', 'c',
  15. 0, 42,
  16. 0, 3, 'm', 's', 'g',
  17. }
  18. createTopicsResponseV2 = []byte{
  19. 0, 0, 0, 100,
  20. 0, 0, 0, 1,
  21. 0, 5, 't', 'o', 'p', 'i', 'c',
  22. 0, 42,
  23. 0, 3, 'm', 's', 'g',
  24. }
  25. )
  26. func TestCreateTopicsResponse(t *testing.T) {
  27. resp := &CreateTopicsResponse{
  28. TopicErrors: map[string]*TopicError{
  29. "topic": {
  30. Err: ErrInvalidRequest,
  31. },
  32. },
  33. }
  34. testResponse(t, "version 0", resp, createTopicsResponseV0)
  35. resp.Version = 1
  36. msg := "msg"
  37. resp.TopicErrors["topic"].ErrMsg = &msg
  38. testResponse(t, "version 1", resp, createTopicsResponseV1)
  39. resp.Version = 2
  40. resp.ThrottleTime = 100 * time.Millisecond
  41. testResponse(t, "version 2", resp, createTopicsResponseV2)
  42. }
  43. func TestTopicError(t *testing.T) {
  44. // Assert that TopicError satisfies error interface
  45. var err error = &TopicError{
  46. Err: ErrTopicAuthorizationFailed,
  47. }
  48. got := err.Error()
  49. want := ErrTopicAuthorizationFailed.Error()
  50. if got != want {
  51. t.Errorf("TopicError.Error() = %v; want %v", got, want)
  52. }
  53. msg := "reason why topic authorization failed"
  54. err = &TopicError{
  55. Err: ErrTopicAuthorizationFailed,
  56. ErrMsg: &msg,
  57. }
  58. got = err.Error()
  59. want = ErrTopicAuthorizationFailed.Error() + " - " + msg
  60. if got != want {
  61. t.Errorf("TopicError.Error() = %v; want %v", got, want)
  62. }
  63. }