-
Notifications
You must be signed in to change notification settings - Fork 163
/
rand_test.go
78 lines (67 loc) · 1.44 KB
/
rand_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package ice
import (
"sync"
"testing"
"github.com/stretchr/testify/require"
)
func TestRandomGeneratorCollision(t *testing.T) {
candidateIDGen := newCandidateIDGenerator()
testCases := map[string]struct {
gen func(t *testing.T) string
}{
"CandidateID": {
gen: func(*testing.T) string {
return candidateIDGen.Generate()
},
},
"PWD": {
gen: func(t *testing.T) string {
s, err := generatePwd()
require.NoError(t, err)
return s
},
},
"Ufrag": {
gen: func(t *testing.T) string {
s, err := generateUFrag()
require.NoError(t, err)
return s
},
},
}
const N = 100
const iteration = 100
for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
for iter := 0; iter < iteration; iter++ {
var wg sync.WaitGroup
var mu sync.Mutex
rands := make([]string, 0, N)
for i := 0; i < N; i++ {
wg.Add(1)
go func() {
r := testCase.gen(t)
mu.Lock()
rands = append(rands, r)
mu.Unlock()
wg.Done()
}()
}
wg.Wait()
if len(rands) != N {
t.Fatal("Failed to generate randoms")
}
for i := 0; i < N; i++ {
for j := i + 1; j < N; j++ {
if rands[i] == rands[j] {
t.Fatalf("generateRandString caused collision: %s == %s", rands[i], rands[j])
}
}
}
}
})
}
}