1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-17 08:02:40 +08:00
This commit is contained in:
2023-11-03 15:48:14 +08:00
parent 01f426c5b2
commit bdbf18969e
10 changed files with 93 additions and 33 deletions

View File

@ -1,8 +1,10 @@
package bytesconv
import (
"encoding/hex"
"encoding/json"
"testing"
"time"
)
type SimpleUser struct {
@ -25,3 +27,18 @@ func TestGob(t *testing.T) {
t.Logf("%+v", u2)
}
type delayTask struct {
message string
delay time.Time
execute func()
}
func TestMarshal(t *testing.T) {
d := delayTask{
message: "sssssssss",
}
b, err := Encode(d)
t.Log(hex.EncodeToString(b), err)
}

View File

@ -0,0 +1 @@
package locker

View File

@ -59,7 +59,7 @@ type distributedlock struct {
func NewDistributedLocker(ctx context.Context, key string, clients ...redis.Client) *distributedlock {
expire := defaultExpire
if deadline, ok := ctx.Deadline(); ok {
expire = deadline.Sub(time.Now())
expire = time.Until(deadline)
}
locker := &distributedlock{

View File

@ -1,7 +1,10 @@
package idgenerator
import (
"context"
"sync"
"testing"
"time"
"github.com/charlienet/go-mixed/idGenerator/store"
"github.com/charlienet/go-mixed/redis"
@ -10,7 +13,7 @@ import (
func TestBufferAlloc(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) {
tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
f := func() (*store.Segment, error) {
return store.NewRedisStore("sss", rdb).Assign(3, 99, 10)
}
@ -21,5 +24,28 @@ func TestBufferAlloc(t *testing.T) {
t.Log(b.allot())
}
}, redis.ReidsOption{Addr: "192.168.123.50:6379", Password: "123456"})
})
}
func TestTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-ctx.Done():
println("协程退出", ctx.Err().Error())
case <-time.After(time.Second * 100):
println("协程超时")
}
}()
wg.Wait()
println("应用退出")
}

View File

