-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
87 lines (72 loc) · 1.6 KB
/
main_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
package rabbitmqclient
import (
"flag"
"fmt"
"log"
"os"
"testing"
"time"
"github.com/fairyhunter13/amqpwrapper"
"github.com/streadway/amqp"
)
const (
uriDialTemplate = "amqp://guest:guest@%s:5672"
)
const (
normalTimeSleep = 2 * time.Second
)
type TestSetup struct {
amqpConnectionManager amqpwrapper.IConnectionManager
}
func newTestSetup() *TestSetup {
return &TestSetup{}
}
func (t *TestSetup) InitConnection(uriHost string) *TestSetup {
t.amqpConnectionManager = t.GetNewConnection(uriHost)
return t
}
func (t *TestSetup) NewContainer() (*Container, error) {
return NewContainer(t.GetConnection())
}
func (t *TestSetup) NewContainerAndConnection() (*Container, error) {
return NewContainer(t.GetNewConnection(""))
}
func (t *TestSetup) GetConnection() amqpwrapper.IConnectionManager {
return t.amqpConnectionManager
}
func (t *TestSetup) GetNewConnection(uriHost string) amqpwrapper.IConnectionManager {
if uriHost == "" {
uriHost = "localhost"
}
if envHost := os.Getenv("RABBITMQ_HOST"); envHost != "" {
uriHost = envHost
}
uriHost = fmt.Sprintf(uriDialTemplate, uriHost)
var (
err error
conn amqpwrapper.IConnectionManager
)
conn, err = amqpwrapper.NewManager(uriHost, amqp.Config{})
if err != nil {
log.Panicf("Error: %+v", err)
os.Exit(1)
}
return conn
}
var (
testSetup *TestSetup
)
func initTestSetup() {
testSetup = newTestSetup()
testSetup = testSetup.InitConnection("localhost")
}
func teardownTestSetup() {
testSetup.amqpConnectionManager.Close()
}
func TestMain(m *testing.M) {
flag.Parse()
initTestSetup()
code := m.Run()
teardownTestSetup()
os.Exit(code)
}