You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
naza/pkg/unique/unique_test.go

46 lines
838 B
Go

// Copyright 2019, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package unique
import (
"sync"
"testing"
"github.com/q191201771/naza/pkg/assert"
)
func TestGenUniqueKey(t *testing.T) {
m := make(map[string]struct{})
var mutex sync.Mutex
var wg sync.WaitGroup
wg.Add(1000)
for i := 0; i < 1000; i++ {
go func(j int) {
var uk string
if j%2 == 0 {
uk = GenUniqueKey("hello")
} else {
uk = GenUniqueKey("world")
}
mutex.Lock()
m[uk] = struct{}{}
mutex.Unlock()
wg.Done()
}(i)
}
wg.Wait()
assert.Equal(t, 1000, len(m))
}
func BenchmarkGenUniqueKey(b *testing.B) {
for i := 0; i < b.N; i++ {
GenUniqueKey("benchmark")
}
}