增加错误日志记录报文
This commit is contained in:
parent
78d3015aa4
commit
5494d7079e
|
|
@ -1,17 +1,17 @@
|
||||||
package uid
|
package uid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/bwmarrin/snowflake"
|
"github.com/bwmarrin/snowflake"
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"math"
|
"math"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSignGenerator_GetNumber(t *testing.T) {
|
func TestGenerator_GetNumber(t *testing.T) {
|
||||||
rdb := redis.NewClient(&redis.Options{
|
rdb := redis.NewClient(&redis.Options{
|
||||||
Addr: "47.97.27.195:6379",
|
Addr: "47.97.27.195:6379",
|
||||||
Password: "lansexiongdi@666",
|
Password: "lansexiongdi@666",
|
||||||
|
|
@ -27,7 +27,7 @@ func TestSignGenerator_GetNumber(t *testing.T) {
|
||||||
t.Log(number)
|
t.Log(number)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSignGenerator_Num(t *testing.T) {
|
func TestGenerator_Num(t *testing.T) {
|
||||||
rdb := redis.NewClient(&redis.Options{
|
rdb := redis.NewClient(&redis.Options{
|
||||||
Addr: "47.97.27.195:6379",
|
Addr: "47.97.27.195:6379",
|
||||||
Password: "lansexiongdi@666",
|
Password: "lansexiongdi@666",
|
||||||
|
|
@ -47,7 +47,7 @@ func TestSignGenerator_Num(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, exists := ids.LoadOrStore(id, true); exists {
|
if _, exists := ids.LoadOrStore(id, true); exists {
|
||||||
t.Errorf("duplicate ID generated")
|
t.Errorf("duplicate ID generated, id:%d", id)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
@ -60,22 +60,11 @@ func TestSignGenerator_ReverseNumber(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_GenerateNo(t *testing.T) {
|
func Test_GenerateNo(t *testing.T) {
|
||||||
//no := GenerateNo()
|
|
||||||
//
|
|
||||||
//t.Log(len(no))
|
|
||||||
//t.Log(no)
|
|
||||||
|
|
||||||
//p := os.Getpid()
|
|
||||||
//
|
|
||||||
//t.Log(p)
|
|
||||||
//t.Log(p % 1000)
|
|
||||||
|
|
||||||
//uid := uuid.New().String()
|
//uid := uuid.New().String()
|
||||||
//t.Log(len(uid))
|
//t.Log(len(uid))
|
||||||
//t.Log(uid)
|
//t.Log(uid)
|
||||||
|
|
||||||
//serverIdStr, _ := os.Hostname()
|
serverIdStr, _ := os.Hostname()
|
||||||
serverIdStr := "lsxddeMac-mini1111111dddddddddddddwffdsf"
|
|
||||||
t.Log(serverIdStr)
|
t.Log(serverIdStr)
|
||||||
|
|
||||||
id := hashMod(serverIdStr)
|
id := hashMod(serverIdStr)
|
||||||
|
|
@ -101,17 +90,6 @@ func hashMod(hashStr string) int {
|
||||||
return int(math.Mod(float64(hashValue), 32))
|
return int(math.Mod(float64(hashValue), 32))
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateSnowflakeID(nodeID int64) (string, error) {
|
|
||||||
if nodeID < 0 || nodeID > 1023 {
|
|
||||||
return "", fmt.Errorf("invalid node ID: %d", nodeID)
|
|
||||||
}
|
|
||||||
node, err := snowflake.NewNode(nodeID)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return node.Generate().String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGenerateID(t *testing.T) {
|
func TestGenerateID(t *testing.T) {
|
||||||
nodeID := int64(1) // 确保唯一
|
nodeID := int64(1) // 确保唯一
|
||||||
node, _ := snowflake.NewNode(nodeID)
|
node, _ := snowflake.NewNode(nodeID)
|
||||||
|
|
@ -124,7 +102,7 @@ func TestGenerateID(t *testing.T) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
id := node.Generate().String()
|
id := node.Generate().String()
|
||||||
if _, exists := ids.LoadOrStore(id, true); exists {
|
if _, exists := ids.LoadOrStore(id, true); exists {
|
||||||
t.Errorf("duplicate ID generated")
|
t.Errorf("duplicate ID generated, id:%s", id)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue