|
1 | 1 | using System;
|
| 2 | +using System.Text; |
2 | 3 | using System.Threading;
|
3 | 4 | using System.Threading.Tasks;
|
4 | 5 | using NUnit.Framework;
|
@@ -101,5 +102,96 @@ public void CanNotModifyPayloadAfterPublish()
|
101 | 102 | m.BasicCancel(tag);
|
102 | 103 | }
|
103 | 104 | }
|
| 105 | + |
| 106 | + [Test] |
| 107 | + public void TestMaxMessageSize() |
| 108 | + { |
| 109 | + var re = new ManualResetEventSlim(); |
| 110 | + const ushort maxMsgSize = 1024; |
| 111 | + |
| 112 | + int count = 0; |
| 113 | + byte[] msg0 = Encoding.UTF8.GetBytes("hi"); |
| 114 | + |
| 115 | + var r = new System.Random(); |
| 116 | + byte[] msg1 = new byte[maxMsgSize * 2]; |
| 117 | + r.NextBytes(msg1); |
| 118 | + |
| 119 | + var cf = new ConnectionFactory(); |
| 120 | + cf.AutomaticRecoveryEnabled = false; |
| 121 | + cf.TopologyRecoveryEnabled = false; |
| 122 | + cf.MaxMessageSize = maxMsgSize; |
| 123 | + |
| 124 | + bool sawConnectionShutdown = false; |
| 125 | + bool sawModelShutdown = false; |
| 126 | + bool sawConsumerRegistered = false; |
| 127 | + bool sawConsumerCancelled = false; |
| 128 | + |
| 129 | + using (IConnection c = cf.CreateConnection()) |
| 130 | + { |
| 131 | + c.ConnectionShutdown += (o, a) => |
| 132 | + { |
| 133 | + sawConnectionShutdown= true; |
| 134 | + }; |
| 135 | + |
| 136 | + Assert.AreEqual(maxMsgSize, cf.MaxMessageSize); |
| 137 | + Assert.AreEqual(maxMsgSize, cf.Endpoint.MaxMessageSize); |
| 138 | + Assert.AreEqual(maxMsgSize, c.Endpoint.MaxMessageSize); |
| 139 | + |
| 140 | + using (IModel m = c.CreateModel()) |
| 141 | + { |
| 142 | + m.ModelShutdown += (o, a) => |
| 143 | + { |
| 144 | + sawModelShutdown= true; |
| 145 | + }; |
| 146 | + |
| 147 | + m.CallbackException += (o, a) => |
| 148 | + { |
| 149 | + Assert.Fail("Unexpected m.CallbackException"); |
| 150 | + }; |
| 151 | + |
| 152 | + QueueDeclareOk q = m.QueueDeclare(); |
| 153 | + IBasicProperties bp = m.CreateBasicProperties(); |
| 154 | + |
| 155 | + var consumer = new EventingBasicConsumer(m); |
| 156 | + |
| 157 | + consumer.Shutdown += (o, a) => |
| 158 | + { |
| 159 | + re.Set(); |
| 160 | + }; |
| 161 | + |
| 162 | + consumer.Registered += (o, a) => |
| 163 | + { |
| 164 | + sawConsumerRegistered = true; |
| 165 | + }; |
| 166 | + |
| 167 | + consumer.Unregistered += (o, a) => |
| 168 | + { |
| 169 | + Assert.Fail("Unexpected consumer.Unregistered"); |
| 170 | + }; |
| 171 | + |
| 172 | + consumer.ConsumerCancelled += (o, a) => |
| 173 | + { |
| 174 | + sawConsumerCancelled = true; |
| 175 | + }; |
| 176 | + |
| 177 | + consumer.Received += (o, a) => |
| 178 | + { |
| 179 | + Interlocked.Increment(ref count); |
| 180 | + }; |
| 181 | + |
| 182 | + string tag = m.BasicConsume(q.QueueName, true, consumer); |
| 183 | + |
| 184 | + m.BasicPublish("", q.QueueName, bp, msg0); |
| 185 | + m.BasicPublish("", q.QueueName, bp, msg1); |
| 186 | + Assert.IsTrue(re.Wait(TimeSpan.FromSeconds(5))); |
| 187 | + |
| 188 | + Assert.AreEqual(1, count); |
| 189 | + Assert.IsTrue(sawConnectionShutdown); |
| 190 | + Assert.IsTrue(sawModelShutdown); |
| 191 | + Assert.IsTrue(sawConsumerRegistered); |
| 192 | + Assert.IsTrue(sawConsumerCancelled); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
104 | 196 | }
|
105 | 197 | }
|
0 commit comments