|
4 | 4 | "strconv"
|
5 | 5 | "sync"
|
6 | 6 | "testing"
|
| 7 | + "time" |
7 | 8 |
|
8 | 9 | "github.com/stretchr/testify/require"
|
9 | 10 | )
|
@@ -43,27 +44,76 @@ import (
|
43 | 44 | // }
|
44 | 45 | // wg.Wait()
|
45 | 46 | // }
|
46 |
| -func TestConcurrentAddElement(t *testing.T) { |
| 47 | +func TestConcurrentAddElement10Goroutine100000Loop(t *testing.T) { |
| 48 | + var wg sync.WaitGroup |
| 49 | + |
47 | 50 | 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) { |
48 | 69 | 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++ { |
50 | 77 | wg.Add(1)
|
51 | 78 | go func(n int) {
|
52 | 79 | defer wg.Done()
|
53 |
| - for nthWork := 0; nthWork < 100; nthWork++ { |
| 80 | + for nthWork := 0; nthWork < numOfLoop; nthWork++ { |
54 | 81 | s.Add(strconv.Itoa(n) + ".testing-" + strconv.Itoa(nthWork))
|
55 | 82 | }
|
56 | 83 |
|
57 | 84 | }(nthGoroutine)
|
58 | 85 | }
|
59 | 86 | wg.Wait()
|
60 |
| - require.Equal(t, 1000, s.Len()) |
| 87 | + require.Equal(t, totalExpectElement, s.Len()) |
| 88 | +} |
61 | 89 |
|
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 |
66 | 92 |
|
| 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()) |
67 | 117 | }
|
68 | 118 |
|
69 | 119 | // func TestConcurrentRemoveElement(t *testing.T) {}
|
|
0 commit comments