#1: Integrate queue to the application

This commit is contained in:
ErickSkrauch
2019-04-27 01:46:15 +03:00
parent f3690686ec
commit f7cdab243f
12 changed files with 298 additions and 117 deletions

View File

@@ -13,9 +13,31 @@ var HttpClient = &http.Client{
}
type SignedTexturesResponse struct {
Id string `json:"id"`
Name string `json:"name"`
Props []*Property `json:"properties"`
Id string `json:"id"`
Name string `json:"name"`
Props []*Property `json:"properties"`
decodedTextures *TexturesProp
}
func (t *SignedTexturesResponse) DecodeTextures() *TexturesProp {
if t.decodedTextures == nil {
var texturesProp string
for _, prop := range t.Props {
if prop.Name == "textures" {
texturesProp = prop.Value
break
}
}
if texturesProp == "" {
return nil
}
decodedTextures, _ := DecodeTextures(texturesProp)
t.decodedTextures = decodedTextures
}
return t.decodedTextures
}
type Property struct {

View File

@@ -8,6 +8,33 @@ import (
"gopkg.in/h2non/gock.v1"
)
func TestSignedTexturesResponse(t *testing.T) {
t.Run("DecodeTextures", func(t *testing.T) {
obj := &SignedTexturesResponse{
Id: "00000000000000000000000000000000",
Name: "mock",
Props: []*Property{
{
Name: "textures",
Value: "eyJ0aW1lc3RhbXAiOjE1NTU4NTYzMDc0MTIsInByb2ZpbGVJZCI6IjNlM2VlNmMzNWFmYTQ4YWJiNjFlOGNkOGM0MmZjMGQ5IiwicHJvZmlsZU5hbWUiOiJFcmlja1NrcmF1Y2giLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZmMxNzU3NjMzN2ExMDZkOWMyMmFjNzgyZTM2MmMxNmM0ZTBlNDliZTUzZmFhNDE4NTdiZmYzMzJiNzc5MjgxZSJ9fX0=",
},
},
}
textures := obj.DecodeTextures()
testify.Equal(t, "3e3ee6c35afa48abb61e8cd8c42fc0d9", textures.ProfileID)
})
t.Run("DecodedTextures without textures prop", func(t *testing.T) {
obj := &SignedTexturesResponse{
Id: "00000000000000000000000000000000",
Name: "mock",
Props: []*Property{},
}
textures := obj.DecodeTextures()
testify.Nil(t, textures)
})
}
func TestUsernamesToUuids(t *testing.T) {
t.Run("exchange usernames to uuids", func(t *testing.T) {
assert := testify.New(t)

View File

@@ -33,6 +33,7 @@ type JobsQueue struct {
}
func (ctx *JobsQueue) GetTexturesForUsername(username string) chan *mojang.SignedTexturesResponse {
// TODO: convert username to lower case
ctx.onFirstCall.Do(func() {
ctx.queue.New()
ctx.broadcast = newBroadcaster()

View File

@@ -20,6 +20,29 @@ type Storage interface {
TexturesStorage
}
// SplittedStorage allows you to use separate storage engines to satisfy
// the Storage interface
type SplittedStorage struct {
UuidsStorage
TexturesStorage
}
func (s *SplittedStorage) GetUuid(username string) (string, error) {
return s.UuidsStorage.GetUuid(username)
}
func (s *SplittedStorage) StoreUuid(username string, uuid string) {
s.UuidsStorage.StoreUuid(username, uuid)
}
func (s *SplittedStorage) GetTextures(uuid string) (*mojang.SignedTexturesResponse, error) {
return s.TexturesStorage.GetTextures(uuid)
}
func (s *SplittedStorage) StoreTextures(textures *mojang.SignedTexturesResponse) {
s.TexturesStorage.StoreTextures(textures)
}
// This error can be used to indicate, that requested
// value doesn't exists in the storage
type ValueNotFound struct {

View File

@@ -0,0 +1,88 @@
package queue
import (
"github.com/elyby/chrly/api/mojang"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
type uuidsStorageMock struct {
mock.Mock
}
func (m *uuidsStorageMock) GetUuid(username string) (string, error) {
args := m.Called(username)
return args.String(0), args.Error(1)
}
func (m *uuidsStorageMock) StoreUuid(username string, uuid string) {
m.Called(username, uuid)
}
type texturesStorageMock struct {
mock.Mock
}
func (m *texturesStorageMock) GetTextures(uuid string) (*mojang.SignedTexturesResponse, error) {
args := m.Called(uuid)
var result *mojang.SignedTexturesResponse
if casted, ok := args.Get(0).(*mojang.SignedTexturesResponse); ok {
result = casted
}
return result, args.Error(1)
}
func (m *texturesStorageMock) StoreTextures(textures *mojang.SignedTexturesResponse) {
m.Called(textures)
}
func TestSplittedStorage(t *testing.T) {
createMockedStorage := func() (*SplittedStorage, *uuidsStorageMock, *texturesStorageMock) {
uuidsStorage := &uuidsStorageMock{}
texturesStorage := &texturesStorageMock{}
return &SplittedStorage{uuidsStorage, texturesStorage}, uuidsStorage, texturesStorage
}
t.Run("GetUuid", func(t *testing.T) {
storage, uuidsMock, _ := createMockedStorage()
uuidsMock.On("GetUuid", "username").Once().Return("find me", nil)
result, err := storage.GetUuid("username")
assert.Nil(t, err)
assert.Equal(t, "find me", result)
uuidsMock.AssertExpectations(t)
})
t.Run("StoreUuid", func(t *testing.T) {
storage, uuidsMock, _ := createMockedStorage()
uuidsMock.On("StoreUuid", "username", "result").Once()
storage.StoreUuid("username", "result")
uuidsMock.AssertExpectations(t)
})
t.Run("GetTextures", func(t *testing.T) {
result := &mojang.SignedTexturesResponse{Id: "mock id"}
storage, _, texturesMock := createMockedStorage()
texturesMock.On("GetTextures", "uuid").Once().Return(result, nil)
returned, err := storage.GetTextures("uuid")
assert.Nil(t, err)
assert.Equal(t, result, returned)
texturesMock.AssertExpectations(t)
})
t.Run("StoreTextures", func(t *testing.T) {
toStore := &mojang.SignedTexturesResponse{Id: "mock id"}
storage, _, texturesMock := createMockedStorage()
texturesMock.On("StoreTextures", toStore).Once()
storage.StoreTextures(toStore)
texturesMock.AssertExpectations(t)
})
}
func TestValueNotFound_Error(t *testing.T) {
err := &ValueNotFound{}
assert.Equal(t, "value not found in the storage", err.Error())
}