From 9ff5f6b125489886b2c5f0943742ec74b77d68f3 Mon Sep 17 00:00:00 2001 From: ccpwcn Date: Mon, 17 Jun 2024 15:24:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=9B=AA=E8=8A=B1?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E4=B8=AD?= =?UTF-8?q?=E7=9A=84map=E5=B9=B6=E5=8F=91=E5=86=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- snowflake_generic_test.go | 12 ++++++------ snowflake_test.go | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/snowflake_generic_test.go b/snowflake_generic_test.go index 3e2cc1f..871ff29 100644 --- a/snowflake_generic_test.go +++ b/snowflake_generic_test.go @@ -47,17 +47,17 @@ func Test_GenericSnowflake_Concurrent_Id(t *testing.T) { } else { const count = 5 const machine = 3 - var buffer = make(map[int64]int8, count*3) + var buffer sync.Map var wg sync.WaitGroup for i := 1; i <= machine; i++ { wg.Add(1) go func(i int64) { for j := 0; j < count; j++ { id := GetSnowflakeId[int64]() - if _, ok := buffer[id]; ok { + if _, ok := buffer.Load(id); ok { t.Errorf("ID %+v 重复了", id) } else { - buffer[id] = 1 + buffer.Store(id, 1) t.Logf("ID %+v\n", id) } } @@ -74,17 +74,17 @@ func Test_GenericSnowflake_Concurrent_Id_String(t *testing.T) { } else { const count = 5 const machine = 3 - var buffer = make(map[string]int8, count*3) + var buffer sync.Map var wg sync.WaitGroup for i := 1; i <= machine; i++ { wg.Add(1) go func(i string) { for j := 0; j < count; j++ { id := GetSnowflakeId[string]() - if _, ok := buffer[id]; ok { + if _, ok := buffer.Load(id); ok { t.Errorf("ID %+v 重复了", id) } else { - buffer[id] = 1 + buffer.Store(id, 1) t.Logf("ID %+v\n", id) } } diff --git a/snowflake_test.go b/snowflake_test.go index 641e1bd..95fb5cb 100644 --- a/snowflake_test.go +++ b/snowflake_test.go @@ -29,17 +29,17 @@ func TestSnowflake_Concurrent_Id(t *testing.T) { } else { const count = 5 const machine = 3 - var buffer = make(map[int64]int8, count*3) + var buffer sync.Map var wg sync.WaitGroup for i := 1; i <= machine; i++ { wg.Add(1) go func(i int64) { for j := 0; j < count; j++ { id := SnowflakeId() - if _, ok := buffer[id]; ok { + if _, ok := buffer.Load(id); ok { t.Errorf("ID %+v 重复了", id) } else { - buffer[id] = 1 + buffer.Store(id, 1) t.Logf("ID %+v\n", id) } }