62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
|
|
package eventbus
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
"github.com/stretchr/testify/require"
|
||
|
|
"go.uber.org/zap"
|
||
|
|
"go.uber.org/zap/zapcore"
|
||
|
|
"go.uber.org/zap/zaptest/observer"
|
||
|
|
)
|
||
|
|
|
||
|
|
// TestPublish_DisabledBusLogsWarnAndReturnsError verifies the "EventBus
|
||
|
|
// disabled" path emits a WARN with message-size context and still returns
|
||
|
|
// an EventBusUnavailableError so callers that care can notice.
|
||
|
|
func TestPublish_DisabledBusLogsWarnAndReturnsError(t *testing.T) {
|
||
|
|
core, observed := observer.New(zapcore.WarnLevel)
|
||
|
|
eb := &RabbitMQEventBus{
|
||
|
|
config: &RabbitMQConfig{Enable: false},
|
||
|
|
logger: zap.New(core),
|
||
|
|
IsEnabled: false,
|
||
|
|
}
|
||
|
|
|
||
|
|
err := eb.Publish(context.Background(), "veza.events", "track.uploaded", false, false, amqp.Publishing{
|
||
|
|
Body: []byte(`{"track_id":"abc"}`),
|
||
|
|
})
|
||
|
|
|
||
|
|
var unavail *EventBusUnavailableError
|
||
|
|
require.True(t, errors.As(err, &unavail), "disabled bus must return EventBusUnavailableError")
|
||
|
|
|
||
|
|
entries := observed.All()
|
||
|
|
require.Len(t, entries, 1)
|
||
|
|
assert.Equal(t, zapcore.WarnLevel, entries[0].Level)
|
||
|
|
assert.Contains(t, entries[0].Message, "disabled")
|
||
|
|
|
||
|
|
// Verify structured context so dashboards can group drops by
|
||
|
|
// exchange+routing_key.
|
||
|
|
fields := map[string]interface{}{}
|
||
|
|
for _, f := range entries[0].Context {
|
||
|
|
fields[f.Key] = f
|
||
|
|
}
|
||
|
|
assert.Contains(t, fields, "exchange")
|
||
|
|
assert.Contains(t, fields, "routing_key")
|
||
|
|
assert.Contains(t, fields, "payload_bytes")
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestPublish_NilLoggerIsAllowed guards against the legacy callers that
|
||
|
|
// construct an EventBus without a logger — the method must stay panic-free.
|
||
|
|
func TestPublish_NilLoggerIsAllowed(t *testing.T) {
|
||
|
|
eb := &RabbitMQEventBus{
|
||
|
|
config: &RabbitMQConfig{Enable: false},
|
||
|
|
logger: nil,
|
||
|
|
IsEnabled: false,
|
||
|
|
}
|
||
|
|
assert.NotPanics(t, func() {
|
||
|
|
_ = eb.Publish(context.Background(), "x", "y", false, false, amqp.Publishing{Body: []byte("hi")})
|
||
|
|
})
|
||
|
|
}
|