2020-04-10 05:37:53 +05:30
|
|
|
package eventsubscribers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
2020-04-10 17:49:00 +05:30
|
|
|
"time"
|
2020-04-10 05:37:53 +05:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-21 00:48:27 +05:30
|
|
|
"github.com/stretchr/testify/mock"
|
2020-04-10 05:37:53 +05:30
|
|
|
|
|
|
|
"github.com/elyby/chrly/api/mojang"
|
2020-04-19 05:01:09 +05:30
|
|
|
"github.com/elyby/chrly/dispatcher"
|
2020-04-10 05:37:53 +05:30
|
|
|
)
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
type pingableMock struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pingableMock) Ping() error {
|
|
|
|
args := p.Called()
|
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatabaseChecker(t *testing.T) {
|
|
|
|
t.Run("no error", func(t *testing.T) {
|
|
|
|
p := &pingableMock{}
|
|
|
|
p.On("Ping").Return(nil)
|
|
|
|
checker := DatabaseChecker(p)
|
|
|
|
assert.Nil(t, checker(context.Background()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with error", func(t *testing.T) {
|
|
|
|
err := errors.New("mock error")
|
|
|
|
p := &pingableMock{}
|
|
|
|
p.On("Ping").Return(err)
|
|
|
|
checker := DatabaseChecker(p)
|
|
|
|
assert.Equal(t, err, checker(context.Background()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("context timeout", func(t *testing.T) {
|
|
|
|
p := &pingableMock{}
|
|
|
|
waitChan := make(chan time.Time, 1)
|
|
|
|
p.On("Ping").WaitUntil(waitChan).Return(nil)
|
|
|
|
|
2023-12-12 06:05:08 +05:30
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 0)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
checker := DatabaseChecker(p)
|
|
|
|
assert.Errorf(t, checker(ctx), "check timeout")
|
|
|
|
close(waitChan)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-10 05:37:53 +05:30
|
|
|
func TestMojangBatchUuidsProviderChecker(t *testing.T) {
|
|
|
|
t.Run("empty state", func(t *testing.T) {
|
2020-04-19 05:01:09 +05:30
|
|
|
d := dispatcher.New()
|
|
|
|
checker := MojangBatchUuidsProviderResponseChecker(d, time.Millisecond)
|
2020-04-10 05:37:53 +05:30
|
|
|
assert.Nil(t, checker(context.Background()))
|
|
|
|
})
|
2020-05-01 01:46:22 +05:30
|
|
|
|
2020-04-10 05:37:53 +05:30
|
|
|
t.Run("when no error occurred", func(t *testing.T) {
|
2020-04-19 05:01:09 +05:30
|
|
|
d := dispatcher.New()
|
|
|
|
checker := MojangBatchUuidsProviderResponseChecker(d, time.Millisecond)
|
|
|
|
d.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, []*mojang.ProfileInfo{}, nil)
|
2020-04-10 05:37:53 +05:30
|
|
|
assert.Nil(t, checker(context.Background()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("when error occurred", func(t *testing.T) {
|
2020-04-19 05:01:09 +05:30
|
|
|
d := dispatcher.New()
|
|
|
|
checker := MojangBatchUuidsProviderResponseChecker(d, time.Millisecond)
|
2020-04-10 05:37:53 +05:30
|
|
|
err := errors.New("some error occurred")
|
2020-04-19 05:01:09 +05:30
|
|
|
d.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, nil, err)
|
2020-04-10 05:37:53 +05:30
|
|
|
assert.Equal(t, err, checker(context.Background()))
|
|
|
|
})
|
2020-04-10 17:49:00 +05:30
|
|
|
|
|
|
|
t.Run("should reset value after passed duration", func(t *testing.T) {
|
2020-04-19 05:01:09 +05:30
|
|
|
d := dispatcher.New()
|
|
|
|
checker := MojangBatchUuidsProviderResponseChecker(d, 20*time.Millisecond)
|
2020-04-10 17:49:00 +05:30
|
|
|
err := errors.New("some error occurred")
|
2020-04-19 05:01:09 +05:30
|
|
|
d.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, nil, err)
|
2020-04-10 17:49:00 +05:30
|
|
|
assert.Equal(t, err, checker(context.Background()))
|
|
|
|
time.Sleep(40 * time.Millisecond)
|
|
|
|
assert.Nil(t, checker(context.Background()))
|
|
|
|
})
|
2020-04-10 05:37:53 +05:30
|
|
|
}
|
2020-04-10 18:09:48 +05:30
|
|
|
|
|
|
|
func TestMojangBatchUuidsProviderQueueLengthChecker(t *testing.T) {
|
|
|
|
t.Run("empty state", func(t *testing.T) {
|
2020-04-19 05:01:09 +05:30
|
|
|
d := dispatcher.New()
|
|
|
|
checker := MojangBatchUuidsProviderQueueLengthChecker(d, 10)
|
2020-04-10 18:09:48 +05:30
|
|
|
assert.Nil(t, checker(context.Background()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("less than allowed limit", func(t *testing.T) {
|
2020-04-19 05:01:09 +05:30
|
|
|
d := dispatcher.New()
|
|
|
|
checker := MojangBatchUuidsProviderQueueLengthChecker(d, 10)
|
|
|
|
d.Emit("mojang_textures:batch_uuids_provider:round", []string{"username"}, 9)
|
2020-04-10 18:09:48 +05:30
|
|
|
assert.Nil(t, checker(context.Background()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("greater than allowed limit", func(t *testing.T) {
|
2020-04-19 05:01:09 +05:30
|
|
|
d := dispatcher.New()
|
|
|
|
checker := MojangBatchUuidsProviderQueueLengthChecker(d, 10)
|
|
|
|
d.Emit("mojang_textures:batch_uuids_provider:round", []string{"username"}, 10)
|
2020-04-10 18:09:48 +05:30
|
|
|
checkResult := checker(context.Background())
|
|
|
|
if assert.Error(t, checkResult) {
|
|
|
|
assert.Equal(t, "the maximum number of tasks in the queue has been exceeded", checkResult.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-05-01 01:46:22 +05:30
|
|
|
|
|
|
|
func TestMojangApiTexturesProviderResponseChecker(t *testing.T) {
|
|
|
|
t.Run("empty state", func(t *testing.T) {
|
|
|
|
d := dispatcher.New()
|
|
|
|
checker := MojangApiTexturesProviderResponseChecker(d, time.Millisecond)
|
|
|
|
assert.Nil(t, checker(context.Background()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("when no error occurred", func(t *testing.T) {
|
|
|
|
d := dispatcher.New()
|
|
|
|
checker := MojangApiTexturesProviderResponseChecker(d, time.Millisecond)
|
|
|
|
d.Emit("mojang_textures:mojang_api_textures_provider:after_request",
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
&mojang.SignedTexturesResponse{},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
assert.Nil(t, checker(context.Background()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("when error occurred", func(t *testing.T) {
|
|
|
|
d := dispatcher.New()
|
|
|
|
checker := MojangApiTexturesProviderResponseChecker(d, time.Millisecond)
|
|
|
|
err := errors.New("some error occurred")
|
|
|
|
d.Emit("mojang_textures:mojang_api_textures_provider:after_request", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", nil, err)
|
|
|
|
assert.Equal(t, err, checker(context.Background()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should reset value after passed duration", func(t *testing.T) {
|
|
|
|
d := dispatcher.New()
|
|
|
|
checker := MojangApiTexturesProviderResponseChecker(d, 20*time.Millisecond)
|
|
|
|
err := errors.New("some error occurred")
|
|
|
|
d.Emit("mojang_textures:mojang_api_textures_provider:after_request", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", nil, err)
|
|
|
|
assert.Equal(t, err, checker(context.Background()))
|
|
|
|
time.Sleep(40 * time.Millisecond)
|
|
|
|
assert.Nil(t, checker(context.Background()))
|
|
|
|
})
|
|
|
|
}
|