Debian Codegraph @

go/pkg/amqp/message_test.go

raw
1 /*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19
20 package amqp
21
22 import (
23 "reflect"
24 "testing"
25 "time"
26
27 "github.com/apache/qpid-proton/go/pkg/internal/test"
28 )
29
30 func roundTrip(m Message) error {
31 buffer, err := m.Encode(nil)
32 if err != nil {
33 return err
34 }
35 m2, err := DecodeMessage(buffer)
36 if err != nil {
37 return err
38 }
39 return test.Differ(m, m2)
40 }
41
42 func TestDefaultMessage(t *testing.T) {
43 m := NewMessage()
44 if err := roundTrip(m); err != nil {
45 t.Error(err)
46 }
47 mv := reflect.ValueOf(m)
48 // Check defaults
49 for _, x := range []struct {
50 method string
51 want interface{}
52 }{
53 {"Inferred", false},
54 {"Durable", false},
55 {"Priority", uint8(4)},
56 {"TTL", time.Duration(0)},
57 {"UserId", ""},
58 {"Address", ""},
59 {"Subject", ""},
60 {"ReplyTo", ""},
61 {"ContentType", ""},
62 {"ContentEncoding", ""},
63 {"GroupId", ""},
64 {"GroupSequence", int32(0)},
65 {"ReplyToGroupId", ""},
66 {"MessageId", nil},
67 {"CorrelationId", nil},
68 {"DeliveryAnnotations", map[AnnotationKey]interface{}{}},
69 {"MessageAnnotations", map[AnnotationKey]interface{}{}},
70 {"ApplicationProperties", map[string]interface{}{}},
71
72 // Deprecated
73 {"Instructions", map[string]interface{}(nil)},
74 {"Annotations", map[string]interface{}(nil)},
75 {"Properties", map[string]interface{}{}},
76 {"Body", nil},
77 } {
78 ret := mv.MethodByName(x.method).Call(nil)
79 if err := test.Differ(x.want, ret[0].Interface()); err != nil {
80 t.Errorf("%s: %s", x.method, err)
81 }
82 }
83 if err := test.Differ("Message{}", m.String()); err != nil {
84 t.Error(err)
85 }
86 }
87
88 func TestMessageString(t *testing.T) {
89 m := NewMessageWith("hello")
90 m.SetInferred(false)
91 m.SetUserId("user")
92 m.SetDeliveryAnnotations(map[AnnotationKey]interface{}{AnnotationKeySymbol("instructions"): "foo"})
93 m.SetMessageAnnotations(map[AnnotationKey]interface{}{AnnotationKeySymbol("annotations"): "bar"})
94 m.SetApplicationProperties(map[string]interface{}{"int": int32(32)})
95 if err := roundTrip(m); err != nil {
96 t.Error(err)
97 }
98 msgstr := "Message{user-id: user, delivery-annotations: map[instructions:foo], message-annotations: map[annotations:bar], application-properties: map[int:32], body: hello}"
99 if err := test.Differ(msgstr, m.String()); err != nil {
100 t.Error(err)
101 }
102 }
103
104 // Set all message properties
105 func setMessageProperties(m Message) Message {
106 m.SetInferred(false)
107 m.SetDurable(true)
108 m.SetPriority(42)
109 m.SetTTL(0)
110 m.SetUserId("user")
111 m.SetAddress("address")
112 m.SetSubject("subject")
113 m.SetReplyTo("replyto")
114 m.SetContentType("content")
115 m.SetContentEncoding("encoding")
116 m.SetGroupId("group")
117 m.SetGroupSequence(42)
118 m.SetReplyToGroupId("replytogroup")
119 m.SetMessageId("id")
120 m.SetCorrelationId("correlation")
121 m.SetDeliveryAnnotations(map[AnnotationKey]interface{}{AnnotationKeySymbol("instructions"): "foo"})
122 m.SetMessageAnnotations(map[AnnotationKey]interface{}{AnnotationKeySymbol("annotations"): "bar"})
123 m.SetApplicationProperties(map[string]interface{}{"int": int32(32), "bool": true})
124 return m
125 }
126
127 func TestMessageRoundTrip(t *testing.T) {
128 m1 := NewMessage()
129 setMessageProperties(m1)
130 m1.Marshal("hello")
131
132 buffer, err := m1.Encode(nil)
133 if err != nil {
134 t.Fatal(err)
135 }
136 m, err := DecodeMessage(buffer)
137 if err != nil {
138 t.Fatal(err)
139 }
140 if err = test.Differ(m1, m); err != nil {
141 t.Error(err)
142 }
143
144 for _, data := range [][]interface{}{
145 {m.Inferred(), false},
146 {m.Durable(), true},
147 {m.Priority(), uint8(42)},
148 {m.TTL(), time.Duration(0)},
149 {m.UserId(), "user"},
150 {m.Address(), "address"},
151 {m.Subject(), "subject"},
152 {m.ReplyTo(), "replyto"},
153 {m.ContentType(), "content"},
154 {m.ContentEncoding(), "encoding"},
155 {m.GroupId(), "group"},
156 {m.GroupSequence(), int32(42)},
157 {m.ReplyToGroupId(), "replytogroup"},
158 {m.MessageId(), "id"},
159 {m.CorrelationId(), "correlation"},
160
161 {m.DeliveryAnnotations(), map[AnnotationKey]interface{}{AnnotationKeySymbol("instructions"): "foo"}},
162 {m.MessageAnnotations(), map[AnnotationKey]interface{}{AnnotationKeySymbol("annotations"): "bar"}},
163 {m.ApplicationProperties(), map[string]interface{}{"int": int32(32), "bool": true}},
164 {m.Body(), "hello"},
165
166 // Deprecated
167 {m.Instructions(), map[string]interface{}{"instructions": "foo"}},
168 {m.Annotations(), map[string]interface{}{"annotations": "bar"}},
169 } {
170 if err := test.Differ(data[0], data[1]); err != nil {
171 t.Error(err)
172 }
173 }
174 if err := roundTrip(m); err != nil {
175 t.Error(err)
176 }
177 }
178
179 func TestDeprecated(t *testing.T) {
180 m := NewMessage()
181
182 m.SetInstructions(map[string]interface{}{"instructions": "foo"})
183 m.SetAnnotations(map[string]interface{}{"annotations": "bar"})
184 m.SetProperties(map[string]interface{}{"int": int32(32), "bool": true})
185
186 for _, data := range [][]interface{}{
187 {m.DeliveryAnnotations(), map[AnnotationKey]interface{}{AnnotationKeySymbol("instructions"): "foo"}},
188 {m.MessageAnnotations(), map[AnnotationKey]interface{}{AnnotationKeySymbol("annotations"): "bar"}},
189 {m.ApplicationProperties(), map[string]interface{}{"int": int32(32), "bool": true}},
190
191 {m.Instructions(), map[string]interface{}{"instructions": "foo"}},
192 {m.Annotations(), map[string]interface{}{"annotations": "bar"}},
193 {m.Properties(), map[string]interface{}{"int": int32(32), "bool": true}},
194 } {
195 if err := test.Differ(data[0], data[1]); err != nil {
196 t.Error(err)
197 }
198 }
199 if err := roundTrip(m); err != nil {
200 t.Error(err)
201 }
202 }
203
204 func TestMessageBodyTypes(t *testing.T) {
205 var s string
206 var body interface{}
207 var i int64
208
209 m := NewMessageWith(int64(42))
210 m.Unmarshal(&body)
211 m.Unmarshal(&i)
212 if err := test.Differ(body.(int64), int64(42)); err != nil {
213 t.Error(err)
214 }
215 if err := test.Differ(i, int64(42)); err != nil {
216 t.Error(err)
217 }
218
219 m = NewMessageWith("hello")
220 m.Unmarshal(&s)
221 m.Unmarshal(&body)
222 if err := test.Differ(s, "hello"); err != nil {
223 t.Error(err)
224 }
225 if err := test.Differ(body.(string), "hello"); err != nil {
226 t.Error(err)
227 }
228 if err := roundTrip(m); err != nil {
229 t.Error(err)
230 }
231
232 m = NewMessageWith(Binary("bin"))
233 m.Unmarshal(&s)
234 m.Unmarshal(&body)
235 if err := test.Differ(body.(Binary), Binary("bin")); err != nil {
236 t.Error(err)
237 }
238 if err := test.Differ(s, "bin"); err != nil {
239 t.Error(err)
240 }
241 if err := roundTrip(m); err != nil {
242 t.Error(err)
243 }
244
245 // TODO aconway 2015-09-08: array etc.
246 }
247
248 // Benchmarks assign to package-scope variables to prevent being optimized out.
249 var bmM Message
250 var bmBuf []byte
251
252 func BenchmarkNewMessageEmpty(b *testing.B) {
253 for n := 0; n < b.N; n++ {
254 bmM = NewMessage()
255 }
256 }
257
258 func BenchmarkNewMessageString(b *testing.B) {
259 for n := 0; n < b.N; n++ {
260 bmM = NewMessageWith("hello")
261 }
262 }
263
264 func BenchmarkNewMessageAll(b *testing.B) {
265 for n := 0; n < b.N; n++ {
266 bmM = setMessageProperties(NewMessageWith("hello"))
267 }
268 }
269
270 func BenchmarkEncode(b *testing.B) {
271 m := setMessageProperties(NewMessageWith("hello"))
272 b.ResetTimer()
273 for n := 0; n < b.N; n++ {
274 buf, err := m.Encode(nil)
275 if err != nil {
276 b.Fatal(err)
277 }
278 bmBuf = buf
279 }
280 }
281
282 func BenchmarkDecode(b *testing.B) {
283 var buf []byte
284 buf, err := setMessageProperties(NewMessageWith("hello")).Encode(buf)
285 if err != nil {
286 b.Fatal(err)
287 }
288 m := NewMessage()
289 b.ResetTimer()
290 for n := 0; n < b.N; n++ {
291 if err := m.Decode(buf); err != nil {
292 b.Fatal(err)
293 }
294 bmM = m
295 }
296 }