From 40029dd2041d271b7e29ab934ce162c4a83e82fb Mon Sep 17 00:00:00 2001 From: askuy Date: Sat, 16 Nov 2024 22:28:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=B5=8B=E8=AF=95=E5=B9=B6?= =?UTF-8?q?=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esnowflake_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/esnowflake_test.go b/esnowflake_test.go index d0040d5..af60466 100644 --- a/esnowflake_test.go +++ b/esnowflake_test.go @@ -2,6 +2,7 @@ package esnowflake import ( "fmt" + "sync" "testing" "time" @@ -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()