sync_producer_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package mocks
  2. import (
  3. "testing"
  4. "github.com/Shopify/sarama"
  5. )
  6. func TestMockSyncProducerImplementsSyncProducerInterface(t *testing.T) {
  7. var mp interface{} = &SyncProducer{}
  8. if _, ok := mp.(sarama.SyncProducer); !ok {
  9. t.Error("The mock async producer should implement the sarama.SyncProducer interface.")
  10. }
  11. }
  12. func TestSyncProducerReturnsExpectationsToSendMessage(t *testing.T) {
  13. sp := NewSyncProducer(t, nil)
  14. defer func() {
  15. if err := sp.Close(); err != nil {
  16. t.Error(err)
  17. }
  18. }()
  19. sp.ExpectSendMessageAndSucceed()
  20. sp.ExpectSendMessageAndSucceed()
  21. sp.ExpectSendMessageAndFail(sarama.ErrOutOfBrokers)
  22. msg := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
  23. _, offset, err := sp.SendMessage(msg)
  24. if err != nil {
  25. t.Errorf("The first message should have been produced successfully, but got %s", err)
  26. }
  27. if offset != 1 || offset != msg.Offset {
  28. t.Errorf("The first message should have been assigned offset 1, but got %d", msg.Offset)
  29. }
  30. _, offset, err = sp.SendMessage(msg)
  31. if err != nil {
  32. t.Errorf("The second message should have been produced successfully, but got %s", err)
  33. }
  34. if offset != 2 || offset != msg.Offset {
  35. t.Errorf("The second message should have been assigned offset 2, but got %d", offset)
  36. }
  37. _, _, err = sp.SendMessage(msg)
  38. if err != sarama.ErrOutOfBrokers {
  39. t.Errorf("The third message should not have been produced successfully")
  40. }
  41. if err := sp.Close(); err != nil {
  42. t.Error(err)
  43. }
  44. }
  45. func TestSyncProducerWithTooManyExpectations(t *testing.T) {
  46. trm := newTestReporterMock()
  47. sp := NewSyncProducer(trm, nil)
  48. sp.ExpectSendMessageAndSucceed()
  49. sp.ExpectSendMessageAndFail(sarama.ErrOutOfBrokers)
  50. msg := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
  51. if _, _, err := sp.SendMessage(msg); err != nil {
  52. t.Error("No error expected on first SendMessage call", err)
  53. }
  54. if err := sp.Close(); err != nil {
  55. t.Error(err)
  56. }
  57. if len(trm.errors) != 1 {
  58. t.Error("Expected to report an error")
  59. }
  60. }
  61. func TestSyncProducerWithTooFewExpectations(t *testing.T) {
  62. trm := newTestReporterMock()
  63. sp := NewSyncProducer(trm, nil)
  64. sp.ExpectSendMessageAndSucceed()
  65. msg := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
  66. if _, _, err := sp.SendMessage(msg); err != nil {
  67. t.Error("No error expected on first SendMessage call", err)
  68. }
  69. if _, _, err := sp.SendMessage(msg); err != errOutOfExpectations {
  70. t.Error("errOutOfExpectations expected on second SendMessage call, found:", err)
  71. }
  72. if err := sp.Close(); err != nil {
  73. t.Error(err)
  74. }
  75. if len(trm.errors) != 1 {
  76. t.Error("Expected to report an error")
  77. }
  78. }