diff --git a/README.md b/README.md index a0a63b3..d978d59 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ You can find all the working examples under the `tests` folder. ### Consumer -https://github.com/pmorelli92/bunnify/blob/aa8f63943345a8e3d092e98b9cd69cf73e7a0ec9/tests/consumer_publish_test.go#L38-L59 +https://github.com/pmorelli92/bunnify/blob/f356a80625d9dcdaec12d05953447ebcc24a1b13/tests/consumer_publish_test.go#L38-L61 ### Dead letter consumer diff --git a/tests/consumer_publish_test.go b/tests/consumer_publish_test.go index 6189204..ef0ca6d 100644 --- a/tests/consumer_publish_test.go +++ b/tests/consumer_publish_test.go @@ -2,6 +2,7 @@ package tests import ( "context" + "encoding/json" "fmt" "testing" "time" @@ -110,3 +111,99 @@ func TestConsumerPublisher(t *testing.T) { goleak.VerifyNone(t) } + +func TestConsumerDefaultHandler(t *testing.T) { + // Setup + queueName := uuid.NewString() + + type orderCreated struct { + ID string `json:"id"` + } + + type orderUpdated struct { + ID string `json:"id"` + UpdatedAt time.Time `json:"updatedAt"` + } + + connection := bunnify.NewConnection() + if err := connection.Start(); err != nil { + t.Fatal(err) + } + + var consumedEvents []bunnify.ConsumableEvent[json.RawMessage] + eventHandler := func(ctx context.Context, event bunnify.ConsumableEvent[json.RawMessage]) error { + consumedEvents = append(consumedEvents, event) + return nil + } + + // Bind only to queue received messages + consumer := connection.NewConsumer( + queueName, + bunnify.WithDefaultHandler(eventHandler)) + + if err := consumer.Consume(); err != nil { + t.Fatal(err) + } + + orderCreatedEvent := orderCreated{ID: uuid.NewString()} + orderUpdatedEvent := orderUpdated{ID: uuid.NewString(), UpdatedAt: time.Now()} + publisher := connection.NewPublisher() + + // Publish directly to the queue, without routing key + err := publisher.Publish( + context.TODO(), + "", + queueName, + bunnify.NewPublishableEvent(orderCreatedEvent)) + if err != nil { + t.Fatal(err) + } + + // Publish directly to the queue, without routing key + err = publisher.Publish( + context.TODO(), + "", + queueName, + bunnify.NewPublishableEvent(orderUpdatedEvent)) + if err != nil { + t.Fatal(err) + } + + time.Sleep(50 * time.Millisecond) + + if err := connection.Close(); err != nil { + t.Fatal(err) + } + + // Assert + if len(consumedEvents) != 2 { + t.Fatalf("expected 2 events, got %d", len(consumedEvents)) + } + + // First event should be orderCreated + var receivedOrderCreated orderCreated + err = json.Unmarshal(consumedEvents[0].Payload, &receivedOrderCreated) + if err != nil { + t.Fatal(err) + } + + if orderCreatedEvent.ID != receivedOrderCreated.ID { + t.Fatalf("expected created order ID to be %s got %s", orderCreatedEvent.ID, receivedOrderCreated.ID) + } + + var receivedOrderUpdated orderUpdated + err = json.Unmarshal(consumedEvents[1].Payload, &receivedOrderUpdated) + if err != nil { + t.Fatal(err) + } + + if orderUpdatedEvent.ID != receivedOrderUpdated.ID { + t.Fatalf("expected updated order ID to be %s got %s", orderUpdatedEvent.ID, receivedOrderUpdated.ID) + } + + if !orderUpdatedEvent.UpdatedAt.Equal(receivedOrderUpdated.UpdatedAt) { + t.Fatalf("expected updated order time to be %s got %s", orderUpdatedEvent.UpdatedAt, receivedOrderUpdated.UpdatedAt) + } + + goleak.VerifyNone(t) +} diff --git a/tests/dead_letter_receives_event_test.go b/tests/dead_letter_receives_event_test.go index 86b2708..1110b0b 100644 --- a/tests/dead_letter_receives_event_test.go +++ b/tests/dead_letter_receives_event_test.go @@ -2,7 +2,6 @@ package tests import ( "context" - "encoding/json" "fmt" "testing" "time" @@ -36,9 +35,9 @@ func TestDeadLetterReceivesEvent(t *testing.T) { return fmt.Errorf("error, this event will go to dead-letter") } - var eventFromDeadLetter bunnify.ConsumableEvent[json.RawMessage] - defaultHandler := func(ctx context.Context, event bunnify.ConsumableEvent[json.RawMessage]) error { - eventFromDeadLetter = event + var deadEvent bunnify.ConsumableEvent[orderCreated] + deadEventHandler := func(ctx context.Context, event bunnify.ConsumableEvent[orderCreated]) error { + deadEvent = event return nil } @@ -61,7 +60,7 @@ func TestDeadLetterReceivesEvent(t *testing.T) { deadLetterConsumer := connection.NewConsumer( deadLetterQueueName, - bunnify.WithDefaultHandler(defaultHandler)) + bunnify.WithHandler(routingKey, deadEventHandler)) if err := deadLetterConsumer.Consume(); err != nil { t.Fatal(err) @@ -81,32 +80,27 @@ func TestDeadLetterReceivesEvent(t *testing.T) { } // Assert - if publishedEvent.ID != eventFromDeadLetter.ID { - t.Fatalf("expected event ID %s, got %s", publishedEvent.ID, eventFromDeadLetter.ID) + if publishedEvent.ID != deadEvent.ID { + t.Fatalf("expected event ID %s, got %s", publishedEvent.ID, deadEvent.ID) } - if publishedEvent.CorrelationID != eventFromDeadLetter.CorrelationID { - t.Fatalf("expected correlation ID %s, got %s", publishedEvent.CorrelationID, eventFromDeadLetter.CorrelationID) + if publishedEvent.CorrelationID != deadEvent.CorrelationID { + t.Fatalf("expected correlation ID %s, got %s", publishedEvent.CorrelationID, deadEvent.CorrelationID) } - if !publishedEvent.Timestamp.Equal(eventFromDeadLetter.Timestamp) { - t.Fatalf("expected timestamp %s, got %s", publishedEvent.Timestamp, eventFromDeadLetter.Timestamp) + if !publishedEvent.Timestamp.Equal(deadEvent.Timestamp) { + t.Fatalf("expected timestamp %s, got %s", publishedEvent.Timestamp, deadEvent.Timestamp) } - var jsonData map[string]interface{} - if err = json.Unmarshal(eventFromDeadLetter.Payload, &jsonData); err != nil { - t.Fatal(err) - } - - if publishedOrderCreated.ID != jsonData["id"].(string) { - t.Fatalf("expected order created ID %s, got %s", publishedOrderCreated.ID, jsonData["id"].(string)) + if publishedOrderCreated.ID != deadEvent.Payload.ID { + t.Fatalf("expected order created ID %s, got %s", publishedOrderCreated.ID, deadEvent.Payload.ID) } - if exchangeName != eventFromDeadLetter.DeliveryInfo.Exchange { - t.Fatalf("expected exchange %s, got %s", exchangeName, eventFromDeadLetter.DeliveryInfo.Exchange) + if exchangeName != deadEvent.DeliveryInfo.Exchange { + t.Fatalf("expected exchange %s, got %s", exchangeName, deadEvent.DeliveryInfo.Exchange) } - if queueName != eventFromDeadLetter.DeliveryInfo.Queue { - t.Fatalf("expected queue %s, got %s", queueName, eventFromDeadLetter.DeliveryInfo.Queue) + if queueName != deadEvent.DeliveryInfo.Queue { + t.Fatalf("expected queue %s, got %s", queueName, deadEvent.DeliveryInfo.Queue) } - if routingKey != eventFromDeadLetter.DeliveryInfo.RoutingKey { - t.Fatalf("expected routing key %s, got %s", routingKey, eventFromDeadLetter.DeliveryInfo.RoutingKey) + if routingKey != deadEvent.DeliveryInfo.RoutingKey { + t.Fatalf("expected routing key %s, got %s", routingKey, deadEvent.DeliveryInfo.RoutingKey) } goleak.VerifyNone(t)