1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +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 package bytesconv
import ( import (
"encoding/hex"
"encoding/json" "encoding/json"
"testing" "testing"
"time"
) )
type SimpleUser struct { type SimpleUser struct {
@ -25,3 +27,18 @@ func TestGob(t *testing.T) {
t.Logf("%+v", u2) 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 { func NewDistributedLocker(ctx context.Context, key string, clients ...redis.Client) *distributedlock {
expire := defaultExpire expire := defaultExpire
if deadline, ok := ctx.Deadline(); ok { if deadline, ok := ctx.Deadline(); ok {
expire = deadline.Sub(time.Now()) expire = time.Until(deadline)
} }
locker := &distributedlock{ locker := &distributedlock{

View File

@ -1,7 +1,10 @@
package idgenerator package idgenerator
import ( import (
"context"
"sync"
"testing" "testing"
"time"
"github.com/charlienet/go-mixed/idGenerator/store" "github.com/charlienet/go-mixed/idGenerator/store"
"github.com/charlienet/go-mixed/redis" "github.com/charlienet/go-mixed/redis"
@ -10,7 +13,7 @@ import (
func TestBufferAlloc(t *testing.T) { func TestBufferAlloc(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) { tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
f := func() (*store.Segment, error) { f := func() (*store.Segment, error) {
return store.NewRedisStore("sss", rdb).Assign(3, 99, 10) return store.NewRedisStore("sss", rdb).Assign(3, 99, 10)
} }
@ -21,5 +24,28 @@ func TestBufferAlloc(t *testing.T) {
t.Log(b.allot()) 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" "github.com/charlienet/go-mixed/tests"
) )
var redisOption = redis.ReidsOption{Addr: "192.168.123.50:6379", Password: "123456"}
func TestGenerator(t *testing.T) { func TestGenerator(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) { tests.RunOnRedis(t, func(rdb redis.Client) {
generator, err := idgenerator.New( generator, err := idgenerator.New(
@ -28,7 +26,7 @@ func TestGenerator(t *testing.T) {
} }
func TestDecimalGenerator(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( generator, err := idgenerator.New(
idgenerator.WithDecimalFormater(idgenerator.YYYYMMDDHHmmss, 3, 1), idgenerator.WithDecimalFormater(idgenerator.YYYYMMDDHHmmss, 3, 1),
idgenerator.WithRedis("idgen_test", rdb)) idgenerator.WithRedis("idgen_test", rdb))
@ -39,11 +37,11 @@ func TestDecimalGenerator(t *testing.T) {
t.Log(generator.Next()) t.Log(generator.Next())
} }
}, redis.ReidsOption{Addr: "192.168.123.50:6379", Password: "123456"}) })
} }
func TestDecimalMonth(t *testing.T) { func TestDecimalMonth(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) { tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
generator, err := idgenerator.New( generator, err := idgenerator.New(
idgenerator.WithDecimalFormater(idgenerator.YYYYMMDD, 2, 1), idgenerator.WithDecimalFormater(idgenerator.YYYYMMDD, 2, 1),
idgenerator.WithRedis("idgen_test", rdb)) idgenerator.WithRedis("idgen_test", rdb))
@ -54,11 +52,11 @@ func TestDecimalMonth(t *testing.T) {
t.Log(generator.Next()) t.Log(generator.Next())
} }
}, redis.ReidsOption{Addr: "192.168.123.50:6379", Password: "123456"}) })
} }
func TestParallelCreate(t *testing.T) { func TestParallelCreate(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) { tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2) wg.Add(2)
@ -90,16 +88,15 @@ func TestParallelCreate(t *testing.T) {
wg.Wait() wg.Wait()
}, redisOption) })
} }
func TestParallel(t *testing.T) { func TestParallel(t *testing.T) {
set := sets.NewHashSet[int64]().Sync() set := sets.NewHashSet[int64]().Sync()
opt := redis.ReidsOption{Addr: "192.168.123.50:6379", Password: "123456"}
_ = set _ = set
f := func() { f := func() {
tests.RunOnRedis(t, func(rdb redis.Client) { tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
generator, err := idgenerator.New( generator, err := idgenerator.New(
idgenerator.WithDecimalFormater(idgenerator.YYYYMMDDHHmmss, 3, 1), idgenerator.WithDecimalFormater(idgenerator.YYYYMMDDHHmmss, 3, 1),
idgenerator.WithRedis("idgen_testcccc", rdb)) idgenerator.WithRedis("idgen_testcccc", rdb))
@ -117,7 +114,7 @@ func TestParallel(t *testing.T) {
set.Add(id) set.Add(id)
} }
}, opt) })
} }
var wg sync.WaitGroup var wg sync.WaitGroup
@ -133,7 +130,7 @@ func TestParallel(t *testing.T) {
} }
func BenchmarkGenerator(b *testing.B) { 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) { b.Run("bbb", func(b *testing.B) {
generator, err := idgenerator.New( generator, err := idgenerator.New(
idgenerator.WithDecimalFormater(idgenerator.YYYYMMDDHHmmss, 3, 1), 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" "sync"
"time" "time"
_ "embed"
"github.com/charlienet/go-mixed/rand" "github.com/charlienet/go-mixed/rand"
"github.com/charlienet/go-mixed/redis" "github.com/charlienet/go-mixed/redis"
) )
//go:embed redis_id_store.lua
var redis_id_function string
var once sync.Once
type redisStore struct { type redisStore struct {
rdb redis.Client rdb redis.Client
key string // 缓存键 key string // 缓存键
@ -23,6 +30,8 @@ type redisStore struct {
} }
func NewRedisStore(key string, rdb redis.Client) *redisStore { func NewRedisStore(key string, rdb redis.Client) *redisStore {
once.Do(func() { rdb.LoadFunction(redis_id_function) })
return &redisStore{ return &redisStore{
rdb: rdb, rdb: rdb,
key: key, key: key,

View File

@ -10,16 +10,16 @@ import (
) )
func TestSmallSerail(t *testing.T) { func TestSmallSerail(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) { tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
s := store.NewRedisStore("sss", rdb) s := store.NewRedisStore("sss", rdb)
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
t.Log(s.Assign(0, 9, 20)) t.Log(s.Assign(0, 9, 20))
} }
}, redis.RedisOption{Addr: "192.168.123.50:6379", Password: "123456"}) })
} }
func TestSmallAssign(t *testing.T) { func TestSmallAssign(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) { tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
s := store.NewRedisStore("sss", rdb) s := store.NewRedisStore("sss", rdb)
@ -27,11 +27,11 @@ func TestSmallAssign(t *testing.T) {
t.Log(s.Assign(0, 9, 30)) t.Log(s.Assign(0, 9, 30))
} }
}, redis.RedisOption{Addr: "192.168.123.50:6379", Password: "123456"}) })
} }
func TestBigAssign(t *testing.T) { func TestBigAssign(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) { tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
s := store.NewRedisStore("sss", rdb) s := store.NewRedisStore("sss", rdb)
@ -39,11 +39,11 @@ func TestBigAssign(t *testing.T) {
t.Log(s.Assign(0, 99, 10)) t.Log(s.Assign(0, 99, 10))
} }
}, redis.RedisOption{Addr: "192.168.123.50:6379", Password: "123456"}) })
} }
func TestRedisAssign(t *testing.T) { func TestRedisAssign(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) { tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
s := store.NewRedisStore("sss", rdb) s := store.NewRedisStore("sss", rdb)
@ -51,11 +51,11 @@ func TestRedisAssign(t *testing.T) {
t.Log(s.Assign(21, 99, 30)) t.Log(s.Assign(21, 99, 30))
} }
}, redis.RedisOption{Addr: "192.168.123.50:6379", Password: "123456"}) })
} }
func TestFullRedisAssign(t *testing.T) { func TestFullRedisAssign(t *testing.T) {
tests.RunOnRedis(t, func(rdb redis.Client) { tests.RunOnDefaultRedis(t, func(rdb redis.Client) {
s := store.NewRedisStore("sss", rdb) s := store.NewRedisStore("sss", rdb)
@ -63,11 +63,11 @@ func TestFullRedisAssign(t *testing.T) {
t.Log(s.Assign(0, 999, 99)) t.Log(s.Assign(0, 999, 99))
} }
}, redis.RedisOption{Addr: "192.168.123.50:6379", Password: "123456"}) })
} }
func TestUpdateMachineCode(t *testing.T) { 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++ { for i := 0; i < 20; i++ {
s := store.NewRedisStore("id", rdb) s := store.NewRedisStore("id", rdb)
@ -85,7 +85,7 @@ func TestUpdateMachineCode(t *testing.T) {
time.Sleep(time.Second * 10) 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 ( import (
mrnd "math/rand" mrnd "math/rand"
"sync"
"time" "time"
"github.com/charlienet/go-mixed/locker"
) )
// 随机数生成器接口 // 随机数生成器接口
@ -47,13 +46,12 @@ var (
type mathRandGenerator struct { type mathRandGenerator struct {
source mrnd.Source source mrnd.Source
r locker.Locker r *sync.Mutex
} }
func NewRandGenerator() *mathRandGenerator { func NewRandGenerator() *mathRandGenerator {
return &mathRandGenerator{ return &mathRandGenerator{
source: mrnd.NewSource(getSeed()), source: mrnd.NewSource(getSeed()),
r: locker.NewSpinLocker(),
} }
} }

View File

@ -128,7 +128,10 @@ func (rdb redisClient) Prefix() string {
} }
func (rdb redisClient) LoadFunction(code 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 { func (rdb redisClient) Separator() string {

View File

@ -10,6 +10,15 @@ import (
"github.com/stretchr/testify/assert" "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) { func RunOnRedis(t assert.TestingT, fn func(rdb redis.Client), opt ...redis.RedisOption) {
var redis redis.Client var redis redis.Client
var clean func() var clean func()