Skip to content

Commit 6761ffd

Browse files
committed
Very basic fake implementation of ConfirmSelect and WaitForConfirms* methods (no messages counting)
1 parent 4b26b6b commit 6761ffd

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

src/AddUp.FakeRabbitMQ.Tests/FakeModelTests.cs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics.CodeAnalysis;
3-
using System.Globalization;
44
using System.Linq;
55
using System.Text;
66
using RabbitMQ.Client;
@@ -761,5 +761,57 @@ public void BasicGet_Should_Not_Remove_The_Message_From_Queue_If_Not_Acked(bool
761761
Assert.Equal(expectedMessageCount, model.WorkingMessages.Count);
762762
}
763763
}
764+
765+
[Fact]
766+
public void WaitForConfirms_should_throw_if_ConfirmSelect_was_not_called()
767+
{
768+
// arrange
769+
var node = new RabbitServer();
770+
using (var model = new FakeModel(node))
771+
// act & assert
772+
Assert.Throws<InvalidOperationException>(() => model.WaitForConfirms());
773+
}
774+
775+
[Fact]
776+
public void WaitForConfirmsOrDie_returns_true_if_ConfirmSelect_was_called()
777+
{
778+
// arrange
779+
var node = new RabbitServer();
780+
using (var model = new FakeModel(node))
781+
{
782+
// act
783+
model.ConfirmSelect();
784+
var result = model.WaitForConfirms();
785+
786+
// assert
787+
Assert.True(result);
788+
}
789+
}
790+
791+
[Fact]
792+
public void WaitForConfirmsOrDie_should_throw_if_ConfirmSelect_was_not_called()
793+
{
794+
// arrange
795+
var node = new RabbitServer();
796+
using (var model = new FakeModel(node))
797+
// act & assert
798+
Assert.Throws<InvalidOperationException>(() => model.WaitForConfirmsOrDie());
799+
}
800+
801+
[Fact]
802+
public void WaitForConfirmsOrDie_does_not_throw_if_ConfirmSelect_was_called()
803+
{
804+
// arrange
805+
var node = new RabbitServer();
806+
using (var model = new FakeModel(node))
807+
{
808+
// act
809+
model.ConfirmSelect();
810+
model.WaitForConfirmsOrDie();
811+
}
812+
813+
// assert
814+
Assert.True(true);
815+
}
764816
}
765817
}

src/AddUp.FakeRabbitMQ/FakeModel.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal sealed class FakeModel : IModel
1515
private readonly ConcurrentDictionary<string, IBasicConsumer> consumers = new ConcurrentDictionary<string, IBasicConsumer>();
1616
private readonly RabbitServer server;
1717
private long lastDeliveryTag;
18+
private bool confirmModeSelected;
1819

1920
public FakeModel(RabbitServer rabbitServer) => server = rabbitServer;
2021

@@ -173,12 +174,21 @@ public uint QueueDelete(string queue, bool ifUnused, bool ifEmpty)
173174
return instance != null ? 1u : 0u;
174175
}
175176

176-
public void ConfirmSelect() => throw new NotImplementedException();
177-
public bool WaitForConfirms() => throw new NotImplementedException();
178-
public bool WaitForConfirms(TimeSpan timeout) => throw new NotImplementedException();
179-
public bool WaitForConfirms(TimeSpan timeout, out bool timedOut) => throw new NotImplementedException();
180-
public void WaitForConfirmsOrDie() => throw new NotImplementedException();
181-
public void WaitForConfirmsOrDie(TimeSpan timeout) => throw new NotImplementedException();
177+
public void ConfirmSelect() => confirmModeSelected = true;
178+
179+
public void WaitForConfirmsOrDie() => WaitForConfirmsOrDie(TimeSpan.Zero);
180+
public void WaitForConfirmsOrDie(TimeSpan timeout) => _ = WaitForConfirms(timeout);
181+
182+
public bool WaitForConfirms() => WaitForConfirms(TimeSpan.Zero);
183+
public bool WaitForConfirms(TimeSpan timeout) => WaitForConfirms(timeout, out _);
184+
public bool WaitForConfirms(TimeSpan timeout, out bool timedOut)
185+
{
186+
if (!confirmModeSelected)
187+
throw new InvalidOperationException("Confirms not selected");
188+
189+
timedOut = false;
190+
return true;
191+
}
182192

183193
public string BasicConsume(string queue, bool autoAck, IBasicConsumer consumer) => BasicConsume(queue, autoAck, Guid.NewGuid().ToString(), true, false, null, consumer);
184194
public string BasicConsume(string queue, bool autoAck, string consumerTag, IBasicConsumer consumer) => BasicConsume(queue, autoAck, consumerTag, true, false, null, consumer);

0 commit comments

Comments
 (0)