2020-02-16 15:53:47 +05:30
|
|
|
|
package eventsubscribers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2020-03-30 15:00:06 +05:30
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
|
|
|
|
|
"github.com/elyby/chrly/api/mojang"
|
2020-02-16 15:53:47 +05:30
|
|
|
|
"github.com/elyby/chrly/dispatcher"
|
|
|
|
|
"github.com/elyby/chrly/tests"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type StatsReporterTestCase struct {
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events map[string][]interface{}
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls [][]interface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var statsReporterTestCases = []*StatsReporterTestCase{
|
|
|
|
|
// Before request
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:before_request": {httptest.NewRequest("GET", "http://localhost/skins/username", nil)},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.skins.request", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:before_request": {httptest.NewRequest("GET", "http://localhost/skins?name=username", nil)},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.skins.get_request", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:before_request": {httptest.NewRequest("GET", "http://localhost/cloaks/username", nil)},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.capes.request", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:before_request": {httptest.NewRequest("GET", "http://localhost/cloaks?name=username", nil)},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.capes.get_request", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:before_request": {httptest.NewRequest("GET", "http://localhost/textures/username", nil)},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.textures.request", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:before_request": {httptest.NewRequest("GET", "http://localhost/textures/signed/username", nil)},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.signed_textures.request", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:before_request": {httptest.NewRequest("POST", "http://localhost/api/skins", nil)},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.api.skins.post.request", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:before_request": {httptest.NewRequest("DELETE", "http://localhost/api/skins/username", nil)},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.api.skins.delete.request", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:before_request": {httptest.NewRequest("DELETE", "http://localhost/api/skins/id:1", nil)},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.api.skins.delete.request", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:before_request": {httptest.NewRequest("GET", "http://localhost/unknown", nil)},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: nil,
|
|
|
|
|
},
|
|
|
|
|
// After request
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:after_request": {httptest.NewRequest("POST", "http://localhost/api/skins", nil), 201},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.api.skins.post.success", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:after_request": {httptest.NewRequest("POST", "http://localhost/api/skins", nil), 400},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.api.skins.post.validation_failed", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:after_request": {httptest.NewRequest("DELETE", "http://localhost/api/skins/username", nil), 204},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.api.skins.delete.success", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:after_request": {httptest.NewRequest("DELETE", "http://localhost/api/skins/username", nil), 404},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.api.skins.delete.not_found", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:after_request": {httptest.NewRequest("DELETE", "http://localhost/api/skins/id:1", nil), 204},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.api.skins.delete.success", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:after_request": {httptest.NewRequest("DELETE", "http://localhost/api/skins/id:1", nil), 404},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.api.skins.delete.not_found", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"skinsystem:after_request": {httptest.NewRequest("DELETE", "http://localhost/unknown", nil), 404},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: nil,
|
|
|
|
|
},
|
|
|
|
|
// Authenticator
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"authenticator:success": {},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.authentication.challenge", int64(1)},
|
|
|
|
|
{"IncCounter", "mock_prefix.authentication.success", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-03-30 15:00:06 +05:30
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"authentication:error": {errors.New("error")},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.authentication.challenge", int64(1)},
|
|
|
|
|
{"IncCounter", "mock_prefix.authentication.failed", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-03-30 15:00:06 +05:30
|
|
|
|
// Mojang signed textures provider
|
2020-03-31 06:23:46 +05:30
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:call": {"username"},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures.request", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:usernames:after_cache": {"username", "", errors.New("error")},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:usernames:after_cache": {"username", "", nil},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures:usernames:cache_hit_nil", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:usernames:after_cache": {"username", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", nil},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures:usernames:cache_hit", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:textures:after_cache": {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", nil, errors.New("error")},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:textures:after_cache": {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", nil, nil},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:textures:after_cache": {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", &mojang.SignedTexturesResponse{}, nil},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures.textures.cache_hit", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:already_processing": {"username"},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures.already_scheduled", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:usernames:after_call": {"username", nil, errors.New("error")},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:usernames:after_call": {"username", nil, nil},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures.usernames.uuid_miss", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:usernames:after_call": {"username", &mojang.ProfileInfo{}, nil},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures.usernames.uuid_hit", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:textures:after_call": {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", nil, errors.New("error")},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:textures:after_call": {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", nil, nil},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures.usernames.textures_miss", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:textures:after_call": {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", &mojang.SignedTexturesResponse{}, nil},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures.usernames.textures_hit", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-03-30 15:00:06 +05:30
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:before_result": {"username", ""},
|
2020-03-30 18:14:12 +05:30
|
|
|
|
"mojang_textures:after_result": {"username", &mojang.SignedTexturesResponse{}, nil},
|
2020-03-30 15:00:06 +05:30
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"RecordTimer", "mock_prefix.mojang_textures.result_time", mock.AnythingOfType("time.Duration")},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:textures:before_call": {"аааааааааааааааааааааааааааааааа"},
|
2020-03-30 18:14:12 +05:30
|
|
|
|
"mojang_textures:textures:after_call": {"аааааааааааааааааааааааааааааааа", &mojang.SignedTexturesResponse{}, nil},
|
2020-03-30 15:00:06 +05:30
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures.textures.request", int64(1)},
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures.usernames.textures_hit", int64(1)},
|
|
|
|
|
{"RecordTimer", "mock_prefix.mojang_textures.textures.request_time", mock.AnythingOfType("time.Duration")},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
// Batch UUIDs provider
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:batch_uuids_provider:queued": {"username"},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"IncCounter", "mock_prefix.mojang_textures.usernames.queued", int64(1)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:batch_uuids_provider:round": {[]string{"username1", "username2"}, 5},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"UpdateGauge", "mock_prefix.mojang_textures.usernames.iteration_size", int64(2)},
|
|
|
|
|
{"UpdateGauge", "mock_prefix.mojang_textures.usernames.queue_size", int64(5)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Events: map[string][]interface{}{
|
|
|
|
|
"mojang_textures:batch_uuids_provider:before_round": {},
|
|
|
|
|
"mojang_textures:batch_uuids_provider:after_round": {},
|
|
|
|
|
},
|
|
|
|
|
ExpectedCalls: [][]interface{}{
|
|
|
|
|
{"RecordTimer", "mock_prefix.mojang_textures.usernames.round_time", mock.AnythingOfType("time.Duration")},
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-02-16 15:53:47 +05:30
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 15:00:06 +05:30
|
|
|
|
func TestStatsReporter_handleEvents(t *testing.T) {
|
2020-02-16 15:53:47 +05:30
|
|
|
|
for _, c := range statsReporterTestCases {
|
2020-03-30 15:00:06 +05:30
|
|
|
|
t.Run("handle events", func(t *testing.T) {
|
2020-02-16 15:53:47 +05:30
|
|
|
|
wdMock := &tests.WdMock{}
|
|
|
|
|
if c.ExpectedCalls != nil {
|
|
|
|
|
for _, c := range c.ExpectedCalls {
|
|
|
|
|
topicName, _ := c[0].(string)
|
|
|
|
|
wdMock.On(topicName, c[1:]...).Once()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reporter := &StatsReporter{
|
|
|
|
|
Reporter: wdMock,
|
|
|
|
|
Prefix: "mock_prefix",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d := dispatcher.New()
|
|
|
|
|
reporter.ConfigureWithDispatcher(d)
|
2020-03-30 15:00:06 +05:30
|
|
|
|
for event, args := range c.Events {
|
|
|
|
|
d.Emit(event, args...)
|
|
|
|
|
}
|
2020-02-16 15:53:47 +05:30
|
|
|
|
|
|
|
|
|
wdMock.AssertExpectations(t)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|