-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuite_test.go
188 lines (156 loc) · 4.4 KB
/
suite_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* enqueuestomp
*
* MIT License
*
* Copyright (c) 2020 Globo.com
*/
package enqueuestomp_test
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
golog "log"
"net/http"
"os"
"strconv"
"strings"
"syscall"
"testing"
"time"
"github.com/globocom/enqueuestomp/v2"
check "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) {
check.TestingT(t)
}
type EnqueueStompSuite struct {
c *check.C
j *jolokia
}
var _ = check.Suite(&EnqueueStompSuite{})
func (s *EnqueueStompSuite) SetUpSuite(c *check.C) {
s.c = c
s.j = &jolokia{
addr: "http://localhost:8161",
username: "admin",
passwd: "admin",
c: c,
}
}
func (s *EnqueueStompSuite) SetUpTest(c *check.C) {
s.j.Delete(enqueuestomp.DestinationTypeQueue, queueName)
s.j.Delete(enqueuestomp.DestinationTypeTopic, topicName)
}
func (s *EnqueueStompSuite) TearDownTest(c *check.C) {
s.deleteFile(queueWriteOutputPath)
s.deleteFile(topicWriteOutputPath)
time.Sleep(20 * time.Millisecond)
}
func (s *EnqueueStompSuite) waitQueueSize(enqueue enqueuestomp.EnqueueStomp) {
for enqueue.QueueSize() > 0 {
time.Sleep(300 * time.Millisecond)
}
time.Sleep(100 * time.Millisecond)
}
func (s *EnqueueStompSuite) deleteFile(filePath string) {
_ = os.Remove(filePath)
}
func (s *EnqueueStompSuite) countFileLines(filePath string) int {
file, err := os.Open(filePath)
s.c.Assert(err, check.IsNil)
fileScanner := bufio.NewScanner(file)
lineCount := 0
for fileScanner.Scan() {
lineCount++
}
return lineCount
}
// https://gist.github.com/yashpatil/de7437522bfccfeee4cb#retrieve-all-attributes-2
type jolokia struct {
addr string
username string
passwd string
c *check.C
}
func (j *jolokia) StatQueue(queueName string, attribute string) string {
return j._stat(enqueuestomp.DestinationTypeQueue, queueName, attribute)
}
func (j *jolokia) StatTopic(topicName string, attribute string) string {
return j._stat(enqueuestomp.DestinationTypeTopic, topicName, attribute)
}
func (j *jolokia) _stat(destinationType string, destinationName string, attribute string) string {
url := j.makeURLStats(destinationType, destinationName, attribute)
// println("url :%s", url)
body := j.doRequest(url)
var data map[string]interface{}
if err := json.Unmarshal(body, &data); err != nil {
j.c.Assert(err, check.IsNil)
}
val := data["value"]
switch val.(type) { // nolint
case float64:
return strconv.Itoa(int(val.(float64)))
case int:
return strconv.Itoa(val.(int))
default:
return fmt.Sprintf("%v", val)
}
}
func (j *jolokia) Delete(destinationType string, destinationName string) {
url := j.makeURLDelete(destinationType, destinationName)
_ = j.doRequest(url)
}
func (j *jolokia) makeURLDelete(destinationType string, destinationName string) string {
path := fmt.Sprintf(
"/exec/org.apache.activemq:type=Broker,brokerName=localhost/remove%s(java.lang.String)/%s",
strings.Title(destinationType), destinationName,
)
return j.makeURLJolokia(path)
}
func (j *jolokia) makeURLStats(destinationType string, destinationName string, attribute string) string {
path := fmt.Sprintf(
"/read/org.apache.activemq:type=Broker,brokerName=localhost,destinationType=%s,destinationName=%s/%s",
strings.Title(destinationType), destinationName, attribute,
)
return j.makeURLJolokia(path)
}
func (j *jolokia) makeURLJolokia(path string) string {
return fmt.Sprintf("%s/api/jolokia%s", j.addr, path)
}
func (j *jolokia) doRequest(url string) []byte {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil) // nolint:noctx
j.c.Assert(err, check.IsNil)
req.SetBasicAuth(j.username, j.passwd)
resp, err := client.Do(req)
j.c.Assert(err, check.IsNil)
defer resp.Body.Close()
if resp.StatusCode > 399 {
j.c.Errorf("jokia status code %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
j.c.Assert(err, check.IsNil)
return body
}
// nolint
// increase resources limitation.
func changeMaxULimit(ulimit int) error {
var uLimit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &uLimit); err != nil {
golog.Printf("error on get ulimit: %s", err)
return err
}
if uLimit.Cur != uLimit.Max {
golog.Printf("Current Ulimit value %d", uLimit.Cur)
uLimit.Cur = uint64(ulimit)
golog.Printf("changed Ulimit to %d", uLimit.Cur)
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &uLimit); err != nil {
golog.Printf("error on set ulimit : %s", err)
return err
}
}
return nil
}