Skip to content

Commit

Permalink
加入测试并发
Browse files Browse the repository at this point in the history
  • Loading branch information
askuy committed Nov 16, 2024
1 parent c34ba1b commit 40029dd
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions esnowflake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package esnowflake

import (
"fmt"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -58,6 +59,28 @@ func TestGenerateByRandom_HandlesPoolRefill(t *testing.T) {
}
}

func TestGenerateByRandom_ConcurrencyNotEqual(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
testLength := 1000000
output := sync.Map{}
// 测试并发生成随机数,是否重复
wg := sync.WaitGroup{}
wg.Add(testLength)
for i := 0; i < testLength; i++ {
go func() {
id := config.GenerateByRandom()
_, flag := output.Load(id)
if flag {
t.Errorf("Expected unique IDs, but got %s and %s", id, id)
}
output.Store(id, struct{}{})
wg.Done()
}()

}
wg.Wait()
}

func TestGenerateByRandom_GetTime(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
encode := config.GenerateByRandom()
Expand Down

0 comments on commit 40029dd

Please sign in to comment.