@ -10,8 +10,6 @@ import (
"github.com/charlienet/go-mixed/tests"
)
var redisOption = redis.ReidsOption{Addr: "192.168.123.50:6379", Password: "123456"}
func TestGenerator(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) {
generator, err := idgenerator.New(
@ -28,7 +26,7 @@ func TestGenerator(t *testing.T) {
}
func TestDecimalGenerator(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) {
tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
generator, err := idgenerator.New(
idgenerator.WithDecimalFormater(idgenerator.YYYYMMDDHHmmss, 3, 1),
idgenerator.WithRedis("idgen_test", rdb))
@ -39,11 +37,11 @@ func TestDecimalGenerator(t *testing.T) {
t.Log(generator.Next())
}
}, redis.ReidsOption{Addr: "192.168.123.50:6379", Password: "123456"})
})
}
func TestDecimalMonth(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) {
tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
generator, err := idgenerator.New(
idgenerator.WithDecimalFormater(idgenerator.YYYYMMDD, 2, 1),
idgenerator.WithRedis("idgen_test", rdb))
@ -54,11 +52,11 @@ func TestDecimalMonth(t *testing.T) {
t.Log(generator.Next())
}
}, redis.ReidsOption{Addr: "192.168.123.50:6379", Password: "123456"})
})
}
func TestParallelCreate(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) {
tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
var wg sync.WaitGroup
wg.Add(2)
@ -90,16 +88,15 @@ func TestParallelCreate(t *testing.T) {
wg.Wait()
}, redisOption)
})
}
func TestParallel(t *testing.T) {
set := sets.NewHashSet[int64]().Sync()
opt := redis.ReidsOption{Addr: "192.168.123.50:6379", Password: "123456"}
_ = set
f := func() {
tests.RunOnRedis(t, func(rdb redis.Client) {
tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
generator, err := idgenerator.New(
idgenerator.WithDecimalFormater(idgenerator.YYYYMMDDHHmmss, 3, 1),
idgenerator.WithRedis("idgen_testcccc", rdb))
@ -117,7 +114,7 @@ func TestParallel(t *testing.T) {
set.Add(id)
}
}, opt)
})
}
var wg sync.WaitGroup
@ -133,7 +130,7 @@ func TestParallel(t *testing.T) {
}
func BenchmarkGenerator(b *testing.B) {
tests.RunOnRedis(b, func(rdb redis.Client) {
tests.RunOnDefaultRedis(b, func(rdb redis.Client) {
b.Run("bbb", func(b *testing.B) {
generator, err := idgenerator.New(
idgenerator.WithDecimalFormater(idgenerator.YYYYMMDDHHmmss, 3, 1),
@ -147,5 +144,5 @@ func BenchmarkGenerator(b *testing.B) {
}
})
}, redis.ReidsOption{Addr: "192.168.123.50:6379", Password: "123456"})
})
}

View File

@ -7,10 +7,17 @@ import (
"sync"
"time"
_ "embed"
"github.com/charlienet/go-mixed/rand"
"github.com/charlienet/go-mixed/redis"
)
//go:embed redis_id_store.lua
var redis_id_function string
var once sync.Once
type redisStore struct {
rdb redis.Client
key string // 缓存键
@ -23,6 +30,8 @@ type redisStore struct {
}
func NewRedisStore(key string, rdb redis.Client) *redisStore {
once.Do(func() { rdb.LoadFunction(redis_id_function) })
return &redisStore{
rdb: rdb,
key: key,

View File

@ -10,16 +10,16 @@ import (
)
func TestSmallSerail(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) {
tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
s := store.NewRedisStore("sss", rdb)
for i := 0; i < 5; i++ {
t.Log(s.Assign(0, 9, 20))
}
}, redis.RedisOption{Addr: "192.168.123.50:6379", Password: "123456"})
})
}
func TestSmallAssign(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) {
tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
s := store.NewRedisStore("sss", rdb)
@ -27,11 +27,11 @@ func TestSmallAssign(t *testing.T) {
t.Log(s.Assign(0, 9, 30))
}
}, redis.RedisOption{Addr: "192.168.123.50:6379", Password: "123456"})
})
}
func TestBigAssign(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) {
tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
s := store.NewRedisStore("sss", rdb)
@ -39,11 +39,11 @@ func TestBigAssign(t *testing.T) {
t.Log(s.Assign(0, 99, 10))
}
}, redis.RedisOption{Addr: "192.168.123.50:6379", Password: "123456"})
})
}
func TestRedisAssign(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) {
tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
s := store.NewRedisStore("sss", rdb)
@ -51,11 +51,11 @@ func TestRedisAssign(t *testing.T) {
t.Log(s.Assign(21, 99, 30))
}
}, redis.RedisOption{Addr: "192.168.123.50:6379", Password: "123456"})
})
}
func TestFullRedisAssign(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) {
tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
s := store.NewRedisStore("sss", rdb)
@ -63,11 +63,11 @@ func TestFullRedisAssign(t *testing.T) {
t.Log(s.Assign(0, 999, 99))
}
}, redis.RedisOption{Addr: "192.168.123.50:6379", Password: "123456"})
})
}
func TestUpdateMachineCode(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) {
tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
for i := 0; i < 20; i++ {
s := store.NewRedisStore("id", rdb)
@ -85,7 +85,7 @@ func TestUpdateMachineCode(t *testing.T) {
time.Sleep(time.Second * 10)
}, redis.RedisOption{Addr: "192.168.123.50:6379", Password: "123456", Prefix: "cacc"})
})
}

View File

@ -2,9 +2,8 @@ package rand
import (
mrnd "math/rand"
"sync"
"time"
"github.com/charlienet/go-mixed/locker"
)
// 随机数生成器接口
@ -47,13 +46,12 @@ var (
type mathRandGenerator struct {
source mrnd.Source
r locker.Locker
r *sync.Mutex
}
func NewRandGenerator() *mathRandGenerator {
return &mathRandGenerator{
source: mrnd.NewSource(getSeed()),
r: locker.NewSpinLocker(),
}
}

View File

@ -128,7 +128,10 @@ func (rdb redisClient) Prefix() string {
}
func (rdb redisClient) LoadFunction(code string) {
rdb.FunctionLoadReplace(context.Background(), code)
_, err := rdb.FunctionLoadReplace(context.Background(), code).Result()
if err != nil {
panic(err)
}
}
func (rdb redisClient) Separator() string {

View File

@ -10,6 +10,15 @@ import (
"github.com/stretchr/testify/assert"
)
var DefaultRedis = redis.RedisOption{
Addr: "redis:6379",
Password: "123456",
}
func RunOnDefaultRedis(t assert.TestingT, fn func(rdb redis.Client)) {
RunOnRedis(t, fn, DefaultRedis)
}
func RunOnRedis(t assert.TestingT, fn func(rdb redis.Client), opt ...redis.RedisOption) {
var redis redis.Client
var clean func()