Rabbit交换机确认模式。comfirm和事务模式测试。

This commit is contained in:
falcon 2023-03-10 15:51:15 +08:00
parent 12beee8577
commit 2a9928c209
2 changed files with 57 additions and 6 deletions

View File

@ -138,5 +138,46 @@ namespace RabbitMqTest
} }
/// <summary>
/// 发送到exchange后exchange需要确认.Confirm模式
/// </summary>
[TestMethod("exchangeConfirm")]
public void ExchangeConfirm() {
var exchangeName = "tempExchange";
var channel = GetChannel();
channel.ExchangeDeclare(exchangeName,ExchangeType.Fanout,false,true,null);
channel.ConfirmSelect();
channel.BasicPublish(exchangeName,"",null,RandomMsgBytes());
if(channel.WaitForConfirms()) {
Console.WriteLine("交换机确认成功!");
}
else {
Console.WriteLine("交换机确认失败!");
Assert.Fail();
} }
} }
/// <summary>
/// 发送到exchange后exchange需要确认.事务模式
/// </summary>
[TestMethod("exchangeTx")]
public void ExchangeConfirm2() {
var exchangeName = "tempExchange";
using var channel = GetChannel();
channel.ExchangeDeclare(exchangeName,ExchangeType.Fanout,false,true,null);
try {
channel.TxSelect();
channel.BasicPublish(exchangeName,"",null,RandomMsgBytes());
channel.TxCommit();
Console.WriteLine("交换机确认成功!");
}
catch(Exception ex) {
Console.WriteLine("交换机确认失败!");
Assert.Fail();
}
}
}
}

View File

@ -34,6 +34,16 @@ namespace RabbitMqTest
return sb.ToString(); return sb.ToString();
} }
/// <summary>
/// 生成随机消息字节数组
/// </summary>
/// <param name="len"></param>
/// <returns></returns>
protected virtual Byte[] RandomMsgBytes(int len = 10) {
var str = RandomString(len);
return Encoding.UTF8.GetBytes(str);
}
/// <summary> /// <summary>
/// 创建队列 /// 创建队列
/// </summary> /// </summary>