diff --git a/bytesconv/serialization_test.go b/bytesconv/serialization_test.go index 9567a27..3888440 100644 --- a/bytesconv/serialization_test.go +++ b/bytesconv/serialization_test.go @@ -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) +} diff --git a/distributed_locker/consul_store.go b/distributed_locker/consul_store.go new file mode 100644 index 0000000..06362a5 --- /dev/null +++ b/distributed_locker/consul_store.go @@ -0,0 +1 @@ +package locker diff --git a/distributed_locker/distributed_lock.go b/distributed_locker/distributed_lock.go index 0755bac..b223187 100644 --- a/distributed_locker/distributed_lock.go +++ b/distributed_locker/distributed_lock.go @@ -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{ diff --git a/idGenerator/buffer_test.go b/idGenerator/buffer_test.go index a672550..59a1ef2 100644 --- a/idGenerator/buffer_test.go +++ b/idGenerator/buffer_test.go @@ -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("应用退出") } diff --git a/idGenerator/genterator_test.go b/idGenerator/genterator_test.go index 1080559..b0b031f 100644 --- a/idGenerator/genterator_test.go +++ b/idGenerator/genterator_test.go @@ -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"}) + }) } diff --git a/idGenerator/store/redis_store.go b/idGenerator/store/redis_store.go index b86b623..76e499c 100644 --- a/idGenerator/store/redis_store.go +++ b/idGenerator/store/redis_store.go @@ -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, diff --git a/idGenerator/store/redis_store_test.go b/idGenerator/store/redis_store_test.go index 49aa958..196980f 100644 --- a/idGenerator/store/redis_store_test.go +++ b/idGenerator/store/redis_store_test.go @@ -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"}) + }) } diff --git a/rand/rand_generator.go b/rand/rand_generator.go index 3c870f2..b3c40ed 100644 --- a/rand/rand_generator.go +++ b/rand/rand_generator.go @@ -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(), } } diff --git a/redis/redis.go b/redis/redis.go index 357ca47..a40797c 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -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 { diff --git a/tests/redis.go b/tests/redis.go index f40f326..37b49f5 100644 --- a/tests/redis.go +++ b/tests/redis.go @@ -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()