mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
update
This commit is contained in:
@ -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("应用退出")
|
||||
}
|
||||
|
@ -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"})
|
||||
})
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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"})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user