Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sender] Fix Marshaller nil bug in NewSender() #248

Merged
merged 7 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions v2/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestSetLoggerFunc(t *testing.T) {
})
defer SetLoggerFunc(func(ctx context.Context) Logger { return &contextLogger{ctx: ctx, logger: slog.Default()} })
logger := &testLogger{}
ctx := context.WithValue(context.Background(), testlogkey, logger)
ctx := context.WithValue(context.Background(), testLogKey{}, logger)
getLogger(ctx).Info("test")
g := NewWithT(t)
g.Expect(getLogger(ctx)).To(Equal(logger))
Expand All @@ -41,7 +41,7 @@ func TestSetLoggerFunc(t *testing.T) {
g.Expect(func() { getLogger(ctx).Info("") }).ToNot(Panic())

// getLogger returns nil
nilCtx := context.WithValue(context.Background(), testlogkey, nil)
nilCtx := context.WithValue(context.Background(), testLogKey{}, nil)
g.Expect(func() { getLogger(nilCtx).Info("test") }).ToNot(Panic())

//coverage on testlogger
Expand All @@ -53,20 +53,19 @@ type testLogger struct {
entries []string
}

var testlogkey = struct{}{}
type testLogKey struct{}

func (t *testLogger) Info(s string) {
t.entries = append(t.entries, s)
}

func (t *testLogger) Warn(s string) {
}

func (t *testLogger) Error(s string) {
}

func getTestLogger(ctx context.Context) Logger {
if l, ok := ctx.Value(testlogkey).(*testLogger); ok {
if l, ok := ctx.Value(testLogKey{}).(*testLogger); ok {
return l
}
return &contextLogger{ctx: ctx, logger: slog.Default()}
Expand Down
5 changes: 4 additions & 1 deletion v2/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ type SenderOptions struct {
// NewSender takes in a Sender and a Marshaller to create a new object that can send messages to the ServiceBus queue
func NewSender(sender AzServiceBusSender, options *SenderOptions) *Sender {
if options == nil {
options = &SenderOptions{Marshaller: &DefaultJSONMarshaller{}}
options = &SenderOptions{}
}
if options.Marshaller == nil {
options.Marshaller = &DefaultJSONMarshaller{}
}
if options.SendTimeout == 0 {
options.SendTimeout = defaultSendTimeout
Expand Down
11 changes: 11 additions & 0 deletions v2/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ func TestFunc_NewSender(t *testing.T) {
if sender.options.Marshaller != marshaller {
t.Errorf("failed to set marshaller, expected: %s, actual: %s", reflect.TypeOf(marshaller), reflect.TypeOf(sender.options.Marshaller))
}

sender = NewSender(nil, &SenderOptions{EnableTracingPropagation: true})
if sender.options.Marshaller == nil {
t.Errorf("failed to set marshaller")
}
if !sender.options.EnableTracingPropagation {
t.Errorf("failed to set EnableTracingPropagation, expected: true, actual: %t", sender.options.EnableTracingPropagation)
}
if sender.options.SendTimeout != defaultSendTimeout {
t.Errorf("failed to set SendTimeout, expected: %s, actual: %s", defaultSendTimeout, sender.options.SendTimeout)
}
}

func TestHandlers_SetMessageId(t *testing.T) {
Expand Down
Loading