Skip to content

Commit

Permalink
test: Add testing multi goroutine and multi loop
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
1eedaegon and DPS0340 committed Mar 29, 2024
1 parent 0b08e97 commit 1c9eec7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
66 changes: 58 additions & 8 deletions hashset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -43,27 +44,76 @@ import (
// }
// wg.Wait()
// }
func TestConcurrentAddElement(t *testing.T) {
func TestConcurrentAddElement10Goroutine100000Loop(t *testing.T) {
var wg sync.WaitGroup

s := New()
numOfGoroutine := 10
numOfLoop := 100000
totalExpectElement := numOfGoroutine * numOfLoop

for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
for nthWork := 0; nthWork < numOfLoop; nthWork++ {
s.Add(strconv.Itoa(n) + ".testing-" + strconv.Itoa(nthWork))
}

}(nthGoroutine)
}
wg.Wait()
require.Equal(t, totalExpectElement, s.Len())
}
func TestConcurrentAddElement100000Goroutine10Loop(t *testing.T) {
var wg sync.WaitGroup
for nthGoroutine := 0; nthGoroutine < 10; nthGoroutine++ {

s := New()
numOfGoroutine := 100000
numOfLoop := 10
totalExpectElement := numOfGoroutine * numOfLoop

for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
for nthWork := 0; nthWork < 100; nthWork++ {
for nthWork := 0; nthWork < numOfLoop; nthWork++ {
s.Add(strconv.Itoa(n) + ".testing-" + strconv.Itoa(nthWork))
}

}(nthGoroutine)
}
wg.Wait()
require.Equal(t, 1000, s.Len())
require.Equal(t, totalExpectElement, s.Len())
}

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

s := New()
numOfGoroutine := 2
numOfLoop := 100000
for idx := 0; idx < numOfLoop; idx++ {
key := strconv.Itoa(idx) + ".testing-" + strconv.Itoa(idx)
for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
if n == 0 {
s.Add(key)
} else {
for !s.Contains(key) {
time.Sleep(time.Nanosecond)
}
s.Remove(key)
}
}(nthGoroutine)
}
wg.Wait()
}

wg.Wait()
require.Equal(t, 0, s.Len())
}

// func TestConcurrentRemoveElement(t *testing.T) {}
Expand Down
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ install:
go mod tidy

.PHONY: test
test: install
go test ./... -race
test:
go test ./... -race -v

0 comments on commit 1c9eec7

Please sign in to comment.