|
1 | 1 | using System;
|
| 2 | +using System.Text; |
2 | 3 | using System.Threading;
|
3 | 4 | using System.Threading.Tasks;
|
4 | 5 |
|
5 | 6 | using RabbitMQ.Client.Events;
|
6 |
| -using RabbitMQ.Client.Framing; |
7 | 7 | using Xunit;
|
| 8 | +using Xunit.Sdk; |
8 | 9 |
|
9 | 10 | namespace RabbitMQ.Client.Unit
|
10 | 11 | {
|
@@ -130,5 +131,95 @@ public void CanNotModifyPayloadAfterPublish()
|
130 | 131 | m.BasicCancel(tag);
|
131 | 132 | }
|
132 | 133 | }
|
| 134 | + |
| 135 | + [Fact] |
| 136 | + public void TestMaxMessageSize() |
| 137 | + { |
| 138 | + var re = new ManualResetEventSlim(); |
| 139 | + const ushort maxMsgSize = 1024; |
| 140 | + |
| 141 | + int count = 0; |
| 142 | + byte[] msg0 = Encoding.UTF8.GetBytes("hi"); |
| 143 | + |
| 144 | + var r = new System.Random(); |
| 145 | + byte[] msg1 = new byte[maxMsgSize * 2]; |
| 146 | + r.NextBytes(msg1); |
| 147 | + |
| 148 | + var cf = new ConnectionFactory(); |
| 149 | + cf.AutomaticRecoveryEnabled = false; |
| 150 | + cf.TopologyRecoveryEnabled = false; |
| 151 | + cf.MaxMessageSize = maxMsgSize; |
| 152 | + |
| 153 | + bool sawConnectionShutdown = false; |
| 154 | + bool sawModelShutdown = false; |
| 155 | + bool sawConsumerRegistered = false; |
| 156 | + bool sawConsumerCancelled = false; |
| 157 | + |
| 158 | + using (IConnection c = cf.CreateConnection()) |
| 159 | + { |
| 160 | + c.ConnectionShutdown += (o, a) => |
| 161 | + { |
| 162 | + sawConnectionShutdown = true; |
| 163 | + }; |
| 164 | + |
| 165 | + Assert.Equal(maxMsgSize, cf.MaxMessageSize); |
| 166 | + Assert.Equal(maxMsgSize, cf.Endpoint.MaxMessageSize); |
| 167 | + Assert.Equal(maxMsgSize, c.Endpoint.MaxMessageSize); |
| 168 | + |
| 169 | + using (IModel m = c.CreateModel()) |
| 170 | + { |
| 171 | + m.ModelShutdown += (o, a) => |
| 172 | + { |
| 173 | + sawModelShutdown = true; |
| 174 | + }; |
| 175 | + |
| 176 | + m.CallbackException += (o, a) => |
| 177 | + { |
| 178 | + throw new XunitException("Unexpected m.CallbackException"); |
| 179 | + }; |
| 180 | + |
| 181 | + QueueDeclareOk q = m.QueueDeclare(); |
| 182 | + |
| 183 | + var consumer = new EventingBasicConsumer(m); |
| 184 | + |
| 185 | + consumer.Shutdown += (o, a) => |
| 186 | + { |
| 187 | + re.Set(); |
| 188 | + }; |
| 189 | + |
| 190 | + consumer.Registered += (o, a) => |
| 191 | + { |
| 192 | + sawConsumerRegistered = true; |
| 193 | + }; |
| 194 | + |
| 195 | + consumer.Unregistered += (o, a) => |
| 196 | + { |
| 197 | + throw new XunitException("Unexpected consumer.Unregistered"); |
| 198 | + }; |
| 199 | + |
| 200 | + consumer.ConsumerCancelled += (o, a) => |
| 201 | + { |
| 202 | + sawConsumerCancelled = true; |
| 203 | + }; |
| 204 | + |
| 205 | + consumer.Received += (o, a) => |
| 206 | + { |
| 207 | + Interlocked.Increment(ref count); |
| 208 | + }; |
| 209 | + |
| 210 | + string tag = m.BasicConsume(q.QueueName, true, consumer); |
| 211 | + |
| 212 | + m.BasicPublish("", q.QueueName, msg0); |
| 213 | + m.BasicPublish("", q.QueueName, msg1); |
| 214 | + Assert.True(re.Wait(TimeSpan.FromSeconds(5))); |
| 215 | + |
| 216 | + Assert.Equal(1, count); |
| 217 | + Assert.True(sawConnectionShutdown); |
| 218 | + Assert.True(sawModelShutdown); |
| 219 | + Assert.True(sawConsumerRegistered); |
| 220 | + Assert.True(sawConsumerCancelled); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
133 | 224 | }
|
134 | 225 | }
|
0 commit comments