Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

134 рядки
4.7 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using Dapper;
  4. using DotNetCore.CAP.Infrastructure;
  5. using DotNetCore.CAP.Models;
  6. using Xunit;
  7. namespace DotNetCore.CAP.MySql.Test
  8. {
  9. [Collection("MySql")]
  10. public class MySqlStorageConnectionTest : DatabaseTestHost
  11. {
  12. private MySqlStorageConnection _storage;
  13. public MySqlStorageConnectionTest()
  14. {
  15. var options = GetService<MySqlOptions>();
  16. var capOptions = GetService<CapOptions>();
  17. _storage = new MySqlStorageConnection(options, capOptions);
  18. }
  19. [Fact]
  20. public async Task GetPublishedMessageAsync_Test()
  21. {
  22. var sql = "INSERT INTO `cap.published`(`Name`,`Content`,`Retries`,`Added`,`ExpiresAt`,`StatusName`) VALUES(@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName);SELECT @@IDENTITY;";
  23. var publishMessage = new CapPublishedMessage
  24. {
  25. Name = "MySqlStorageConnectionTest",
  26. Content = "",
  27. StatusName = StatusName.Scheduled
  28. };
  29. var insertedId = default(int);
  30. using (var connection = ConnectionUtil.CreateConnection())
  31. {
  32. insertedId = connection.QueryFirst<int>(sql, publishMessage);
  33. }
  34. var message = await _storage.GetPublishedMessageAsync(insertedId);
  35. Assert.NotNull(message);
  36. Assert.Equal("MySqlStorageConnectionTest", message.Name);
  37. Assert.Equal(StatusName.Scheduled, message.StatusName);
  38. }
  39. [Fact]
  40. public async Task FetchNextMessageAsync_Test()
  41. {
  42. var sql = "INSERT INTO `Cap.Queue`(`MessageId`,`MessageType`) VALUES(@MessageId,@MessageType);";
  43. var queue = new CapQueue
  44. {
  45. MessageId = 3333,
  46. MessageType = MessageType.Publish
  47. };
  48. using (var connection = ConnectionUtil.CreateConnection())
  49. {
  50. connection.Execute(sql, queue);
  51. }
  52. using (var fetchedMessage = await _storage.FetchNextMessageAsync())
  53. {
  54. Assert.NotNull(fetchedMessage);
  55. Assert.Equal(MessageType.Publish, fetchedMessage.MessageType);
  56. Assert.Equal(3333, fetchedMessage.MessageId);
  57. }
  58. }
  59. [Fact]
  60. public async Task StoreReceivedMessageAsync_Test()
  61. {
  62. var receivedMessage = new CapReceivedMessage
  63. {
  64. Name = "MySqlStorageConnectionTest",
  65. Content = "",
  66. Group = "mygroup",
  67. StatusName = StatusName.Scheduled
  68. };
  69. Exception exception = null;
  70. try
  71. {
  72. await _storage.StoreReceivedMessageAsync(receivedMessage);
  73. }
  74. catch (Exception ex)
  75. {
  76. exception = ex;
  77. }
  78. Assert.Null(exception);
  79. }
  80. [Fact]
  81. public async Task GetReceivedMessageAsync_Test()
  82. {
  83. var sql = $@"
  84. INSERT INTO `cap.received`(`Name`,`Group`,`Content`,`Retries`,`Added`,`ExpiresAt`,`StatusName`)
  85. VALUES(@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);SELECT @@IDENTITY;";
  86. var receivedMessage = new CapReceivedMessage
  87. {
  88. Name = "MySqlStorageConnectionTest",
  89. Content = "",
  90. Group = "mygroup",
  91. StatusName = StatusName.Scheduled
  92. };
  93. var insertedId = default(int);
  94. using (var connection = ConnectionUtil.CreateConnection())
  95. {
  96. insertedId = connection.QueryFirst<int>(sql, receivedMessage);
  97. }
  98. var message = await _storage.GetReceivedMessageAsync(insertedId);
  99. Assert.NotNull(message);
  100. Assert.Equal(StatusName.Scheduled, message.StatusName);
  101. Assert.Equal("MySqlStorageConnectionTest", message.Name);
  102. Assert.Equal("mygroup", message.Group);
  103. }
  104. [Fact]
  105. public async Task GetNextReceviedMessageToBeEnqueuedAsync_Test()
  106. {
  107. var receivedMessage = new CapReceivedMessage
  108. {
  109. Name = "MySqlStorageConnectionTest",
  110. Content = "",
  111. Group = "mygroup",
  112. StatusName = StatusName.Scheduled
  113. };
  114. await _storage.StoreReceivedMessageAsync(receivedMessage);
  115. var message = await _storage.GetNextReceivedMessageToBeEnqueuedAsync();
  116. Assert.NotNull(message);
  117. Assert.Equal(StatusName.Scheduled, message.StatusName);
  118. Assert.Equal("MySqlStorageConnectionTest", message.Name);
  119. Assert.Equal("mygroup", message.Group);
  120. }
  121. }
  122. }