Skip to content

Commit 1c9eec7

Browse files
1eedaegonDPS0340
andcommitted
test: Add testing multi goroutine and multi loop
- Test 10 goroutine and 100000 loop - Test 100000 goroutine and 10 loop - Test exclusive add and remove operations in different goroutines for 100000 loops Co-authored-by: DPS0340 <[email protected]>
1 parent 0b08e97 commit 1c9eec7

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

hashset_test.go

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strconv"
55
"sync"
66
"testing"
7+
"time"
78

89
"github.com/stretchr/testify/require"
910
)
@@ -43,27 +44,76 @@ import (
4344
// }
4445
// wg.Wait()
4546
// }
46-
func TestConcurrentAddElement(t *testing.T) {
47+
func TestConcurrentAddElement10Goroutine100000Loop(t *testing.T) {
48+
var wg sync.WaitGroup
49+
4750
s := New()
51+
numOfGoroutine := 10
52+
numOfLoop := 100000
53+
totalExpectElement := numOfGoroutine * numOfLoop
54+
55+
for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
56+
wg.Add(1)
57+
go func(n int) {
58+
defer wg.Done()
59+
for nthWork := 0; nthWork < numOfLoop; nthWork++ {
60+
s.Add(strconv.Itoa(n) + ".testing-" + strconv.Itoa(nthWork))
61+
}
62+
63+
}(nthGoroutine)
64+
}
65+
wg.Wait()
66+
require.Equal(t, totalExpectElement, s.Len())
67+
}
68+
func TestConcurrentAddElement100000Goroutine10Loop(t *testing.T) {
4869
var wg sync.WaitGroup
49-
for nthGoroutine := 0; nthGoroutine < 10; nthGoroutine++ {
70+
71+
s := New()
72+
numOfGoroutine := 100000
73+
numOfLoop := 10
74+
totalExpectElement := numOfGoroutine * numOfLoop
75+
76+
for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
5077
wg.Add(1)
5178
go func(n int) {
5279
defer wg.Done()
53-
for nthWork := 0; nthWork < 100; nthWork++ {
80+
for nthWork := 0; nthWork < numOfLoop; nthWork++ {
5481
s.Add(strconv.Itoa(n) + ".testing-" + strconv.Itoa(nthWork))
5582
}
5683

5784
}(nthGoroutine)
5885
}
5986
wg.Wait()
60-
require.Equal(t, 1000, s.Len())
87+
require.Equal(t, totalExpectElement, s.Len())
88+
}
6189

62-
// testGongurrency(100, 10, func(prefix string, num int) {
63-
// set.Remove("prefix" + strconv.Itoa(num))
64-
// })
65-
// require.Equal(t, set.Len(), 0)
90+
func TestConcurrentExclusiveLock100000Loop(t *testing.T) {
91+
var wg sync.WaitGroup
6692

93+
s := New()
94+
numOfGoroutine := 2
95+
numOfLoop := 100000
96+
for idx := 0; idx < numOfLoop; idx++ {
97+
key := strconv.Itoa(idx) + ".testing-" + strconv.Itoa(idx)
98+
for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
99+
wg.Add(1)
100+
go func(n int) {
101+
defer wg.Done()
102+
if n == 0 {
103+
s.Add(key)
104+
} else {
105+
for !s.Contains(key) {
106+
time.Sleep(time.Nanosecond)
107+
}
108+
s.Remove(key)
109+
}
110+
}(nthGoroutine)
111+
}
112+
wg.Wait()
113+
}
114+
115+
wg.Wait()
116+
require.Equal(t, 0, s.Len())
67117
}
68118

69119
// func TestConcurrentRemoveElement(t *testing.T) {}

makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ install:
33
go mod tidy
44

55
.PHONY: test
6-
test: install
7-
go test ./... -race
6+
test:
7+
go test ./... -race -v
88

0 commit comments

Comments
 (0)