Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
164 lines
3.6 KiB
Go
164 lines
3.6 KiB
Go
package profiler
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
h "github.com/cortezaproject/corteza/server/pkg/http"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
day = time.Hour * 24
|
|
)
|
|
|
|
func Test_ApigwProfiler_newHit(t *testing.T) {
|
|
var (
|
|
p = New()
|
|
req = require.New(t)
|
|
|
|
rr, err = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
|
|
hit = p.Hit(rr)
|
|
)
|
|
|
|
req.NoError(err)
|
|
req.Equal(hit.Status, http.StatusOK)
|
|
req.NotNil(hit.Ts)
|
|
req.NotNil(hit.ID)
|
|
}
|
|
|
|
func Test_ApigwProfiler_push(t *testing.T) {
|
|
var (
|
|
p = New()
|
|
req = require.New(t)
|
|
rr, err = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
|
|
|
|
hit = p.Hit(rr)
|
|
id = p.Push(hit)
|
|
)
|
|
|
|
_, found := p.l[id]
|
|
|
|
req.NoError(err)
|
|
req.NotEmpty(id)
|
|
req.Len(p.l, 1)
|
|
req.True(found)
|
|
}
|
|
|
|
func Test_ApigwProfiler_filterPath(t *testing.T) {
|
|
var (
|
|
pp = New()
|
|
req = require.New(t)
|
|
|
|
now = time.Date(2022, time.March, 1, 1, 1, 1, 0, time.UTC)
|
|
then = now.Add(-1 * day)
|
|
later = now.Add(day)
|
|
|
|
hr, _ = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
|
|
hr2, _ = h.NewRequest(httptest.NewRequest("GET", "/bar", strings.NewReader(`foo`)))
|
|
hr3, _ = h.NewRequest(httptest.NewRequest("GET", "/baz", strings.NewReader(`foo`)))
|
|
)
|
|
|
|
pp.Push(&Hit{R: hr, Ts: &then})
|
|
pp.Push(&Hit{R: hr, Ts: &then})
|
|
pp.Push(&Hit{R: hr2, Ts: &now})
|
|
pp.Push(&Hit{R: hr3, Ts: &later})
|
|
|
|
list := pp.Hits(Sort{
|
|
Path: hr3.RequestURI,
|
|
})
|
|
|
|
_, found := list[hr3.RequestURI]
|
|
|
|
req.True(found)
|
|
req.Len(list[hr3.RequestURI], 1)
|
|
}
|
|
|
|
func Test_ApigwProfiler_filterHit(t *testing.T) {
|
|
var (
|
|
pp = New()
|
|
req = require.New(t)
|
|
|
|
now = time.Date(2022, time.March, 1, 1, 1, 1, 0, time.UTC)
|
|
then = now.Add(-1 * day)
|
|
later = now.Add(day)
|
|
|
|
hr, _ = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
|
|
hr2, _ = h.NewRequest(httptest.NewRequest("GET", "/bar", strings.NewReader(`foo`)))
|
|
hr3, _ = h.NewRequest(httptest.NewRequest("GET", "/baz", strings.NewReader(`foo`)))
|
|
)
|
|
|
|
pp.Push(&Hit{R: hr, Ts: &then})
|
|
pp.Push(&Hit{R: hr, Ts: &now})
|
|
pp.Push(&Hit{R: hr2, Ts: &now})
|
|
pp.Push(&Hit{R: hr2, Ts: &later})
|
|
pp.Push(&Hit{R: hr2, Ts: &later})
|
|
|
|
h := pp.Hit(hr3)
|
|
h.Ts = &later
|
|
|
|
id := pp.Push(h)
|
|
|
|
list := pp.Hits(Sort{
|
|
Hit: h.ID,
|
|
})
|
|
|
|
_, found := list[id]
|
|
|
|
req.True(found)
|
|
req.Len(list[id], 1)
|
|
}
|
|
|
|
func Test_ApigwProfiler_purgeAll(t *testing.T) {
|
|
var (
|
|
pp = New()
|
|
req = require.New(t)
|
|
|
|
now = time.Date(2022, time.March, 1, 1, 1, 1, 0, time.UTC)
|
|
then = now.Add(-1 * day)
|
|
later = now.Add(day)
|
|
|
|
hr, _ = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
|
|
hr2, _ = h.NewRequest(httptest.NewRequest("GET", "/bar", strings.NewReader(`foo`)))
|
|
)
|
|
|
|
pp.Push(&Hit{R: hr, Ts: &then})
|
|
pp.Push(&Hit{R: hr, Ts: &now})
|
|
pp.Push(&Hit{R: hr2, Ts: &now})
|
|
pp.Push(&Hit{R: hr2, Ts: &later})
|
|
pp.Push(&Hit{R: hr2, Ts: &later})
|
|
|
|
req.Len(pp.l, 2)
|
|
pp.Purge(&PurgeFilter{})
|
|
req.Len(pp.l, 0)
|
|
}
|
|
|
|
func Test_ApigwProfiler_purgeRoute(t *testing.T) {
|
|
var (
|
|
pp = New()
|
|
req = require.New(t)
|
|
|
|
now = time.Date(2022, time.March, 1, 1, 1, 1, 0, time.UTC)
|
|
then = now.Add(-1 * day)
|
|
later = now.Add(day)
|
|
|
|
hr, _ = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
|
|
hr2, _ = h.NewRequest(httptest.NewRequest("GET", "/bar", strings.NewReader(`foo`)))
|
|
)
|
|
|
|
pp.Push(&Hit{Route: 1, R: hr, Ts: &then})
|
|
pp.Push(&Hit{Route: 1, R: hr, Ts: &now})
|
|
pp.Push(&Hit{Route: 2, R: hr2, Ts: &now})
|
|
pp.Push(&Hit{Route: 2, R: hr2, Ts: &later})
|
|
pp.Push(&Hit{Route: 2, R: hr2, Ts: &later})
|
|
|
|
req.Len(pp.l, 2)
|
|
|
|
pp.Purge(&PurgeFilter{RouteID: 2})
|
|
|
|
req.Len(pp.l, 1)
|
|
}
|