From 1c9eec7f9f0776d97d5837e71773d94f7138e877 Mon Sep 17 00:00:00 2001 From: 1eedaegon Date: Fri, 29 Mar 2024 18:27:10 +0900 Subject: [PATCH] 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 --- hashset_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++------ makefile | 4 +-- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/hashset_test.go b/hashset_test.go index e9def86..daa9988 100644 --- a/hashset_test.go +++ b/hashset_test.go @@ -4,6 +4,7 @@ import ( "strconv" "sync" "testing" + "time" "github.com/stretchr/testify/require" ) @@ -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) {} diff --git a/makefile b/makefile index abd57d5..ff28b19 100644 --- a/makefile +++ b/makefile @@ -3,6 +3,6 @@ install: go mod tidy .PHONY: test -test: install - go test ./... -race +test: + go test ./... -race -v