[MM-16620] Properly handle analytics client life cycle
Properly handle analytics client life cycle This change improves the life cycle management of the analytics client. It does so in two primary ways: - Move the client from being a shared pointer to belonging to the Server struct. - Expose the client.Close() method for proper shutdown of the client.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
df869e3f86
Коммит
f56a8f5a99
@@ -4,32 +4,18 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
)
|
||||
|
||||
func newTestServer() (chan string, *httptest.Server) {
|
||||
result := make(chan string, 100)
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
io.Copy(buf, r.Body)
|
||||
|
||||
result <- buf.String()
|
||||
}))
|
||||
|
||||
return result, server
|
||||
}
|
||||
|
||||
func TestPluginSetting(t *testing.T) {
|
||||
settings := &model.PluginSettings{
|
||||
Plugins: map[string]map[string]interface{}{
|
||||
@@ -57,42 +43,42 @@ func TestPluginActivated(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDiagnostics(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
if testing.Short() {
|
||||
t.SkipNow()
|
||||
}
|
||||
|
||||
data, server := newTestServer()
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
data := make(chan string, 100)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
data <- string(body)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
diagnosticId := "i am not real"
|
||||
th.App.SetDiagnosticId(diagnosticId)
|
||||
th.App.initDiagnostics(server.URL)
|
||||
diagnosticID := "test-diagnostic-id-12345"
|
||||
th.App.SetDiagnosticId(diagnosticID)
|
||||
th.Server.initDiagnostics(server.URL)
|
||||
|
||||
// Should send a client identify message
|
||||
select {
|
||||
case identifyMessage := <-data:
|
||||
t.Log("Got idmessage:\n" + identifyMessage)
|
||||
if !strings.Contains(identifyMessage, diagnosticId) {
|
||||
t.Fail()
|
||||
}
|
||||
require.Contains(t, identifyMessage, diagnosticID)
|
||||
case <-time.After(time.Second * 1):
|
||||
t.Fatal("Did not receive ID message")
|
||||
}
|
||||
|
||||
t.Run("Send", func(t *testing.T) {
|
||||
const TEST_VALUE = "stuff548959847"
|
||||
testValue := "test-send-value-6789"
|
||||
th.App.SendDiagnostic("Testing Diagnostic", map[string]interface{}{
|
||||
"hey": TEST_VALUE,
|
||||
"hey": testValue,
|
||||
})
|
||||
select {
|
||||
case result := <-data:
|
||||
t.Log("Got diagnostic:\n" + result)
|
||||
if !strings.Contains(result, TEST_VALUE) {
|
||||
t.Fail()
|
||||
}
|
||||
require.Contains(t, result, testValue)
|
||||
case <-time.After(time.Second * 1):
|
||||
t.Fatal("Did not receive diagnostic")
|
||||
}
|
||||
@@ -101,7 +87,7 @@ func TestDiagnostics(t *testing.T) {
|
||||
t.Run("SendDailyDiagnostics", func(t *testing.T) {
|
||||
th.App.sendDailyDiagnostics(true)
|
||||
|
||||
info := ""
|
||||
var info string
|
||||
// Collect the info sent.
|
||||
Loop:
|
||||
for {
|
||||
@@ -142,9 +128,7 @@ func TestDiagnostics(t *testing.T) {
|
||||
TRACK_CONFIG_MESSAGE_EXPORT,
|
||||
TRACK_PLUGINS,
|
||||
} {
|
||||
if !strings.Contains(info, item) {
|
||||
t.Fatal("Sent diagnostics missing item: " + item)
|
||||
}
|
||||
require.Contains(t, info, item)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user