2019-04-18 05:26:20 +05:30
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"github.com/elyby/chrly/api/mojang"
|
2019-04-19 04:11:52 +05:30
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2019-04-21 05:34:03 +05:30
|
|
|
"net"
|
2019-04-21 00:52:02 +05:30
|
|
|
"strings"
|
2019-04-21 05:34:03 +05:30
|
|
|
"syscall"
|
2019-04-19 04:11:52 +05:30
|
|
|
"testing"
|
2019-04-21 00:52:02 +05:30
|
|
|
"time"
|
2019-04-18 05:26:20 +05:30
|
|
|
)
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
type mojangApiMocks struct {
|
2019-04-19 04:11:52 +05:30
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (o *mojangApiMocks) UsernamesToUuids(usernames []string) ([]*mojang.ProfileInfo, error) {
|
2019-04-19 04:11:52 +05:30
|
|
|
args := o.Called(usernames)
|
|
|
|
var result []*mojang.ProfileInfo
|
|
|
|
if casted, ok := args.Get(0).([]*mojang.ProfileInfo); ok {
|
|
|
|
result = casted
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, args.Error(1)
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (o *mojangApiMocks) UuidToTextures(uuid string, signed bool) (*mojang.SignedTexturesResponse, error) {
|
2019-04-19 04:11:52 +05:30
|
|
|
args := o.Called(uuid, signed)
|
|
|
|
var result *mojang.SignedTexturesResponse
|
|
|
|
if casted, ok := args.Get(0).(*mojang.SignedTexturesResponse); ok {
|
|
|
|
result = casted
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, args.Error(1)
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
type mockStorage struct {
|
2019-04-20 22:05:37 +05:30
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (m *mockStorage) GetUuid(username string) (string, error) {
|
2019-04-20 22:05:37 +05:30
|
|
|
args := m.Called(username)
|
2019-04-21 00:52:02 +05:30
|
|
|
return args.String(0), args.Error(1)
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (m *mockStorage) StoreUuid(username string, uuid string) {
|
2019-04-21 00:52:02 +05:30
|
|
|
m.Called(username, uuid)
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (m *mockStorage) GetTextures(uuid string) (*mojang.SignedTexturesResponse, error) {
|
2019-04-21 00:52:02 +05:30
|
|
|
args := m.Called(uuid)
|
2019-04-20 22:05:37 +05:30
|
|
|
var result *mojang.SignedTexturesResponse
|
|
|
|
if casted, ok := args.Get(0).(*mojang.SignedTexturesResponse); ok {
|
|
|
|
result = casted
|
|
|
|
}
|
|
|
|
|
2019-04-21 00:52:02 +05:30
|
|
|
return result, args.Error(1)
|
2019-04-20 22:05:37 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (m *mockStorage) StoreTextures(textures *mojang.SignedTexturesResponse) {
|
2019-04-20 22:05:37 +05:30
|
|
|
m.Called(textures)
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
type queueTestSuite struct {
|
2019-04-19 04:11:52 +05:30
|
|
|
suite.Suite
|
|
|
|
Queue *JobsQueue
|
2019-04-21 05:34:03 +05:30
|
|
|
Storage *mockStorage
|
|
|
|
MojangApi *mojangApiMocks
|
2019-04-19 04:11:52 +05:30
|
|
|
Iterate func()
|
|
|
|
|
|
|
|
iterateChan chan bool
|
|
|
|
done func()
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) SetupSuite() {
|
2019-04-21 22:58:58 +05:30
|
|
|
uuidsQueuePeriod = 0
|
2019-04-18 05:26:20 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) SetupTest() {
|
|
|
|
suite.Storage = &mockStorage{}
|
2019-04-20 22:05:37 +05:30
|
|
|
|
|
|
|
suite.Queue = &JobsQueue{Storage: suite.Storage}
|
2019-04-19 04:11:52 +05:30
|
|
|
|
|
|
|
suite.iterateChan = make(chan bool)
|
|
|
|
forever = func() bool {
|
|
|
|
return <-suite.iterateChan
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.Iterate = func() {
|
|
|
|
suite.iterateChan <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.done = func() {
|
|
|
|
suite.iterateChan <- false
|
2019-04-18 05:26:20 +05:30
|
|
|
}
|
2019-04-19 04:11:52 +05:30
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
suite.MojangApi = new(mojangApiMocks)
|
|
|
|
usernamesToUuids = suite.MojangApi.UsernamesToUuids
|
2019-04-19 04:11:52 +05:30
|
|
|
uuidToTextures = suite.MojangApi.UuidToTextures
|
2019-04-18 05:26:20 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TearDownTest() {
|
2019-04-19 04:11:52 +05:30
|
|
|
suite.done()
|
|
|
|
suite.MojangApi.AssertExpectations(suite.T())
|
2019-04-20 22:05:37 +05:30
|
|
|
suite.Storage.AssertExpectations(suite.T())
|
2019-04-18 05:26:20 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestReceiveTexturesForOneUsernameWithoutAnyCache() {
|
2019-04-20 22:05:37 +05:30
|
|
|
expectedResult := &mojang.SignedTexturesResponse{Id: "0d252b7218b648bfb86c2ae476954d32", Name: "maksimkurb"}
|
|
|
|
|
2019-04-21 00:52:02 +05:30
|
|
|
suite.Storage.On("GetUuid", "maksimkurb").Once().Return("", &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreUuid", "maksimkurb", "0d252b7218b648bfb86c2ae476954d32").Once()
|
|
|
|
suite.Storage.On("GetTextures", "0d252b7218b648bfb86c2ae476954d32").Once().Return(nil, &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreTextures", expectedResult).Once()
|
2019-04-21 05:34:03 +05:30
|
|
|
suite.MojangApi.On("UsernamesToUuids", []string{"maksimkurb"}).Once().Return([]*mojang.ProfileInfo{
|
2019-04-19 04:11:52 +05:30
|
|
|
{Id: "0d252b7218b648bfb86c2ae476954d32", Name: "maksimkurb"},
|
|
|
|
}, nil)
|
2019-04-20 22:05:37 +05:30
|
|
|
suite.MojangApi.On("UuidToTextures", "0d252b7218b648bfb86c2ae476954d32", true).Once().Return(expectedResult, nil)
|
2019-04-19 04:11:52 +05:30
|
|
|
|
|
|
|
resultChan := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
|
|
|
|
suite.Iterate()
|
|
|
|
|
|
|
|
result := <-resultChan
|
2019-04-20 22:05:37 +05:30
|
|
|
suite.Assert().Equal(expectedResult, result)
|
2019-04-19 04:11:52 +05:30
|
|
|
}
|
2019-04-18 05:26:20 +05:30
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestReceiveTexturesForFewUsernamesWithoutAnyCache() {
|
2019-04-20 22:05:37 +05:30
|
|
|
expectedResult1 := &mojang.SignedTexturesResponse{Id: "0d252b7218b648bfb86c2ae476954d32", Name: "maksimkurb"}
|
|
|
|
expectedResult2 := &mojang.SignedTexturesResponse{Id: "4566e69fc90748ee8d71d7ba5aa00d20", Name: "Thinkofdeath"}
|
|
|
|
|
2019-04-21 00:52:02 +05:30
|
|
|
suite.Storage.On("GetUuid", "maksimkurb").Once().Return("", &ValueNotFound{})
|
|
|
|
suite.Storage.On("GetUuid", "Thinkofdeath").Once().Return("", &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreUuid", "maksimkurb", "0d252b7218b648bfb86c2ae476954d32").Once()
|
|
|
|
suite.Storage.On("StoreUuid", "Thinkofdeath", "4566e69fc90748ee8d71d7ba5aa00d20").Once()
|
|
|
|
suite.Storage.On("GetTextures", "0d252b7218b648bfb86c2ae476954d32").Once().Return(nil, &ValueNotFound{})
|
|
|
|
suite.Storage.On("GetTextures", "4566e69fc90748ee8d71d7ba5aa00d20").Once().Return(nil, &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreTextures", expectedResult1).Once()
|
|
|
|
suite.Storage.On("StoreTextures", expectedResult2).Once()
|
2019-04-21 05:34:03 +05:30
|
|
|
suite.MojangApi.On("UsernamesToUuids", []string{"maksimkurb", "Thinkofdeath"}).Once().Return([]*mojang.ProfileInfo{
|
2019-04-19 04:11:52 +05:30
|
|
|
{Id: "0d252b7218b648bfb86c2ae476954d32", Name: "maksimkurb"},
|
|
|
|
{Id: "4566e69fc90748ee8d71d7ba5aa00d20", Name: "Thinkofdeath"},
|
|
|
|
}, nil)
|
2019-04-20 22:05:37 +05:30
|
|
|
suite.MojangApi.On("UuidToTextures", "0d252b7218b648bfb86c2ae476954d32", true).Once().Return(expectedResult1, nil)
|
|
|
|
suite.MojangApi.On("UuidToTextures", "4566e69fc90748ee8d71d7ba5aa00d20", true).Once().Return(expectedResult2, nil)
|
2019-04-19 04:11:52 +05:30
|
|
|
|
|
|
|
resultChan1 := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
resultChan2 := suite.Queue.GetTexturesForUsername("Thinkofdeath")
|
|
|
|
|
|
|
|
suite.Iterate()
|
|
|
|
|
2019-04-20 22:05:37 +05:30
|
|
|
suite.Assert().Equal(expectedResult1, <-resultChan1)
|
|
|
|
suite.Assert().Equal(expectedResult2, <-resultChan2)
|
2019-04-18 05:26:20 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestReceiveTexturesForUsernameWithCachedUuid() {
|
2019-04-21 00:52:02 +05:30
|
|
|
expectedResult := &mojang.SignedTexturesResponse{Id: "0d252b7218b648bfb86c2ae476954d32", Name: "maksimkurb"}
|
|
|
|
|
|
|
|
suite.Storage.On("GetUuid", "maksimkurb").Once().Return("0d252b7218b648bfb86c2ae476954d32", nil)
|
|
|
|
// Storage.StoreUuid shouldn't be called
|
|
|
|
suite.Storage.On("GetTextures", "0d252b7218b648bfb86c2ae476954d32").Once().Return(nil, &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreTextures", expectedResult).Once()
|
2019-04-21 05:34:03 +05:30
|
|
|
// MojangApi.UsernamesToUuids shouldn't be called
|
2019-04-21 00:52:02 +05:30
|
|
|
suite.MojangApi.On("UuidToTextures", "0d252b7218b648bfb86c2ae476954d32", true).Once().Return(expectedResult, nil)
|
|
|
|
|
|
|
|
resultChan := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
|
|
|
|
// Note that there is no iteration
|
|
|
|
|
|
|
|
result := <-resultChan
|
|
|
|
suite.Assert().Equal(expectedResult, result)
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestReceiveTexturesForUsernameWithFullyCachedResult() {
|
2019-04-21 00:52:02 +05:30
|
|
|
expectedResult := &mojang.SignedTexturesResponse{Id: "0d252b7218b648bfb86c2ae476954d32", Name: "maksimkurb"}
|
|
|
|
|
|
|
|
suite.Storage.On("GetUuid", "maksimkurb").Once().Return("0d252b7218b648bfb86c2ae476954d32", nil)
|
|
|
|
// Storage.StoreUuid shouldn't be called
|
|
|
|
suite.Storage.On("GetTextures", "0d252b7218b648bfb86c2ae476954d32").Once().Return(expectedResult, nil)
|
|
|
|
// Storage.StoreTextures shouldn't be called
|
2019-04-21 05:34:03 +05:30
|
|
|
// MojangApi.UsernamesToUuids shouldn't be called
|
2019-04-21 00:52:02 +05:30
|
|
|
// MojangApi.UuidToTextures shouldn't be called
|
|
|
|
|
|
|
|
resultChan := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
|
|
|
|
// Note that there is no iteration
|
|
|
|
|
|
|
|
result := <-resultChan
|
|
|
|
suite.Assert().Equal(expectedResult, result)
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestReceiveTexturesForUsernameWithCachedUnknownUuid() {
|
2019-04-21 00:52:02 +05:30
|
|
|
suite.Storage.On("GetUuid", "maksimkurb").Once().Return("", nil)
|
|
|
|
// Storage.StoreUuid shouldn't be called
|
|
|
|
// Storage.GetTextures shouldn't be called
|
|
|
|
// Storage.StoreTextures shouldn't be called
|
2019-04-21 05:34:03 +05:30
|
|
|
// MojangApi.UsernamesToUuids shouldn't be called
|
2019-04-21 00:52:02 +05:30
|
|
|
// MojangApi.UuidToTextures shouldn't be called
|
|
|
|
|
|
|
|
resultChan := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
|
|
|
|
// Note that there is no iteration
|
|
|
|
|
|
|
|
suite.Assert().Nil(<-resultChan)
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestReceiveTexturesForMoreThan100Usernames() {
|
2019-04-19 04:11:52 +05:30
|
|
|
usernames := make([]string, 120, 120)
|
|
|
|
for i := 0; i < 120; i++ {
|
|
|
|
usernames[i] = randStr(8)
|
|
|
|
}
|
|
|
|
|
2019-04-21 00:52:02 +05:30
|
|
|
suite.Storage.On("GetUuid", mock.Anything).Times(120).Return("", &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreUuid", mock.Anything, "").Times(120) // if username is not compared to uuid, then receive ""
|
|
|
|
// Storage.GetTextures and Storage.SetTextures shouldn't be called
|
2019-04-21 05:34:03 +05:30
|
|
|
suite.MojangApi.On("UsernamesToUuids", usernames[0:100]).Once().Return([]*mojang.ProfileInfo{}, nil)
|
|
|
|
suite.MojangApi.On("UsernamesToUuids", usernames[100:120]).Once().Return([]*mojang.ProfileInfo{}, nil)
|
2019-04-19 04:11:52 +05:30
|
|
|
|
|
|
|
for _, username := range usernames {
|
|
|
|
suite.Queue.GetTexturesForUsername(username)
|
2019-04-18 05:26:20 +05:30
|
|
|
}
|
2019-04-19 04:11:52 +05:30
|
|
|
|
|
|
|
suite.Iterate()
|
|
|
|
suite.Iterate()
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestReceiveTexturesForTheSameUsernames() {
|
2019-04-20 22:05:37 +05:30
|
|
|
expectedResult := &mojang.SignedTexturesResponse{Id: "0d252b7218b648bfb86c2ae476954d32", Name: "maksimkurb"}
|
|
|
|
|
2019-04-21 00:52:02 +05:30
|
|
|
suite.Storage.On("GetUuid", "maksimkurb").Twice().Return("", &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreUuid", "maksimkurb", "0d252b7218b648bfb86c2ae476954d32").Once()
|
|
|
|
suite.Storage.On("GetTextures", "0d252b7218b648bfb86c2ae476954d32").Once().Return(nil, &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreTextures", expectedResult).Once()
|
2019-04-21 05:34:03 +05:30
|
|
|
suite.MojangApi.On("UsernamesToUuids", []string{"maksimkurb"}).Once().Return([]*mojang.ProfileInfo{
|
2019-04-20 05:53:49 +05:30
|
|
|
{Id: "0d252b7218b648bfb86c2ae476954d32", Name: "maksimkurb"},
|
|
|
|
}, nil)
|
2019-04-20 22:05:37 +05:30
|
|
|
suite.MojangApi.On("UuidToTextures", "0d252b7218b648bfb86c2ae476954d32", true).Once().Return(expectedResult, nil)
|
2019-04-20 05:53:49 +05:30
|
|
|
|
|
|
|
resultChan1 := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
resultChan2 := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
|
|
|
|
suite.Iterate()
|
|
|
|
|
2019-04-20 22:05:37 +05:30
|
|
|
suite.Assert().Equal(expectedResult, <-resultChan1)
|
|
|
|
suite.Assert().Equal(expectedResult, <-resultChan2)
|
2019-04-20 05:53:49 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestReceiveTexturesForUsernameThatAlreadyProcessing() {
|
2019-04-20 22:05:37 +05:30
|
|
|
expectedResult := &mojang.SignedTexturesResponse{Id: "0d252b7218b648bfb86c2ae476954d32", Name: "maksimkurb"}
|
|
|
|
|
2019-04-21 00:52:02 +05:30
|
|
|
suite.Storage.On("GetUuid", "maksimkurb").Twice().Return("", &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreUuid", "maksimkurb", "0d252b7218b648bfb86c2ae476954d32").Once()
|
|
|
|
suite.Storage.On("GetTextures", "0d252b7218b648bfb86c2ae476954d32").Once().Return(nil, &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreTextures", expectedResult).Once()
|
2019-04-21 05:34:03 +05:30
|
|
|
suite.MojangApi.On("UsernamesToUuids", []string{"maksimkurb"}).Once().Return([]*mojang.ProfileInfo{
|
2019-04-20 05:53:49 +05:30
|
|
|
{Id: "0d252b7218b648bfb86c2ae476954d32", Name: "maksimkurb"},
|
|
|
|
}, nil)
|
|
|
|
suite.MojangApi.On("UuidToTextures", "0d252b7218b648bfb86c2ae476954d32", true).
|
|
|
|
Once().
|
|
|
|
After(10*time.Millisecond). // Simulate long round trip
|
2019-04-20 22:05:37 +05:30
|
|
|
Return(expectedResult, nil)
|
2019-04-20 05:53:49 +05:30
|
|
|
|
|
|
|
resultChan1 := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
|
|
|
|
// Note that for entire test there is only one iteration
|
|
|
|
suite.Iterate()
|
|
|
|
|
|
|
|
// Let it meet delayed UuidToTextures request
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
|
|
|
|
resultChan2 := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
|
2019-04-20 22:05:37 +05:30
|
|
|
suite.Assert().Equal(expectedResult, <-resultChan1)
|
|
|
|
suite.Assert().Equal(expectedResult, <-resultChan2)
|
2019-04-20 05:53:49 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestDoNothingWhenNoTasks() {
|
2019-04-21 00:52:02 +05:30
|
|
|
suite.Storage.On("GetUuid", "maksimkurb").Once().Return("", &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreUuid", "maksimkurb", "").Once()
|
|
|
|
// Storage.GetTextures and Storage.StoreTextures shouldn't be called
|
2019-04-21 05:34:03 +05:30
|
|
|
suite.MojangApi.On("UsernamesToUuids", []string{"maksimkurb"}).Once().Return([]*mojang.ProfileInfo{}, nil)
|
2019-04-19 04:11:52 +05:30
|
|
|
|
|
|
|
// Perform first iteration and await it finish
|
|
|
|
resultChan := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
|
|
|
|
suite.Iterate()
|
|
|
|
|
|
|
|
suite.Assert().Nil(<-resultChan)
|
|
|
|
|
|
|
|
// Let it to perform a few more iterations to ensure, that there is no calls to external APIs
|
|
|
|
suite.Iterate()
|
|
|
|
suite.Iterate()
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
type timeoutError struct {
|
2019-04-19 04:11:52 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (*timeoutError) Error() string { return "timeout error" }
|
|
|
|
func (*timeoutError) Timeout() bool { return true }
|
|
|
|
func (*timeoutError) Temporary() bool { return false }
|
|
|
|
|
|
|
|
var expectedErrors = []error{
|
|
|
|
&mojang.BadRequestError{},
|
|
|
|
&mojang.TooManyRequestsError{},
|
|
|
|
&mojang.ServerError{},
|
|
|
|
&timeoutError{},
|
|
|
|
&net.OpError{Op: "read"},
|
|
|
|
&net.OpError{Op: "dial"},
|
|
|
|
syscall.ECONNREFUSED,
|
2019-04-21 01:09:17 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestShouldNotPanicWhenExpectedErrorReturnedFromUsernameToUuidRequest() {
|
|
|
|
for _, err := range expectedErrors {
|
|
|
|
suite.Storage.On("GetUuid", "maksimkurb").Once().Return("", &ValueNotFound{})
|
|
|
|
suite.MojangApi.On("UsernamesToUuids", []string{"maksimkurb"}).Once().Return(nil, err)
|
|
|
|
resultChan := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
suite.Iterate()
|
|
|
|
suite.Assert().Nil(<-resultChan)
|
|
|
|
suite.MojangApi.AssertExpectations(suite.T())
|
|
|
|
suite.MojangApi.ExpectedCalls = nil // https://github.com/stretchr/testify/issues/558#issuecomment-372112364
|
|
|
|
}
|
2019-04-19 04:11:52 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestShouldNotPanicWhenExpectedErrorReturnedFromUuidToTexturesRequest() {
|
|
|
|
for _, err := range expectedErrors {
|
|
|
|
suite.Storage.On("GetUuid", "maksimkurb").Once().Return("", &ValueNotFound{})
|
|
|
|
suite.Storage.On("StoreUuid", "maksimkurb", "0d252b7218b648bfb86c2ae476954d32").Once()
|
|
|
|
suite.Storage.On("GetTextures", "0d252b7218b648bfb86c2ae476954d32").Once().Return(nil, &ValueNotFound{})
|
|
|
|
// Storage.StoreTextures shouldn't be called
|
|
|
|
suite.MojangApi.On("UsernamesToUuids", []string{"maksimkurb"}).Once().Return([]*mojang.ProfileInfo{
|
|
|
|
{Id: "0d252b7218b648bfb86c2ae476954d32", Name: "maksimkurb"},
|
|
|
|
}, nil)
|
|
|
|
suite.MojangApi.On("UuidToTextures", "0d252b7218b648bfb86c2ae476954d32", true).Once().Return(nil, err)
|
|
|
|
resultChan := suite.Queue.GetTexturesForUsername("maksimkurb")
|
|
|
|
suite.Iterate()
|
|
|
|
suite.Assert().Nil(<-resultChan)
|
|
|
|
suite.MojangApi.AssertExpectations(suite.T())
|
|
|
|
suite.MojangApi.ExpectedCalls = nil // https://github.com/stretchr/testify/issues/558#issuecomment-372112364
|
|
|
|
}
|
2019-04-21 01:34:29 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (suite *queueTestSuite) TestReceiveTexturesForNotAllowedMojangUsername() {
|
2019-04-20 22:21:55 +05:30
|
|
|
resultChan := suite.Queue.GetTexturesForUsername("Not allowed")
|
|
|
|
suite.Assert().Nil(<-resultChan)
|
|
|
|
}
|
|
|
|
|
2019-04-19 04:11:52 +05:30
|
|
|
func TestJobsQueueSuite(t *testing.T) {
|
2019-04-21 05:34:03 +05:30
|
|
|
suite.Run(t, new(queueTestSuite))
|
2019-04-18 05:26:20 +05:30
|
|
|
}
|
|
|
|
|
2019-04-20 22:34:57 +05:30
|
|
|
var replacer = strings.NewReplacer("-", "_", "=", "")
|
|
|
|
|
2019-04-18 05:26:20 +05:30
|
|
|
// https://stackoverflow.com/a/50581165
|
|
|
|
func randStr(len int) string {
|
|
|
|
buff := make([]byte, len)
|
2019-04-19 04:11:52 +05:30
|
|
|
_, _ = rand.Read(buff)
|
2019-04-20 22:34:57 +05:30
|
|
|
str := replacer.Replace(base64.URLEncoding.EncodeToString(buff))
|
2019-04-18 05:26:20 +05:30
|
|
|
|
|
|
|
// Base 64 can be longer than len
|
|
|
|
return str[:len]
|
|
|
|
}
|