-
Notifications
You must be signed in to change notification settings - Fork 17
/
pool_test.go
98 lines (74 loc) · 2.25 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package rabbitroutine
import (
"context"
"testing"
"time"
"github.com/pkg/errors"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/stretchr/testify/assert"
)
func TestPoolChannelWithConfirmRespectContext(t *testing.T) {
defer time.AfterFunc(1*time.Second, func() { panic("Pool.ChannelWithConfirm don't respect context") }).Stop()
conn := NewConnector(Config{})
pool := NewPool(conn)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := pool.ChannelWithConfirm(ctx)
assert.Error(t, err)
assert.Equal(t, errors.Cause(err), ctx.Err())
}
func TestPoolEmptyOnStart(t *testing.T) {
conn := NewConnector(Config{})
pool := NewPool(conn)
assert.Empty(t, pool.set)
}
func TestPoolReleaseSuccess(t *testing.T) {
conn := NewConnector(Config{})
pool := NewPool(conn)
pool.Release(ChannelKeeper{})
assert.Len(t, pool.set, 1)
}
func TestPoolAcquiringFromNonEmpty(t *testing.T) {
conn := NewConnector(Config{})
pool := NewPool(conn)
pool.Release(ChannelKeeper{
errorCh: make(chan *amqp.Error, 1),
confirmCh: make(chan amqp.Confirmation, 1),
})
assert.Len(t, pool.set, 1)
k, err := pool.ChannelWithConfirm(context.Background())
assert.NoError(t, err)
assert.Empty(t, pool.set)
assert.NotNil(t, k.errorCh)
assert.NotNil(t, k.confirmCh)
}
func TestLightningPoolChannelRespectContext(t *testing.T) {
defer time.AfterFunc(1*time.Second, func() { panic("LightningPool.Channel don't respect context") }).Stop()
conn := NewConnector(Config{})
pool := NewLightningPool(conn)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := pool.Channel(ctx)
assert.Error(t, err)
assert.Equal(t, errors.Cause(err), ctx.Err())
}
func TestLightningPoolEmptyOnStart(t *testing.T) {
conn := NewConnector(Config{})
pool := NewLightningPool(conn)
assert.Empty(t, pool.set)
}
func TestLightningPoolReleaseSuccess(t *testing.T) {
conn := NewConnector(Config{})
pool := NewLightningPool(conn)
pool.Release(&amqp.Channel{})
assert.Len(t, pool.set, 1)
}
func TestLightningPoolAcquiringFromNonEmpty(t *testing.T) {
conn := NewConnector(Config{})
pool := NewLightningPool(conn)
pool.Release(&amqp.Channel{})
assert.Len(t, pool.set, 1)
_, err := pool.Channel(context.Background())
assert.NoError(t, err)
assert.Empty(t, pool.set)
}