1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-21 13:38:56 +02:00
focalboard/server/services/telemetry/telemetry_test.go
Rajat Dabade c8e729b6fe
[Refactor]: updated dependency for focalboard server (#5009)
* refactor: updated dependency for focalboard server

* chore: more dependency fixes

* refactor: removed the unless code

* refactor: added ctx for login and removed unnessary code

* refactor: bump up go version

* refactor: removed the commented code

* chore: upgraded golinter version

* fix: linter issue

* refactor: removed feature flg fix golinter

* refactor: removed feature flag from code

* revert: statistic and it's function

* refactor: removed ProductLimit related code

* refactor: removed isWithinViewsLimit implementation

* refactor: moved function GetUsedCardsCount to statistics.go from cloud.go

* refactor: removed insight code board

* refactor: removed limit dialog

* refactor: updated dependencies for linux

* chore: golinter fix

* chore: updated helper test function to use newLogger

* fix: go test

* refactor: db ping attempts from config

* revert: feature in action

* revert: feature flag in action

* revert: boardsEditor setting

---------

Co-authored-by: Rajat Dabade <rajat@Rajats-MacBook-Pro.local>
2024-06-07 23:30:08 +05:30

114 lines
3.1 KiB
Go

package telemetry
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/stretchr/testify/require"
)
func mockServer() (chan []byte, *httptest.Server) {
done := make(chan []byte, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, r.Body); err != nil {
panic(err)
}
var v interface{}
err := json.Unmarshal(buf.Bytes(), &v)
if err != nil {
panic(err)
}
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic(err)
}
// filter the identify message
if strings.Contains(string(b), `"type": "identify"`) {
return
}
done <- b
}))
return done, server
}
func TestTelemetry(t *testing.T) {
receiveChan, server := mockServer()
os.Setenv("RUDDER_KEY", "mock-test-rudder-key")
os.Setenv("RUDDER_DATAPLANE_URL", server.URL)
checkMockRudderServer := func(t *testing.T) {
// check mock rudder server got
got := string(<-receiveChan)
require.Contains(t, got, "mockTrackerKey")
require.Contains(t, got, "mockTrackerValue")
}
t.Run("Register tracker and run telemetry job", func(t *testing.T) {
service := New("mockTelemetryID", mlog.CreateConsoleTestLogger(t))
service.RegisterTracker("mockTracker", func() (Tracker, error) {
return map[string]interface{}{
"mockTrackerKey": "mockTrackerValue",
}, nil
})
service.RunTelemetryJob(time.Now().UnixNano() / int64(time.Millisecond))
checkMockRudderServer(t)
})
t.Run("do telemetry if needed", func(t *testing.T) {
service := New("mockTelemetryID", mlog.CreateConsoleTestLogger(t))
service.RegisterTracker("mockTracker", func() (Tracker, error) {
return map[string]interface{}{
"mockTrackerKey": "mockTrackerValue",
}, nil
})
firstRun := time.Now()
t.Run("Send once every 10 minutes for the first hour", func(t *testing.T) {
service.doTelemetryIfNeeded(firstRun.Add(-30 * time.Minute))
checkMockRudderServer(t)
})
t.Run("Send once every hour thereafter for the first 12 hours", func(t *testing.T) {
// firstRun is 2 hours ago and timestampLastTelemetrySent is hour ago
// need to do telemetry
service.timestampLastTelemetrySent = time.Now().Add(-time.Hour)
service.doTelemetryIfNeeded(firstRun.Add(-2 * time.Hour))
checkMockRudderServer(t)
// firstRun is 2 hours ago and timestampLastTelemetrySent is just now
// no need to do telemetry
service.doTelemetryIfNeeded(firstRun.Add(-2 * time.Hour))
require.Equal(t, 0, len(receiveChan))
})
t.Run("Send at the 24 hour mark and every 24 hours after", func(t *testing.T) {
// firstRun is 24 hours ago and timestampLastTelemetrySent is 24 hours ago
// need to do telemetry
service.timestampLastTelemetrySent = time.Now().Add(-24 * time.Hour)
service.doTelemetryIfNeeded(firstRun.Add(-24 * time.Hour))
checkMockRudderServer(t)
// firstRun is 24 hours ago and timestampLastTelemetrySent is just now
// no need to do telemetry
service.doTelemetryIfNeeded(firstRun.Add(-24 * time.Hour))
require.Equal(t, 0, len(receiveChan))
})
})
}