-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompact_test.go
More file actions
54 lines (50 loc) · 1.04 KB
/
compact_test.go
File metadata and controls
54 lines (50 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package compact
import (
"bytes"
"fmt"
"math/rand"
"strconv"
"sync"
"testing"
"time"
)
func TestCompact(t *testing.T) {
var wg sync.WaitGroup
keys := []int{1, 10, 100, 1000, 10000}
for i := 0; i <= 50000; i++ {
key := keys[rand.Intn(len(keys))]
wg.Add(1)
go doCompact(key, &wg)
}
wg.Wait()
}
func doCompact(key int, wg *sync.WaitGroup) {
compactor := NewCompactor(1000, func(key int, payload []byte) {
fmt.Println(key, string(payload))
})
for i := 0; i < 100; i++ {
compactor.reqChan <- compactArgs{key: key, logs: []int{1}}
}
wg.Done()
}
func TestBuffer(t *testing.T) {
pool := sync.Pool{New: func() interface{} { return new(bytes.Buffer) }}
delay := make(chan []byte, 10)
// write
for i := 0; i < 10; i++ {
buf := pool.Get().(*bytes.Buffer)
buf.Reset()
buf.WriteString("hello" + strconv.Itoa(i))
payload := make([]byte, buf.Len())
copy(payload, buf.Bytes())
delay <- payload
pool.Put(buf)
}
for {
select {
case buf := <-delay:
time.Sleep(5000 * time.Millisecond)
fmt.Println(string(buf))
}
}
}