Welcome to mirror list, hosted at ThFree Co, Russian Federation.

random_test.go « praefect « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d24fe56c41f350b38f169575cc0e09a6f44342c6 (plain)
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
package praefect

import (
	"sync"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// TestNewLockedRandom tests that n is correctly passed down to the random
// implementation and that the return value is correctly passed back. To test
// that access to the random is actually synchronized, we launch 50 goroutines
// to call the random func concurrently to increment actual. If the calls to
// random are not correctly synchronized, actual might be not match expected
// at the end and the race detector should detect racy accesses even in the
// cases where the values match.
func TestNewLockedRandom(t *testing.T) {
	expected := 50
	actual := 0

	random := NewLockedRandom(mockRandom{
		intnFunc: func(n int) int {
			assert.Equal(t, 1, n)
			actual++
			return 2
		},
	})

	var wg sync.WaitGroup
	wg.Add(expected)

	for i := 0; i < expected; i++ {
		go func() {
			defer wg.Done()
			assert.Equal(t, 2, random.Intn(1))
		}()
	}

	wg.Wait()

	require.Equal(t, expected, actual)
}