-
Notifications
You must be signed in to change notification settings - Fork 8
/
logging_test.go
144 lines (120 loc) · 3.23 KB
/
logging_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
package amqprpc
import (
"io"
"log"
"testing"
"time"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/stretchr/testify/assert"
)
func TestServerLogging(t *testing.T) {
reader, writer := io.Pipe()
go func() {
logger := log.New(writer, "TEST", log.LstdFlags)
s := NewServer(testURL)
s.WithDebugLogger(logger.Printf)
s.WithErrorLogger(logger.Printf)
stop := startAndWait(s)
stop()
assert.NoError(t, writer.Close())
}()
buf, err := io.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
assert.NotEqual(t, "", string(buf), "buffer contains logs")
assert.Contains(t, string(buf), "TEST", "logs are prefixed with TEST")
}
func TestClientLogging(t *testing.T) {
reader, writer := io.Pipe()
go func() {
logger := log.New(writer, "TEST", log.LstdFlags)
c := NewClient("amqp://guest:guest@localhost:5672/")
c.WithDebugLogger(logger.Printf)
c.WithErrorLogger(logger.Printf)
_, err := c.Send(NewRequest().WithRoutingKey("foobar").WithTimeout(time.Millisecond))
c.Stop()
assert.Error(t, err, "did not get expected error")
assert.Contains(t, err.Error(), "timed out")
_ = writer.Close()
}()
buf, err := io.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
assert.NotEqual(t, "", string(buf), "buffer contains logs")
assert.Contains(t, string(buf), "TEST", "logs are prefixed with TEST")
}
func Test_stringifyForLog(t *testing.T) {
headers := amqp.Table{
"foo": "bar",
"nested": amqp.Table{
"baz": 13,
},
}
delivery := amqp.Delivery{
CorrelationId: "coorelation1",
Exchange: "exchange",
RoutingKey: "routing_key",
Type: "type",
UserId: "jane",
Headers: headers,
}
request := Request{
Exchange: "exchange",
RoutingKey: "routing_key",
Publishing: amqp.Publishing{
AppId: "amqprpc",
CorrelationId: "coorelation1",
UserId: "jane",
Headers: headers,
},
}
ret := amqp.Return{
AppId: "amqprpc",
CorrelationId: "coorelation1",
Exchange: "exchange",
RoutingKey: "routing_key",
UserId: "jane",
ReplyText: "NO_ROUTE",
ReplyCode: 412,
Headers: headers,
}
tests := []struct {
name string
item interface{}
want string
}{
{
name: "delivery",
item: delivery,
want: "[Exchange=exchange, RoutingKey=routing_key, Type=type, CorrelationId=coorelation1, UserId=jane, Headers=[foo=bar, nested=[baz=13]]]",
},
{
name: "request",
item: request,
want: "[Exchange=exchange, RoutingKey=routing_key, Publishing=[CorrelationID=coorelation1, AppId=amqprpc, UserId=jane, Headers=[foo=bar, nested=[baz=13]]]]",
},
{
name: "return",
item: ret,
want: "[ReplyCode=412, ReplyText=NO_ROUTE, Exchange=exchange, RoutingKey=routing_key, CorrelationID=coorelation1, AppId=amqprpc, UserId=jane, Headers=[foo=bar, nested=[baz=13]]]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got string
switch v := tt.item.(type) {
case amqp.Delivery:
got = stringifyDeliveryForLog(&v)
assert.Equal(t, "[nil]", stringifyDeliveryForLog(nil))
case amqp.Return:
got = stringifyReturnForLog(v)
case Request:
got = stringifyRequestForLog(&v)
assert.Equal(t, "[nil]", stringifyRequestForLog(nil))
}
assert.Equal(t, tt.want, got)
})
}
}