2019-11-21 04:03:13 +05:30
|
|
|
package mojangtextures
|
2019-04-21 22:58:58 +05:30
|
|
|
|
|
|
|
import (
|
2020-04-28 20:27:51 +05:30
|
|
|
"testing"
|
2019-04-21 22:58:58 +05:30
|
|
|
"time"
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
assert "github.com/stretchr/testify/require"
|
2019-04-21 22:58:58 +05:30
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
"github.com/elyby/chrly/api/mojang"
|
2019-04-21 22:58:58 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
var texturesWithSkin = &mojang.SignedTexturesResponse{
|
|
|
|
Id: "dead24f9a4fa4877b7b04c8c6c72bb46",
|
|
|
|
Name: "mock",
|
|
|
|
Props: []*mojang.Property{
|
|
|
|
{
|
|
|
|
Name: "textures",
|
|
|
|
Value: mojang.EncodeTextures(&mojang.TexturesProp{
|
|
|
|
Timestamp: time.Now().UnixNano() / 10e5,
|
|
|
|
ProfileID: "dead24f9a4fa4877b7b04c8c6c72bb46",
|
|
|
|
ProfileName: "mock",
|
|
|
|
Textures: &mojang.TexturesResponse{
|
|
|
|
Skin: &mojang.SkinTexturesResponse{
|
|
|
|
Url: "http://textures.minecraft.net/texture/74d1e08b0bb7e9f590af27758125bbed1778ac6cef729aedfcb9613e9911ae75",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var texturesWithoutSkin = &mojang.SignedTexturesResponse{
|
|
|
|
Id: "dead24f9a4fa4877b7b04c8c6c72bb46",
|
|
|
|
Name: "mock",
|
|
|
|
Props: []*mojang.Property{
|
|
|
|
{
|
|
|
|
Name: "textures",
|
|
|
|
Value: mojang.EncodeTextures(&mojang.TexturesProp{
|
|
|
|
Timestamp: time.Now().UnixNano() / 10e5,
|
|
|
|
ProfileID: "dead24f9a4fa4877b7b04c8c6c72bb46",
|
|
|
|
ProfileName: "mock",
|
|
|
|
Textures: &mojang.TexturesResponse{},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInMemoryTexturesStorage_GetTextures(t *testing.T) {
|
2020-04-28 20:27:51 +05:30
|
|
|
t.Run("should return nil, nil when textures are unavailable", func(t *testing.T) {
|
2019-11-21 04:03:13 +05:30
|
|
|
storage := NewInMemoryTexturesStorage()
|
2019-04-21 22:58:58 +05:30
|
|
|
result, err := storage.GetTextures("b5d58475007d4f9e9ddd1403e2497579")
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
assert.Nil(t, result)
|
|
|
|
assert.Nil(t, err)
|
2019-04-21 22:58:58 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get textures object, when uuid is stored in the storage", func(t *testing.T) {
|
2019-11-21 04:03:13 +05:30
|
|
|
storage := NewInMemoryTexturesStorage()
|
2019-05-06 01:36:29 +05:30
|
|
|
storage.StoreTextures("dead24f9a4fa4877b7b04c8c6c72bb46", texturesWithSkin)
|
2019-04-21 22:58:58 +05:30
|
|
|
result, err := storage.GetTextures("dead24f9a4fa4877b7b04c8c6c72bb46")
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
assert.Equal(t, texturesWithSkin, result)
|
|
|
|
assert.Nil(t, err)
|
2019-04-21 22:58:58 +05:30
|
|
|
})
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
t.Run("should return nil, nil when textures are exists, but cache duration is expired", func(t *testing.T) {
|
2019-11-21 04:03:13 +05:30
|
|
|
storage := NewInMemoryTexturesStorage()
|
2020-04-30 02:54:41 +05:30
|
|
|
storage.Duration = 10 * time.Millisecond
|
|
|
|
storage.GCPeriod = time.Minute
|
2019-05-06 01:36:29 +05:30
|
|
|
storage.StoreTextures("dead24f9a4fa4877b7b04c8c6c72bb46", texturesWithSkin)
|
2019-04-21 22:58:58 +05:30
|
|
|
|
2020-04-30 02:54:41 +05:30
|
|
|
time.Sleep(storage.Duration * 2)
|
2019-04-21 22:58:58 +05:30
|
|
|
|
|
|
|
result, err := storage.GetTextures("dead24f9a4fa4877b7b04c8c6c72bb46")
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
assert.Nil(t, result)
|
|
|
|
assert.Nil(t, err)
|
2019-04-21 22:58:58 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInMemoryTexturesStorage_StoreTextures(t *testing.T) {
|
|
|
|
t.Run("store textures for previously not existed uuid", func(t *testing.T) {
|
2019-11-21 04:03:13 +05:30
|
|
|
storage := NewInMemoryTexturesStorage()
|
2019-05-06 01:36:29 +05:30
|
|
|
storage.StoreTextures("dead24f9a4fa4877b7b04c8c6c72bb46", texturesWithSkin)
|
2019-04-21 22:58:58 +05:30
|
|
|
result, err := storage.GetTextures("dead24f9a4fa4877b7b04c8c6c72bb46")
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
assert.Equal(t, texturesWithSkin, result)
|
|
|
|
assert.Nil(t, err)
|
2019-04-21 22:58:58 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("override already existed textures for uuid", func(t *testing.T) {
|
2019-11-21 04:03:13 +05:30
|
|
|
storage := NewInMemoryTexturesStorage()
|
2019-05-06 01:36:29 +05:30
|
|
|
storage.StoreTextures("dead24f9a4fa4877b7b04c8c6c72bb46", texturesWithoutSkin)
|
|
|
|
storage.StoreTextures("dead24f9a4fa4877b7b04c8c6c72bb46", texturesWithSkin)
|
2019-04-21 22:58:58 +05:30
|
|
|
result, err := storage.GetTextures("dead24f9a4fa4877b7b04c8c6c72bb46")
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
assert.NotEqual(t, texturesWithoutSkin, result)
|
|
|
|
assert.Equal(t, texturesWithSkin, result)
|
|
|
|
assert.Nil(t, err)
|
2019-04-21 22:58:58 +05:30
|
|
|
})
|
2019-04-30 03:15:29 +05:30
|
|
|
|
2020-04-30 02:54:41 +05:30
|
|
|
t.Run("store textures with empty properties", func(t *testing.T) {
|
|
|
|
texturesWithEmptyProps := &mojang.SignedTexturesResponse{
|
|
|
|
Id: "dead24f9a4fa4877b7b04c8c6c72bb46",
|
|
|
|
Name: "mock",
|
|
|
|
Props: []*mojang.Property{},
|
|
|
|
}
|
|
|
|
|
2019-11-21 04:03:13 +05:30
|
|
|
storage := NewInMemoryTexturesStorage()
|
2020-04-30 02:54:41 +05:30
|
|
|
storage.StoreTextures("dead24f9a4fa4877b7b04c8c6c72bb46", texturesWithEmptyProps)
|
2019-05-06 01:36:29 +05:30
|
|
|
result, err := storage.GetTextures("dead24f9a4fa4877b7b04c8c6c72bb46")
|
|
|
|
|
2020-04-30 02:54:41 +05:30
|
|
|
assert.Exactly(t, texturesWithEmptyProps, result)
|
2020-04-28 20:27:51 +05:30
|
|
|
assert.Nil(t, err)
|
2019-05-06 01:36:29 +05:30
|
|
|
})
|
|
|
|
|
2020-04-30 02:54:41 +05:30
|
|
|
t.Run("store nil textures", func(t *testing.T) {
|
|
|
|
storage := NewInMemoryTexturesStorage()
|
|
|
|
storage.StoreTextures("dead24f9a4fa4877b7b04c8c6c72bb46", nil)
|
|
|
|
result, err := storage.GetTextures("dead24f9a4fa4877b7b04c8c6c72bb46")
|
2019-04-30 03:15:29 +05:30
|
|
|
|
2020-04-30 02:54:41 +05:30
|
|
|
assert.Nil(t, result)
|
|
|
|
assert.Nil(t, err)
|
2019-04-30 03:15:29 +05:30
|
|
|
})
|
2019-04-21 22:58:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestInMemoryTexturesStorage_GarbageCollection(t *testing.T) {
|
2019-11-21 04:03:13 +05:30
|
|
|
storage := NewInMemoryTexturesStorage()
|
2020-04-30 02:54:41 +05:30
|
|
|
defer storage.Stop()
|
|
|
|
storage.GCPeriod = 40 * time.Millisecond
|
|
|
|
storage.Duration = 40 * time.Millisecond
|
2019-04-21 22:58:58 +05:30
|
|
|
|
|
|
|
textures1 := &mojang.SignedTexturesResponse{
|
|
|
|
Id: "dead24f9a4fa4877b7b04c8c6c72bb46",
|
|
|
|
Name: "mock1",
|
|
|
|
Props: []*mojang.Property{
|
|
|
|
{
|
|
|
|
Name: "textures",
|
|
|
|
Value: mojang.EncodeTextures(&mojang.TexturesProp{
|
2020-04-30 02:54:41 +05:30
|
|
|
Timestamp: time.Now().UnixNano() / 10e5,
|
2019-04-21 22:58:58 +05:30
|
|
|
ProfileID: "dead24f9a4fa4877b7b04c8c6c72bb46",
|
|
|
|
ProfileName: "mock1",
|
|
|
|
Textures: &mojang.TexturesResponse{},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-04-30 02:54:41 +05:30
|
|
|
|
2019-04-21 22:58:58 +05:30
|
|
|
textures2 := &mojang.SignedTexturesResponse{
|
|
|
|
Id: "b5d58475007d4f9e9ddd1403e2497579",
|
|
|
|
Name: "mock2",
|
|
|
|
Props: []*mojang.Property{
|
|
|
|
{
|
|
|
|
Name: "textures",
|
|
|
|
Value: mojang.EncodeTextures(&mojang.TexturesProp{
|
2020-04-30 02:54:41 +05:30
|
|
|
Timestamp: time.Now().UnixNano() / 10e5,
|
2019-04-21 22:58:58 +05:30
|
|
|
ProfileID: "b5d58475007d4f9e9ddd1403e2497579",
|
|
|
|
ProfileName: "mock2",
|
|
|
|
Textures: &mojang.TexturesResponse{},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-06 01:36:29 +05:30
|
|
|
storage.StoreTextures("dead24f9a4fa4877b7b04c8c6c72bb46", textures1)
|
2020-04-30 02:54:41 +05:30
|
|
|
time.Sleep(storage.GCPeriod / 4) // Store second texture a bit later to avoid it removing by gc
|
2019-05-06 01:36:29 +05:30
|
|
|
storage.StoreTextures("b5d58475007d4f9e9ddd1403e2497579", textures2)
|
2019-04-21 22:58:58 +05:30
|
|
|
|
2020-04-30 02:54:41 +05:30
|
|
|
time.Sleep(storage.GCPeriod) // Let it start first iteration
|
2019-04-21 22:58:58 +05:30
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
texturesFromStorage1, textures1Err := storage.GetTextures("dead24f9a4fa4877b7b04c8c6c72bb46")
|
|
|
|
texturesFromStorage2, textures2Err := storage.GetTextures("b5d58475007d4f9e9ddd1403e2497579")
|
2019-04-21 22:58:58 +05:30
|
|
|
|
2020-04-30 02:54:41 +05:30
|
|
|
assert.Nil(t, texturesFromStorage1)
|
2020-04-28 20:27:51 +05:30
|
|
|
assert.Nil(t, textures1Err)
|
2020-04-30 02:54:41 +05:30
|
|
|
assert.NotNil(t, texturesFromStorage2)
|
2020-04-28 20:27:51 +05:30
|
|
|
assert.Nil(t, textures2Err)
|
2019-04-21 22:58:58 +05:30
|
|
|
|
2020-04-30 02:54:41 +05:30
|
|
|
time.Sleep(storage.GCPeriod) // Let another iteration happen
|
2019-04-21 22:58:58 +05:30
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
texturesFromStorage1, textures1Err = storage.GetTextures("dead24f9a4fa4877b7b04c8c6c72bb46")
|
|
|
|
texturesFromStorage2, textures2Err = storage.GetTextures("b5d58475007d4f9e9ddd1403e2497579")
|
2019-04-21 22:58:58 +05:30
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
assert.Nil(t, texturesFromStorage1)
|
|
|
|
assert.Nil(t, textures1Err)
|
|
|
|
assert.Nil(t, texturesFromStorage2)
|
|
|
|
assert.Nil(t, textures2Err)
|
2019-04-21 22:58:58 +05:30
|
|
|
}
|