mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-17 04:30:03 +02:00
Formatting fix.
This commit is contained in:
parent
953113b456
commit
1e2dda54c8
9 changed files with 440 additions and 448 deletions
|
|
@ -1,42 +1,42 @@
|
|||
|
||||
package abi_cache
|
||||
|
||||
import (
|
||||
"time"
|
||||
"context"
|
||||
redis_cache "github.com/go-redis/cache/v8"
|
||||
eos "github.com/eoscanada/eos-go"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
eos "github.com/eoscanada/eos-go"
|
||||
redis_cache "github.com/go-redis/cache/v8"
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
c *redis_cache.Cache
|
||||
ctx context.Context
|
||||
prefix string
|
||||
c *redis_cache.Cache
|
||||
ctx context.Context
|
||||
prefix string
|
||||
}
|
||||
|
||||
func New(prefix string, options *redis_cache.Options) (*Cache) {
|
||||
return &Cache{
|
||||
c: redis_cache.New(options),
|
||||
ctx: context.Background(),
|
||||
prefix: prefix,
|
||||
}
|
||||
func New(prefix string, options *redis_cache.Options) *Cache {
|
||||
return &Cache{
|
||||
c: redis_cache.New(options),
|
||||
ctx: context.Background(),
|
||||
prefix: prefix,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Cache) Get(account string) (*eos.ABI, error) {
|
||||
var v eos.ABI
|
||||
err := this.c.Get(this.ctx, this.key(account), &v);
|
||||
return &v, err
|
||||
var v eos.ABI
|
||||
err := this.c.Get(this.ctx, this.key(account), &v)
|
||||
return &v, err
|
||||
}
|
||||
|
||||
func (this *Cache) Set(account string, abi *eos.ABI, ttl time.Duration) error {
|
||||
return this.c.Set(&redis_cache.Item{
|
||||
Ctx: this.ctx,
|
||||
Key: this.key(account),
|
||||
Value: *abi,
|
||||
TTL: ttl,
|
||||
})
|
||||
return this.c.Set(&redis_cache.Item{
|
||||
Ctx: this.ctx,
|
||||
Key: this.key(account),
|
||||
Value: *abi,
|
||||
TTL: ttl,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Cache) key(account string) (string) {
|
||||
return this.prefix + "." + account
|
||||
func (this *Cache) key(account string) string {
|
||||
return this.prefix + "." + account
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
|
||||
package abi_cache
|
||||
|
||||
import (
|
||||
"time"
|
||||
"strings"
|
||||
"github.com/go-redis/redis/v8"
|
||||
redis_cache "github.com/go-redis/cache/v8"
|
||||
eos "github.com/eoscanada/eos-go"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
eos "github.com/eoscanada/eos-go"
|
||||
redis_cache "github.com/go-redis/cache/v8"
|
||||
"github.com/go-redis/redis/v8"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var abiString = `
|
||||
|
|
@ -74,86 +74,84 @@ var abiString = `
|
|||
`
|
||||
|
||||
func TestGetSet(t *testing.T) {
|
||||
c := New("abi.cache.test", &redis_cache.Options{
|
||||
Redis: redis.NewClient(&redis.Options{}),
|
||||
// Cache 10k keys for 1 minute.
|
||||
LocalCache: redis_cache.NewTinyLFU(10000, time.Minute),
|
||||
})
|
||||
|
||||
c := New("abi.cache.test", &redis_cache.Options{
|
||||
Redis: redis.NewClient(&redis.Options{}),
|
||||
// Cache 10k keys for 1 minute.
|
||||
LocalCache: redis_cache.NewTinyLFU(10000, time.Minute),
|
||||
})
|
||||
abi, err := eos.NewABI(strings.NewReader(abiString))
|
||||
if err != nil {
|
||||
t.Error("Failed to build ABI", err)
|
||||
}
|
||||
|
||||
abi, err := eos.NewABI(strings.NewReader(abiString))
|
||||
if err != nil {
|
||||
t.Error("Failed to build ABI", err)
|
||||
}
|
||||
err = c.Set("testaccount", abi, time.Minute)
|
||||
if err != nil {
|
||||
t.Error("Failed to set cache item", err)
|
||||
}
|
||||
|
||||
err = c.Set("testaccount", abi, time.Minute)
|
||||
if err != nil {
|
||||
t.Error("Failed to set cache item", err)
|
||||
}
|
||||
c_abi, err := c.Get("testaccount")
|
||||
if err != nil {
|
||||
t.Error("Failed to get cache item", err)
|
||||
}
|
||||
|
||||
c_abi, err := c.Get("testaccount")
|
||||
if err != nil {
|
||||
t.Error("Failed to get cache item", err)
|
||||
}
|
||||
assert.Equal(t, c_abi.Version, "eosio::abi/1.0")
|
||||
|
||||
assert.Equal(t, c_abi.Version, "eosio::abi/1.0")
|
||||
// Types
|
||||
assert.Equal(t, c_abi.Types[0].NewTypeName, "new_type_name_1")
|
||||
assert.Equal(t, c_abi.Types[0].Type, "name")
|
||||
|
||||
// Types
|
||||
assert.Equal(t, c_abi.Types[0].NewTypeName, "new_type_name_1")
|
||||
assert.Equal(t, c_abi.Types[0].Type, "name")
|
||||
// Structs
|
||||
assert.Equal(t, c_abi.Structs[0].Name, "struct_name_1")
|
||||
assert.Equal(t, c_abi.Structs[0].Base, "struct_name_2")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[0].Name, "struct_1_field_1")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[0].Type, "new_type_name_1")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[1].Name, "struct_1_field_2")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[1].Type, "struct_name_3")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[2].Name, "struct_1_field_3")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[2].Type, "string?")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[3].Name, "struct_1_field_4")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[3].Type, "string?")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[4].Name, "struct_1_field_5")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[4].Type, "struct_name_4[]")
|
||||
|
||||
// Structs
|
||||
assert.Equal(t, c_abi.Structs[0].Name, "struct_name_1")
|
||||
assert.Equal(t, c_abi.Structs[0].Base, "struct_name_2")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[0].Name, "struct_1_field_1")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[0].Type, "new_type_name_1")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[1].Name, "struct_1_field_2")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[1].Type, "struct_name_3")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[2].Name, "struct_1_field_3")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[2].Type, "string?")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[3].Name, "struct_1_field_4")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[3].Type, "string?")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[4].Name, "struct_1_field_5")
|
||||
assert.Equal(t, c_abi.Structs[0].Fields[4].Type, "struct_name_4[]")
|
||||
assert.Equal(t, c_abi.Structs[1].Name, "struct_name_2")
|
||||
assert.Equal(t, c_abi.Structs[1].Base, "")
|
||||
assert.Equal(t, c_abi.Structs[1].Fields[0].Name, "struct_2_field_1")
|
||||
assert.Equal(t, c_abi.Structs[1].Fields[0].Type, "string")
|
||||
|
||||
assert.Equal(t, c_abi.Structs[1].Name, "struct_name_2")
|
||||
assert.Equal(t, c_abi.Structs[1].Base, "")
|
||||
assert.Equal(t, c_abi.Structs[1].Fields[0].Name, "struct_2_field_1")
|
||||
assert.Equal(t, c_abi.Structs[1].Fields[0].Type, "string")
|
||||
assert.Equal(t, c_abi.Structs[2].Name, "struct_name_3")
|
||||
assert.Equal(t, c_abi.Structs[2].Base, "")
|
||||
assert.Equal(t, c_abi.Structs[2].Fields[0].Name, "struct_3_field_1")
|
||||
assert.Equal(t, c_abi.Structs[2].Fields[0].Type, "string")
|
||||
|
||||
assert.Equal(t, c_abi.Structs[2].Name, "struct_name_3")
|
||||
assert.Equal(t, c_abi.Structs[2].Base, "")
|
||||
assert.Equal(t, c_abi.Structs[2].Fields[0].Name, "struct_3_field_1")
|
||||
assert.Equal(t, c_abi.Structs[2].Fields[0].Type, "string")
|
||||
assert.Equal(t, c_abi.Structs[3].Name, "struct_name_4")
|
||||
assert.Equal(t, c_abi.Structs[3].Base, "")
|
||||
assert.Equal(t, c_abi.Structs[3].Fields[0].Name, "struct_4_field_1")
|
||||
assert.Equal(t, c_abi.Structs[3].Fields[0].Type, "string")
|
||||
|
||||
assert.Equal(t, c_abi.Structs[3].Name, "struct_name_4")
|
||||
assert.Equal(t, c_abi.Structs[3].Base, "")
|
||||
assert.Equal(t, c_abi.Structs[3].Fields[0].Name, "struct_4_field_1")
|
||||
assert.Equal(t, c_abi.Structs[3].Fields[0].Type, "string")
|
||||
// Actions
|
||||
assert.Equal(t, c_abi.Actions[0].Name, eos.ActN("action_name_1"))
|
||||
assert.Equal(t, c_abi.Actions[0].Type, "struct_name_1")
|
||||
assert.Equal(t, c_abi.Actions[0].RicardianContract, "")
|
||||
|
||||
// Actions
|
||||
assert.Equal(t, c_abi.Actions[0].Name, eos.ActN("action_name_1"))
|
||||
assert.Equal(t, c_abi.Actions[0].Type, "struct_name_1")
|
||||
assert.Equal(t, c_abi.Actions[0].RicardianContract, "")
|
||||
|
||||
// Tables
|
||||
assert.Equal(t, c_abi.Tables[0].Name, eos.TableName("table_name_1"))
|
||||
assert.Equal(t, c_abi.Tables[0].Type, "struct_name_1")
|
||||
assert.Equal(t, c_abi.Tables[0].IndexType, "i64")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyNames[0], "key_name_1")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyNames[1], "key_name_2")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyTypes[0], "string")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyTypes[1], "int")
|
||||
// Tables
|
||||
assert.Equal(t, c_abi.Tables[0].Name, eos.TableName("table_name_1"))
|
||||
assert.Equal(t, c_abi.Tables[0].Type, "struct_name_1")
|
||||
assert.Equal(t, c_abi.Tables[0].IndexType, "i64")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyNames[0], "key_name_1")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyNames[1], "key_name_2")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyTypes[0], "string")
|
||||
assert.Equal(t, c_abi.Tables[0].KeyTypes[1], "int")
|
||||
}
|
||||
|
||||
func TestCacheMiss(t *testing.T) {
|
||||
c := New("abi.cache.test", &redis_cache.Options{
|
||||
Redis: redis.NewClient(&redis.Options{}),
|
||||
// Cache 10k keys for 1 minute.
|
||||
LocalCache: redis_cache.NewTinyLFU(10000, time.Minute),
|
||||
})
|
||||
|
||||
c := New("abi.cache.test", &redis_cache.Options{
|
||||
Redis: redis.NewClient(&redis.Options{}),
|
||||
// Cache 10k keys for 1 minute.
|
||||
LocalCache: redis_cache.NewTinyLFU(10000, time.Minute),
|
||||
})
|
||||
|
||||
_, err := c.Get("nonexist")
|
||||
require.Error(t, err)
|
||||
_, err := c.Get("nonexist")
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue