The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/util/contextutil_test.go

46 lines
1.2 KiB

package util
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func TestWithCancelWithReason(t *testing.T) {
t.Run("should add custom reason to the standard error", func(t *testing.T) {
expected := errors.New("test-err")
ctx, fn := WithCancelCause(context.Background())
fn(expected)
select {
case <-ctx.Done():
default:
require.Fail(t, "the context was not cancelled")
}
require.ErrorIs(t, ctx.Err(), expected)
require.ErrorIs(t, ctx.Err(), context.Canceled)
})
t.Run("should return only the first reason if called multiple times", func(t *testing.T) {
expected := errors.New("test-err")
ctx, fn := WithCancelCause(context.Background())
fn(expected)
fn(errors.New("other error"))
require.ErrorIs(t, ctx.Err(), expected)
})
t.Run("should return only the first reason if called multiple times", func(t *testing.T) {
expected := errors.New("test-err")
ctx, fn := WithCancelCause(context.Background())
fn(expected)
fn(errors.New("other error"))
require.ErrorIs(t, ctx.Err(), expected)
})
t.Run("should return context.Canceled if no reason provided", func(t *testing.T) {
ctx, fn := WithCancelCause(context.Background())
fn(nil)
require.Equal(t, ctx.Err(), context.Canceled)
})
}