Live: persisting last message in cache for broadcast scope (#32938)

This commit is contained in:
Alexander Emelin
2021-04-30 11:06:33 -07:00
committed by GitHub
parent c25eab4eda
commit 0c2bcbf2bc
10 changed files with 436 additions and 19 deletions
+44
View File
@@ -0,0 +1,44 @@
package tests
import (
"testing"
"time"
"github.com/grafana/grafana/pkg/infra/localcache"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/live"
"github.com/grafana/grafana/pkg/services/live/database"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/setting"
)
// SetupTestStorage initializes a storage to used by the integration tests.
// This is required to properly register and execute migrations.
func SetupTestStorage(t *testing.T) *database.Storage {
cfg := setting.NewCfg()
// Live is disabled by default and only if it's enabled its database migrations run
// and the related database tables are created.
cfg.FeatureToggles = map[string]bool{"live": true}
gLive := live.NewGrafanaLive()
gLive.Cfg = cfg
// Hook for initialising the service after the Cfg is populated
// so that database migrations will run.
overrideServiceFunc := func(descriptor registry.Descriptor) (*registry.Descriptor, bool) {
if _, ok := descriptor.Instance.(*live.GrafanaLive); ok {
return &registry.Descriptor{
Name: descriptor.Name,
Instance: gLive,
InitPriority: descriptor.InitPriority,
}, true
}
return nil, false
}
registry.RegisterOverride(overrideServiceFunc)
// Now we can use sql.Store.
sqlStore := sqlstore.InitTestDB(t)
localCache := localcache.New(time.Hour, time.Hour)
return database.NewStorage(sqlStore, localCache)
}
@@ -0,0 +1,61 @@
// +build integration
package tests
import (
"encoding/json"
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/require"
)
func TestLiveMessage(t *testing.T) {
storage := SetupTestStorage(t)
getQuery := &models.GetLiveMessageQuery{
OrgId: 1,
Channel: "test_channel",
}
_, ok, err := storage.GetLiveMessage(getQuery)
require.NoError(t, err)
require.False(t, ok)
saveQuery := &models.SaveLiveMessageQuery{
OrgId: 1,
Channel: "test_channel",
Data: []byte(`{}`),
}
err = storage.SaveLiveMessage(saveQuery)
require.NoError(t, err)
msg, ok, err := storage.GetLiveMessage(getQuery)
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, int64(1), msg.OrgId)
require.Equal(t, "test_channel", msg.Channel)
require.Equal(t, json.RawMessage(`{}`), msg.Data)
require.NotZero(t, msg.Published)
// try saving again, should be replaced.
saveQuery2 := &models.SaveLiveMessageQuery{
OrgId: 1,
Channel: "test_channel",
Data: []byte(`{"input": "hello"}`),
}
err = storage.SaveLiveMessage(saveQuery2)
require.NoError(t, err)
getQuery2 := &models.GetLiveMessageQuery{
OrgId: 1,
Channel: "test_channel",
}
msg2, ok, err := storage.GetLiveMessage(getQuery2)
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, int64(1), msg2.OrgId)
require.Equal(t, "test_channel", msg2.Channel)
require.Equal(t, json.RawMessage(`{"input": "hello"}`), msg2.Data)
require.NotZero(t, msg2.Published)
}