-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_counters_test.go
180 lines (173 loc) · 4.94 KB
/
server_counters_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
package msmtpd
import (
"bytes"
"context"
"io"
"net/http"
"net/smtp"
"sync"
"testing"
"time"
)
func TestCounters(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
srv := &Server{
Logger: &TestLogger{Suite: t},
CloseHandlers: []CloseHandler{
func(tr *Transaction) error {
wg.Done()
return nil
},
},
}
addr, closer := RunTestServerWithoutTLS(t, srv)
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
err = c.Hello("localhost")
if err != nil {
t.Errorf("%s : while making helo", err)
}
t.Logf("Active transactions - %v", srv.GetActiveTransactionsCount())
if srv.GetActiveTransactionsCount() != 1 {
t.Errorf("active transactions - wrong counter %v", srv.GetActiveTransactionsCount())
}
err = c.Quit()
if err != nil {
t.Errorf("%s : while closing", err)
}
wg.Wait()
t.Logf("Transaction is closed")
if srv.GetActiveTransactionsCount() != 0 {
t.Errorf("%v active transactions found after close?", srv.GetActiveTransactionsCount())
}
t.Logf("Bytes read - %v", srv.GetBytesRead())
if srv.GetBytesRead() == 0 {
t.Errorf("bytes read counter not works")
}
t.Logf("Bytes wwritten - %v", srv.GetBytesWritten())
if srv.GetBytesWritten() == 0 {
t.Errorf("bytes written counter not works")
}
t.Logf("Transactions count - %v", srv.GetTransactionsCount())
if srv.GetTransactionsCount() != 1 {
t.Errorf("%v finished transaction not counted", srv.GetTransactionsCount())
}
t.Logf("Reseting counters")
srv.ResetCounters()
t.Logf("Bytes read - %v", srv.GetBytesRead())
t.Logf("Bytes wwritten - %v", srv.GetBytesWritten())
t.Logf("Active transactions count - %v", srv.GetActiveTransactionsCount())
if srv.GetActiveTransactionsCount() != 0 {
t.Errorf("active transactions found")
}
t.Logf("Transactions count - %v", srv.GetTransactionsCount())
if srv.GetTransactionsCount() != 0 {
t.Errorf("transaction counter is not reset")
}
if srv.lastTransactionStartedAt.IsZero() {
t.Errorf("transaction time is not set")
}
if time.Since(srv.lastTransactionStartedAt) > 3*time.Second {
t.Errorf("last transaction time is too old")
}
}
func TestCountersHttpExporter(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
srv := &Server{
Context: context.TODO(),
Logger: &TestLogger{Suite: t},
CloseHandlers: []CloseHandler{
func(tr *Transaction) error {
wg.Done()
return nil
},
},
}
go func() {
sErr := srv.StartPrometheusScrapperEndpoint("127.0.0.1:5031", "/metrics")
if sErr != nil {
t.Errorf("%s : while starting metrics HTTP server", sErr)
}
}()
addr, closer := RunTestServerWithoutTLS(t, srv)
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
err = c.Hello("localhost")
if err != nil {
t.Errorf("%s : while making helo", err)
}
t.Logf("Active transactions - %v", srv.GetActiveTransactionsCount())
if srv.GetActiveTransactionsCount() != 1 {
t.Errorf("active transactions - wrong counter %v", srv.GetActiveTransactionsCount())
}
err = c.Quit()
if err != nil {
t.Errorf("%s : while closing", err)
}
wg.Wait()
t.Logf("Transaction is closed")
resp1, err := http.Get("http://127.0.0.1:5031/metrics")
if err != nil {
t.Errorf("%s : while getting metrics endpoint", err)
}
if resp1.StatusCode != http.StatusOK {
t.Errorf("wrong status - %s", resp1.Status)
}
if resp1.Body != nil {
defer resp1.Body.Close()
}
data, err := io.ReadAll(resp1.Body)
if err != nil {
t.Errorf("%s : while reading body", err)
}
t.Logf("Body: %s", string(data))
if !bytes.Contains(data, []byte("bytes_read{hostname=\"localhost.localdomain\"} 22")) {
t.Errorf("bytes read wrong")
}
if !bytes.Contains(data, []byte("bytes_written{hostname=\"localhost.localdomain\"} 199")) {
t.Errorf("bytes read wrong")
}
if !bytes.Contains(data, []byte("active_transactions_count{hostname=\"localhost.localdomain\"} 0")) {
t.Errorf("bytes read wrong")
}
if !bytes.Contains(data, []byte("all_transactions_count{hostname=\"localhost.localdomain\"} 1")) {
t.Errorf("bytes read wrong")
}
t.Logf("Reseting counters")
srv.ResetCounters()
resp2, err := http.Get("http://127.0.0.1:5031/metrics")
if err != nil {
t.Errorf("%s : while getting metrics endpoint", err)
}
if resp2.StatusCode != http.StatusOK {
t.Errorf("wrong status - %s", resp2.Status)
}
if resp2.Body != nil {
defer resp2.Body.Close()
}
data, err = io.ReadAll(resp2.Body)
if err != nil {
t.Errorf("%s : while reading body", err)
}
if !bytes.Contains(data, []byte("bytes_read{hostname=\"localhost.localdomain\"} 0")) {
t.Errorf("bytes read wrong")
}
if !bytes.Contains(data, []byte("bytes_written{hostname=\"localhost.localdomain\"} 0")) {
t.Errorf("bytes read wrong")
}
if !bytes.Contains(data, []byte("active_transactions_count{hostname=\"localhost.localdomain\"} 0")) {
t.Errorf("bytes read wrong")
}
if !bytes.Contains(data, []byte("all_transactions_count{hostname=\"localhost.localdomain\"} 0")) {
t.Errorf("bytes read wrong")
}
t.Logf("Body: %s", string(data))
}