Skip to content

Commit 22df263

Browse files
authored
Merge pull request #2 from lxzan/dev
fix
2 parents 93e099a + 82b03f0 commit 22df263

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

event_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package event_emitter
33
import (
44
"context"
55
"fmt"
6+
"github.com/lxzan/event_emitter/internal/helper"
67
"github.com/stretchr/testify/assert"
78
"sync"
89
"testing"
@@ -102,6 +103,48 @@ func TestEventEmitter_Publish(t *testing.T) {
102103
assert.Equal(t, mapping[topic], i+1)
103104
}
104105
})
106+
107+
t.Run("batch3", func(t *testing.T) {
108+
var em = New(&Config{BucketNum: 1})
109+
var count = 1000
110+
var mapping1 = make(map[string]int)
111+
var mapping2 = make(map[string]int)
112+
var mu = &sync.Mutex{}
113+
var subjects = make(map[string]uint8)
114+
var wg = &sync.WaitGroup{}
115+
116+
for i := 0; i < count; i++ {
117+
var topics []string
118+
for j := 0; j < 100; j++ {
119+
topic := fmt.Sprintf("topic-%d", helper.Numeric.Intn(count))
120+
topics = append(topics, topic)
121+
}
122+
123+
topics = helper.Uniq(topics)
124+
wg.Add(len(topics))
125+
for j, _ := range topics {
126+
var topic = topics[j]
127+
mapping1[topic]++
128+
subjects[topic] = 1
129+
em.Subscribe(int64(i), topic, func(msg any) {
130+
mu.Lock()
131+
mapping2[topic]++
132+
mu.Unlock()
133+
wg.Done()
134+
})
135+
}
136+
}
137+
138+
for k, _ := range subjects {
139+
var err = em.Publish(context.Background(), k, "hello")
140+
assert.NoError(t, err)
141+
}
142+
143+
wg.Wait()
144+
for k, _ := range subjects {
145+
assert.Equal(t, mapping1[k], mapping2[k])
146+
}
147+
})
105148
}
106149

107150
func TestEventEmitter_UnSubscribe(t *testing.T) {

internal/helper/helper.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package helper
2+
3+
func Uniq[T comparable](arr []T) []T {
4+
var m = make(map[T]struct{}, len(arr))
5+
var list = make([]T, 0, len(arr))
6+
for _, item := range arr {
7+
m[item] = struct{}{}
8+
}
9+
for k, _ := range m {
10+
list = append(list, k)
11+
}
12+
return list
13+
}

internal/helper/helper_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package helper
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestRandomString(t *testing.T) {
9+
assert.Less(t, Numeric.Intn(10), 10)
10+
assert.Equal(t, len(AlphabetNumeric.Generate(16)), 16)
11+
Numeric.Uint32()
12+
Numeric.Uint64()
13+
}
14+
15+
func TestUniq(t *testing.T) {
16+
assert.ElementsMatch(t, Uniq([]int{1, 3, 5, 7, 7, 9}), []int{1, 3, 5, 7, 9})
17+
assert.ElementsMatch(t, Uniq([]string{"ming", "ming", "shi"}), []string{"ming", "shi"})
18+
}

internal/helper/random.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package helper
2+
3+
import (
4+
"math/rand"
5+
"sync"
6+
"time"
7+
)
8+
9+
type RandomString struct {
10+
mu sync.Mutex
11+
r *rand.Rand
12+
layout string
13+
}
14+
15+
var (
16+
AlphabetNumeric = &RandomString{
17+
layout: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
18+
r: rand.New(rand.NewSource(time.Now().UnixNano())),
19+
mu: sync.Mutex{},
20+
}
21+
22+
Numeric = &RandomString{
23+
layout: "0123456789",
24+
r: rand.New(rand.NewSource(time.Now().UnixNano())),
25+
mu: sync.Mutex{},
26+
}
27+
)
28+
29+
func (c *RandomString) Generate(n int) []byte {
30+
c.mu.Lock()
31+
var b = make([]byte, n, n)
32+
var length = len(c.layout)
33+
for i := 0; i < n; i++ {
34+
var idx = c.r.Intn(length)
35+
b[i] = c.layout[idx]
36+
}
37+
c.mu.Unlock()
38+
return b
39+
}
40+
41+
func (c *RandomString) Intn(n int) int {
42+
c.mu.Lock()
43+
x := c.r.Intn(n)
44+
c.mu.Unlock()
45+
return x
46+
}
47+
48+
func (c *RandomString) Uint32() uint32 {
49+
c.mu.Lock()
50+
x := c.r.Uint32()
51+
c.mu.Unlock()
52+
return x
53+
}
54+
55+
func (c *RandomString) Uint64() uint64 {
56+
c.mu.Lock()
57+
x := c.r.Uint64()
58+
c.mu.Unlock()
59+
return x
60+
}

0 commit comments

Comments
 (0)