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) } }