|
| 1 | +package amqp_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hellofresh/goengine" |
| 9 | + "github.com/hellofresh/goengine/driver/sql" |
| 10 | + goengineAmqp "github.com/hellofresh/goengine/extension/amqp" |
| 11 | + goengineLogger "github.com/hellofresh/goengine/extension/logrus" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | + "github.com/sirupsen/logrus/hooks/test" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func TestNotificationPublisher_Publish(t *testing.T) { |
| 19 | + |
| 20 | + ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second) |
| 21 | + defer ctxCancel() |
| 22 | + |
| 23 | + channel := &mockChannel{} |
| 24 | + connection := &mockConnection{} |
| 25 | + |
| 26 | + t.Run("Invalid arguments", func(t *testing.T) { |
| 27 | + logger, _ := getLogger() |
| 28 | + |
| 29 | + _, err := goengineAmqp.NewNotificationPublisher("http://localhost:5672/", "my-queue", 3, 4, logger, connection, channel) |
| 30 | + assert.Equal(t, goengine.InvalidArgumentError("amqpDSN"), err) |
| 31 | + |
| 32 | + _, err = goengineAmqp.NewNotificationPublisher("amqp://localhost:5672/", "", 3, 4, logger, connection, channel) |
| 33 | + assert.Equal(t, goengine.InvalidArgumentError("queue"), err) |
| 34 | + |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("Publish Nil Notification Message", func(t *testing.T) { |
| 38 | + ensure := require.New(t) |
| 39 | + logger, loggerHook := getLogger() |
| 40 | + |
| 41 | + publisher, err := goengineAmqp.NewNotificationPublisher("amqp://localhost:5672/", "my-queue", 3, 4, logger, connection, channel) |
| 42 | + ensure.NoError(err) |
| 43 | + err = publisher.Publish(ctx, nil) |
| 44 | + ensure.Nil(err) |
| 45 | + ensure.Len(loggerHook.Entries, 1) |
| 46 | + ensure.Equal("unable to handle nil notification, skipping", loggerHook.LastEntry().Message) |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("Publish Message", func(t *testing.T) { |
| 50 | + ensure := require.New(t) |
| 51 | + logger, loggerHook := getLogger() |
| 52 | + |
| 53 | + publisher, err := goengineAmqp.NewNotificationPublisher("amqp://localhost:5672/", "my-queue", 3, 4, logger, connection, channel) |
| 54 | + ensure.NoError(err) |
| 55 | + |
| 56 | + err = publisher.Publish(ctx, &sql.ProjectionNotification{No: 1, AggregateID: "8150276e-34fe-49d9-aeae-a35af0040a4f"}) |
| 57 | + |
| 58 | + ensure.NoError(err) |
| 59 | + ensure.Len(loggerHook.Entries, 0) |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +func getLogger() (goengine.Logger, *test.Hook) { |
| 64 | + logger, loggerHook := test.NewNullLogger() |
| 65 | + logger.SetLevel(logrus.DebugLevel) |
| 66 | + |
| 67 | + return goengineLogger.Wrap(logger), loggerHook |
| 68 | +} |
0 commit comments