1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-14 02:33:21 +02:00
opentelemetry-go/log/internal/global/state_test.go
Tyler Yahn 335f4de960
Add global log package (#5085)
* Add the log/global package

* Implement the stubbed features

* Add ConcurrentSafe tests

* Restructure with internal implementation

* Add internal global state

* Use internal state in log/global

* Add TestDelegation

* Fix lint

* Clean log_test.go

* Clean up

* Add changelog entry

* Simplify TestMultipleGlobalLoggerProvider

* Shorten log.go

* Fix comment text wrapping

* Shorten state_test.go

* Don't pollute output in TestSetLoggerProvider
2024-03-19 11:28:11 -07:00

76 lines
1.9 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package global
import (
"sync"
"testing"
"github.com/go-logr/logr"
"github.com/go-logr/logr/testr"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/noop"
)
func TestSetLoggerProvider(t *testing.T) {
reset := func() {
globalLoggerProvider = defaultLoggerProvider()
delegateLoggerOnce = sync.Once{}
}
t.Run("Set With default is a noop", func(t *testing.T) {
t.Cleanup(reset)
t.Cleanup(func(orig logr.Logger) func() {
global.SetLogger(testr.New(t)) // Don't pollute output.
return func() { global.SetLogger(orig) }
}(global.GetLogger()))
SetLoggerProvider(GetLoggerProvider())
provider, ok := GetLoggerProvider().(*loggerProvider)
if !ok {
t.Fatal("Global GetLoggerProvider should be the default logger provider")
}
if provider.delegate != nil {
t.Fatal("logger provider should not delegate when setting itself")
}
})
t.Run("First Set() should replace the delegate", func(t *testing.T) {
t.Cleanup(reset)
SetLoggerProvider(noop.NewLoggerProvider())
if _, ok := GetLoggerProvider().(*loggerProvider); ok {
t.Fatal("Global GetLoggerProvider was not changed")
}
})
t.Run("Set() should delegate existing Logger Providers", func(t *testing.T) {
t.Cleanup(reset)
provider := GetLoggerProvider()
SetLoggerProvider(noop.NewLoggerProvider())
if del := provider.(*loggerProvider); del.delegate == nil {
t.Fatal("The delegated logger providers should have a delegate")
}
})
t.Run("non-comparable types should not panic", func(t *testing.T) {
t.Cleanup(reset)
type nonComparableLoggerProvider struct {
log.LoggerProvider
noCmp [0]func() //nolint:structcheck,unused // This is indeed used.
}
provider := nonComparableLoggerProvider{}
SetLoggerProvider(provider)
assert.NotPanics(t, func() { SetLoggerProvider(provider) })
})
}