Skip to content

Commit 0be2d4a

Browse files
Merge branch 'bollhals-batchpublishsizehint'
(cherry picked from commit 805dfb5)
1 parent a77a19f commit 0be2d4a

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

projects/RabbitMQ.Client/client/api/IModel.cs

+6
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ void BasicPublish(string exchange, string routingKey, bool mandatory,
291291
[AmqpMethodDoNotImplement(null)]
292292
IBasicPublishBatch CreateBasicPublishBatch();
293293

294+
/// <summary>
295+
/// Creates a BasicPublishBatch instance
296+
/// </summary>
297+
[AmqpMethodDoNotImplement(null)]
298+
IBasicPublishBatch CreateBasicPublishBatch(int sizeHint);
299+
294300
/// <summary>
295301
/// Construct a completely empty content header for use with the Basic content class.
296302
/// </summary>

projects/RabbitMQ.Client/client/impl/AutorecoveringModel.cs

+10
Original file line numberDiff line numberDiff line change
@@ -1960,5 +1960,15 @@ public IBasicPublishBatch CreateBasicPublishBatch()
19601960

19611961
return ((IFullModel)_delegate).CreateBasicPublishBatch();
19621962
}
1963+
1964+
public IBasicPublishBatch CreateBasicPublishBatch(int sizeHint)
1965+
{
1966+
if (_disposed)
1967+
{
1968+
throw new ObjectDisposedException(GetType().FullName);
1969+
}
1970+
1971+
return ((IFullModel)_delegate).CreateBasicPublishBatch(sizeHint);
1972+
}
19631973
}
19641974
}

projects/RabbitMQ.Client/client/impl/BasicPublishBatch.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,27 @@
3838
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
3939
//---------------------------------------------------------------------------
4040

41-
using System;
42-
using System.Buffers;
4341
using System.Collections.Generic;
4442

4543
using RabbitMQ.Client.Framing.Impl;
4644

4745
namespace RabbitMQ.Client.Impl
4846
{
49-
class BasicPublishBatch : IBasicPublishBatch
47+
internal sealed class BasicPublishBatch : IBasicPublishBatch
5048
{
51-
private readonly List<Command> _commands = new List<Command>();
49+
private readonly List<Command> _commands;
5250
private readonly ModelBase _model;
51+
5352
internal BasicPublishBatch (ModelBase model)
5453
{
5554
_model = model;
55+
_commands = new List<Command>();
56+
}
57+
58+
internal BasicPublishBatch (ModelBase model, int sizeHint)
59+
{
60+
_model = model;
61+
_commands = new List<Command>(sizeHint);
5662
}
5763

5864
public void Add(string exchange, string routingKey, bool mandatory, IBasicProperties basicProperties, byte[] body)

projects/RabbitMQ.Client/client/impl/ModelBase.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,11 @@ public IBasicPublishBatch CreateBasicPublishBatch()
12031203
return new BasicPublishBatch(this);
12041204
}
12051205

1206+
public IBasicPublishBatch CreateBasicPublishBatch(int sizeHint)
1207+
{
1208+
return new BasicPublishBatch(this, sizeHint);
1209+
}
1210+
12061211

12071212
public void ExchangeBind(string destination,
12081213
string source,

projects/Unit/APIApproval.Approve.verified.txt

+1
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ namespace RabbitMQ.Client
398398
uint ConsumerCount(string queue);
399399
RabbitMQ.Client.IBasicProperties CreateBasicProperties();
400400
RabbitMQ.Client.IBasicPublishBatch CreateBasicPublishBatch();
401+
RabbitMQ.Client.IBasicPublishBatch CreateBasicPublishBatch(int sizeHint);
401402
void ExchangeBind(string destination, string source, string routingKey, System.Collections.Generic.IDictionary<string, object> arguments);
402403
void ExchangeBindNoWait(string destination, string source, string routingKey, System.Collections.Generic.IDictionary<string, object> arguments);
403404
void ExchangeDeclare(string exchange, string type, bool durable, bool autoDelete, System.Collections.Generic.IDictionary<string, object> arguments);

projects/Unit/TestBasicPublishBatch.cs

+34
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,39 @@ public void TestBasicPublishBatchSend()
6262
BasicGetResult resultB = Model.BasicGet("test-message-batch-b", true);
6363
Assert.NotNull(resultB);
6464
}
65+
66+
[Test]
67+
public void TestBasicPublishBatchSendWithSizeHint()
68+
{
69+
Model.ConfirmSelect();
70+
Model.QueueDeclare(queue: "test-message-batch-a", durable: false);
71+
Model.QueueDeclare(queue: "test-message-batch-b", durable: false);
72+
IBasicPublishBatch batch = Model.CreateBasicPublishBatch(2);
73+
batch.Add("", "test-message-batch-a", false, null, new byte [] {});
74+
batch.Add("", "test-message-batch-b", false, null, new byte [] {});
75+
batch.Publish();
76+
Model.WaitForConfirmsOrDie(TimeSpan.FromSeconds(15));
77+
BasicGetResult resultA = Model.BasicGet("test-message-batch-a", true);
78+
Assert.NotNull(resultA);
79+
BasicGetResult resultB = Model.BasicGet("test-message-batch-b", true);
80+
Assert.NotNull(resultB);
81+
}
82+
83+
[Test]
84+
public void TestBasicPublishBatchSendWithWrongSizeHint()
85+
{
86+
Model.ConfirmSelect();
87+
Model.QueueDeclare(queue: "test-message-batch-a", durable: false);
88+
Model.QueueDeclare(queue: "test-message-batch-b", durable: false);
89+
IBasicPublishBatch batch = Model.CreateBasicPublishBatch(1);
90+
batch.Add("", "test-message-batch-a", false, null, new byte [] {});
91+
batch.Add("", "test-message-batch-b", false, null, new byte [] {});
92+
batch.Publish();
93+
Model.WaitForConfirmsOrDie(TimeSpan.FromSeconds(15));
94+
BasicGetResult resultA = Model.BasicGet("test-message-batch-a", true);
95+
Assert.NotNull(resultA);
96+
BasicGetResult resultB = Model.BasicGet("test-message-batch-b", true);
97+
Assert.NotNull(resultB);
98+
}
6599
}
66100
}

0 commit comments

Comments
 (0)