-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathclient_option_test.go
More file actions
82 lines (62 loc) · 2.35 KB
/
client_option_test.go
File metadata and controls
82 lines (62 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gue
import (
"log/slog"
"reflect"
"testing"
"time"
"github.com/cappuccinotm/slogx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/metric/noop"
"go.uber.org/zap"
"go.uber.org/zap/exp/zapslog"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
"go.uber.org/zap/zaptest/observer"
)
func TestWithClientID(t *testing.T) {
clientWithDefaultID, err := NewClient(nil)
require.NoError(t, err)
assert.NotEmpty(t, clientWithDefaultID.id)
customID := "some-meaningful-id"
clientWithCustomID, err := NewClient(nil, WithClientID(customID))
require.NoError(t, err)
assert.Equal(t, customID, clientWithCustomID.id)
}
func TestWithClientLogger(t *testing.T) {
clientWithDefaultLogger, err := NewClient(nil)
require.NoError(t, err)
assert.IsType(t, slogx.NopHandler(), clientWithDefaultLogger.logger.Handler())
const logMessage = "hello"
observe, logs := observer.New(zap.InfoLevel)
logger := zapcore.NewTee(zaptest.NewLogger(t).Core(), observe)
clientWithCustomLogger, err := NewClient(nil, WithClientLogger(slog.New(zapslog.NewHandler(logger))))
require.NoError(t, err)
clientWithCustomLogger.logger.InfoContext(t.Context(), logMessage)
require.Len(t, logs.All(), 1)
assert.Equal(t, logMessage, logs.All()[0].Message)
assert.Equal(t, zapcore.InfoLevel, logs.All()[0].Level)
}
func TestWithClientBackoff(t *testing.T) {
customBackoff := func(retries int) time.Duration {
return time.Duration(retries) * time.Second
}
defaultPtr := reflect.ValueOf(DefaultExponentialBackoff).Pointer()
customPtr := reflect.ValueOf(customBackoff).Pointer()
clientWithDefaultBackoff, err := NewClient(nil)
require.NoError(t, err)
clientWithDefaultBackoffPtr := reflect.ValueOf(clientWithDefaultBackoff.backoff).Pointer()
assert.Equal(t, defaultPtr, clientWithDefaultBackoffPtr)
clientWithCustomBackoff, err := NewClient(nil, WithClientBackoff(customBackoff))
require.NoError(t, err)
assert.Equal(t, customBackoff(123), clientWithCustomBackoff.backoff(123))
assert.NotEqual(t, defaultPtr, customPtr)
}
func TestWithClientMeter(t *testing.T) {
customMeter := noop.NewMeterProvider().Meter("custom")
_, err := NewClient(nil)
require.NoError(t, err)
clientWithCustomMeter, err := NewClient(nil, WithClientMeter(customMeter))
require.NoError(t, err)
assert.Equal(t, customMeter, clientWithCustomMeter.meter)
}