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/services/ngalert/notifier/redis_channel_test.go

66 lines
1.5 KiB

package notifier
import (
"context"
"testing"
"github.com/alicebob/miniredis/v2"
"github.com/prometheus/alertmanager/cluster/clusterpb"
"github.com/prometheus/client_golang/prometheus"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
)
func TestNewRedisChannel(t *testing.T) {
mr, err := miniredis.Run()
require.NoError(t, err)
defer mr.Close()
rdb := redis.NewClient(&redis.Options{
Addr: mr.Addr(),
})
p := &redisPeer{
redis: rdb,
}
channel := newRedisChannel(p, "testKey", "testChannel", "testType")
require.NotNil(t, channel)
}
func TestBroadcastAndHandleMessages(t *testing.T) {
t.Skip() // TODO fix the flaky test https://github.com/grafana/grafana/issues/94037
const channelName = "testChannel"
mr, err := miniredis.Run()
require.NoError(t, err)
defer mr.Close()
rdb := redis.NewClient(&redis.Options{
Addr: mr.Addr(),
})
p := &redisPeer{
redis: rdb,
messagesSent: prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{update}),
messagesSentSize: prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{update}),
}
channel := newRedisChannel(p, "testKey", channelName, "testType").(*RedisChannel)
pubSub := rdb.Subscribe(context.Background(), channelName)
msgs := pubSub.Channel()
msg := []byte("test message")
channel.Broadcast(msg)
receivedMsg := <-msgs
var part clusterpb.Part
err = part.Unmarshal([]byte(receivedMsg.Payload))
require.NoError(t, err)
require.Equal(t, channelName, receivedMsg.Channel)
require.Equal(t, msg, part.Data)
}