-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlock_test.go
62 lines (53 loc) · 1.02 KB
/
mlock_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
package mlock_test
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
"github.com/kimbbakar/mlock"
)
var wg sync.WaitGroup
var count [10000]int
func TestMLock(t *testing.T) {
testCases := []struct {
input1 int
input2 int
output int
}{
{1, 10, 10},
{10, 100, 100},
{100, 1000, 1000},
{1000, 10000, 10000},
{1000, 100000, 100000},
}
d := time.Millisecond * 1
mlock.KeepClean(&d)
for _, tc := range testCases {
t.Run(fmt.Sprintf("Input_%d_%d", tc.input1, tc.input2), func(t *testing.T) {
testFunc(tc.input1, tc.input2)
for i := 0; i < tc.input1; i++ {
if count[i] != tc.output {
t.Errorf("Expected %d, got %d", tc.output, count[i])
}
}
})
}
}
func testFunc(lockCount, concurrentReq int) {
count = [10000]int{}
for j := 0; j < concurrentReq; j++ {
for i := 0; i < lockCount; i++ {
wg.Add(1)
go jobFunc(i, j)
}
}
wg.Wait()
}
func jobFunc(i, j int) {
time.Sleep(time.Second * time.Duration(rand.Intn(2)+1))
mlock.Lock(i)
defer mlock.Unlock(i)
count[i] += 1
wg.Done()
}