-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
53 lines (47 loc) · 881 Bytes
/
pool_test.go
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
package pool
import (
"HelloGo/basic/body"
"context"
"log"
"strconv"
"testing"
"time"
)
var opt = &Option{
addr: "127.0.0.1:3000",
size: 5,
readTimeout: 30 * time.Second,
dialTimeout: 5 * time.Second,
keepAlive: 30 * time.Second,
}
func TestNewPool(t *testing.T) {
pool, err := NewPool(opt)
if err != nil {
t.Fatal(err)
}
for i := 0; i < 10; i++ {
go func(id int) {
if err := SendInPool(pool, "Uid-"+strconv.Itoa(id)); err != nil {
log.Print("Send in pool err: ", err)
}
}(i)
}
for {
time.Sleep(1)
}
}
func SendInPool(p *Pool, uid string) (err error) {
var c *Conn
if c, err = p.Get(); err != nil {
return
}
defer p.Put(c, err)
msg := &body.Message{Uid: uid, Val: "pixelpig!"}
rec, err := c.Send(context.Background(), msg)
if err != nil {
log.Print(err)
} else {
log.Print(uid, ", Msg: ", <-rec)
}
return
}