You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
4.6 KiB
Go
157 lines
4.6 KiB
Go
3 years ago
|
package redis
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
2 years ago
|
"errors"
|
||
|
"io"
|
||
|
"net"
|
||
3 years ago
|
"strings"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
10 months ago
|
red "github.com/redis/go-redis/v9"
|
||
3 years ago
|
"github.com/stretchr/testify/assert"
|
||
2 years ago
|
"github.com/zeromicro/go-zero/core/breaker"
|
||
2 years ago
|
"github.com/zeromicro/go-zero/core/logx/logtest"
|
||
10 months ago
|
"github.com/zeromicro/go-zero/core/trace/tracetest"
|
||
3 years ago
|
tracesdk "go.opentelemetry.io/otel/trace"
|
||
3 years ago
|
)
|
||
|
|
||
|
func TestHookProcessCase1(t *testing.T) {
|
||
10 months ago
|
tracetest.NewInMemoryExporter(t)
|
||
|
w := logtest.NewCollector(t)
|
||
3 years ago
|
|
||
9 months ago
|
err := defaultDurationHook.ProcessHook(func(ctx context.Context, cmd red.Cmder) error {
|
||
10 months ago
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
||
|
return nil
|
||
|
})(context.Background(), red.NewCmd(context.Background()))
|
||
3 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
10 months ago
|
assert.False(t, strings.Contains(w.String(), "slow"))
|
||
3 years ago
|
}
|
||
|
|
||
|
func TestHookProcessCase2(t *testing.T) {
|
||
10 months ago
|
tracetest.NewInMemoryExporter(t)
|
||
2 years ago
|
w := logtest.NewCollector(t)
|
||
10 months ago
|
|
||
9 months ago
|
err := defaultDurationHook.ProcessHook(func(ctx context.Context, cmd red.Cmder) error {
|
||
10 months ago
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
||
|
time.Sleep(slowThreshold.Load() + time.Millisecond)
|
||
|
return nil
|
||
|
})(context.Background(), red.NewCmd(context.Background()))
|
||
3 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
3 years ago
|
assert.True(t, strings.Contains(w.String(), "slow"))
|
||
10 months ago
|
assert.True(t, strings.Contains(w.String(), "trace"))
|
||
|
assert.True(t, strings.Contains(w.String(), "span"))
|
||
3 years ago
|
}
|
||
|
|
||
|
func TestHookProcessPipelineCase1(t *testing.T) {
|
||
10 months ago
|
tracetest.NewInMemoryExporter(t)
|
||
|
w := logtest.NewCollector(t)
|
||
3 years ago
|
|
||
9 months ago
|
err := defaultDurationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
|
||
10 months ago
|
return nil
|
||
|
})(context.Background(), nil)
|
||
2 years ago
|
assert.NoError(t, err)
|
||
10 months ago
|
|
||
9 months ago
|
err = defaultDurationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
|
||
10 months ago
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
||
|
return nil
|
||
|
})(context.Background(), []red.Cmder{
|
||
2 years ago
|
red.NewCmd(context.Background()),
|
||
|
})
|
||
|
assert.NoError(t, err)
|
||
3 years ago
|
|
||
10 months ago
|
assert.False(t, strings.Contains(w.String(), "slow"))
|
||
3 years ago
|
}
|
||
|
|
||
|
func TestHookProcessPipelineCase2(t *testing.T) {
|
||
10 months ago
|
tracetest.NewInMemoryExporter(t)
|
||
2 years ago
|
w := logtest.NewCollector(t)
|
||
10 months ago
|
|
||
9 months ago
|
err := defaultDurationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
|
||
10 months ago
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
||
|
time.Sleep(slowThreshold.Load() + time.Millisecond)
|
||
|
return nil
|
||
|
})(context.Background(), []red.Cmder{
|
||
2 years ago
|
red.NewCmd(context.Background()),
|
||
|
})
|
||
|
assert.NoError(t, err)
|
||
3 years ago
|
|
||
3 years ago
|
assert.True(t, strings.Contains(w.String(), "slow"))
|
||
10 months ago
|
assert.True(t, strings.Contains(w.String(), "trace"))
|
||
|
assert.True(t, strings.Contains(w.String(), "span"))
|
||
3 years ago
|
}
|
||
|
|
||
|
func TestHookProcessPipelineCase3(t *testing.T) {
|
||
10 months ago
|
te := tracetest.NewInMemoryExporter(t)
|
||
3 years ago
|
|
||
9 months ago
|
err := defaultDurationHook.ProcessPipelineHook(func(ctx context.Context, cmds []red.Cmder) error {
|
||
10 months ago
|
assert.Equal(t, "redis", tracesdk.SpanFromContext(ctx).(interface{ Name() string }).Name())
|
||
|
return assert.AnError
|
||
|
})(context.Background(), []red.Cmder{
|
||
2 years ago
|
red.NewCmd(context.Background()),
|
||
10 months ago
|
})
|
||
|
assert.ErrorIs(t, err, assert.AnError)
|
||
|
traceLogs := te.GetSpans().Snapshots()[0]
|
||
|
assert.Equal(t, "redis", traceLogs.Name())
|
||
|
assert.Equal(t, assert.AnError.Error(), traceLogs.Events()[0].Attributes[1].Value.AsString(), "trace should record error")
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
2 years ago
|
func TestLogDuration(t *testing.T) {
|
||
2 years ago
|
w := logtest.NewCollector(t)
|
||
2 years ago
|
|
||
2 years ago
|
logDuration(context.Background(), []red.Cmder{
|
||
|
red.NewCmd(context.Background(), "get", "foo"),
|
||
|
}, 1*time.Second)
|
||
2 years ago
|
assert.True(t, strings.Contains(w.String(), "get foo"))
|
||
|
|
||
2 years ago
|
logDuration(context.Background(), []red.Cmder{
|
||
|
red.NewCmd(context.Background(), "get", "foo"),
|
||
|
red.NewCmd(context.Background(), "set", "bar", 0),
|
||
|
}, 1*time.Second)
|
||
|
assert.True(t, strings.Contains(w.String(), `get foo\nset bar 0`))
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
func TestFormatError(t *testing.T) {
|
||
|
// Test case: err is OpError
|
||
|
err := &net.OpError{
|
||
|
Err: mockOpError{},
|
||
|
}
|
||
|
assert.Equal(t, "timeout", formatError(err))
|
||
|
|
||
|
// Test case: err is nil
|
||
|
assert.Equal(t, "", formatError(nil))
|
||
|
|
||
|
// Test case: err is red.Nil
|
||
|
assert.Equal(t, "", formatError(red.Nil))
|
||
|
|
||
|
// Test case: err is io.EOF
|
||
|
assert.Equal(t, "eof", formatError(io.EOF))
|
||
|
|
||
|
// Test case: err is context.DeadlineExceeded
|
||
|
assert.Equal(t, "context deadline", formatError(context.DeadlineExceeded))
|
||
|
|
||
|
// Test case: err is breaker.ErrServiceUnavailable
|
||
10 months ago
|
assert.Equal(t, "breaker open", formatError(breaker.ErrServiceUnavailable))
|
||
2 years ago
|
|
||
|
// Test case: err is unknown
|
||
|
assert.Equal(t, "unexpected error", formatError(errors.New("some error")))
|
||
|
}
|
||
|
|
||
|
type mockOpError struct {
|
||
|
}
|
||
|
|
||
|
func (mockOpError) Error() string {
|
||
|
return "mock error"
|
||
|
}
|
||
|
|
||
|
func (mockOpError) Timeout() bool {
|
||
|
return true
|
||
|
}
|