-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathrand_test.go
More file actions
78 lines (64 loc) · 1.3 KB
/
rand_test.go
File metadata and controls
78 lines (64 loc) · 1.3 KB
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: 2026 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 {
t.Helper()
s, err := generatePwd()
require.NoError(t, err)
return s
},
},
"Ufrag": {
gen: func(t *testing.T) string {
t.Helper()
s, err := generateUFrag()
require.NoError(t, err)
return s
},
},
}
const num = 100
const iteration = 100
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
for range iteration {
var wg sync.WaitGroup
var mu sync.Mutex
rands := make([]string, 0, num)
for range num {
wg.Add(1)
go func() {
r := testCase.gen(t)
mu.Lock()
rands = append(rands, r)
mu.Unlock()
wg.Done()
}()
}
wg.Wait()
require.Len(t, rands, num)
for i := range num {
for j := i + 1; j < num; j++ {
require.NotEqual(t, rands[i], rands[j])
}
}
}
})
}
}