catface_backend_go/test/snowflake_test.go
2024-11-07 21:28:41 +08:00

59 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package test
import (
"catface/app/global/variable"
_ "catface/bootstrap"
"fmt"
"sync"
"testing"
)
// 雪花算法单元测试
func TestSnowFlake(t *testing.T) {
num := 3
// 并发 3万 测试,实际业务场景中,并发是不可能达到 3万 这个值的
var slice1 []int64
var vMuext sync.Mutex
var wg sync.WaitGroup
wg.Add(num)
for i := 1; i <= num; i++ {
go func() {
defer wg.Done()
//加锁操作主要是为了保证切片([]int64的并发安全
//我们本次测试的核心目的是雪花算法生成的ID必须是唯一的
vMuext.Lock()
slice1 = append(slice1, variable.SnowFlake.GetId())
fmt.Println(slice1)
vMuext.Unlock()
//fmt.Printf("%d\n", variable.SnowFlake.GetId())
}()
}
wg.Wait()
if lastLen := len(RemoveRepeatedElement(slice1)); lastLen == num {
t.Log("单元测试OK")
} else {
t.Errorf("雪花算法单元测试失败,并发 3万 生成的id经过去重之后小于预期个数去重后的个数%d\n", lastLen)
}
}
// 切片去重
func RemoveRepeatedElement(arr []int64) (newArr []int64) {
newArr = make([]int64, 0)
for i := 0; i < len(arr); i++ {
repeat := false
for j := i + 1; j < len(arr); j++ {
if arr[i] == arr[j] {
repeat = true
break
}
}
if !repeat {
newArr = append(newArr, arr[i])
}
}
return
}