You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SqlServerStorageConnectionTest.cs 4.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.SqlServer.Test
  8. {
  9. [Collection("sqlserver")]
  10. public class SqlServerStorageConnectionTest : DatabaseTestHost
  11. {
  12. private SqlServerStorageConnection _storage;
  13. public SqlServerStorageConnectionTest()
  14. {
  15. var options = GetService<SqlServerOptions>();
  16. var capOptions = GetService<CapOptions>();
  17. _storage = new SqlServerStorageConnection(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]) OUTPUT INSERTED.Id VALUES(@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName);";
  23. var publishMessage = new CapPublishedMessage
  24. {
  25. Name = "SqlServerStorageConnectionTest",
  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("SqlServerStorageConnectionTest", 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. var fetchedMessage = await _storage.FetchNextMessageAsync();
  53. fetchedMessage.Dispose();
  54. Assert.NotNull(fetchedMessage);
  55. Assert.Equal(MessageType.Publish, fetchedMessage.MessageType);
  56. Assert.Equal(3333, fetchedMessage.MessageId);
  57. }
  58. [Fact]
  59. public async Task StoreReceivedMessageAsync_Test()
  60. {
  61. var receivedMessage = new CapReceivedMessage
  62. {
  63. Name = "SqlServerStorageConnectionTest",
  64. Content = "",
  65. Group = "mygroup",
  66. StatusName = StatusName.Scheduled
  67. };
  68. Exception exception = null;
  69. try
  70. {
  71. await _storage.StoreReceivedMessageAsync(receivedMessage);
  72. }
  73. catch (Exception ex)
  74. {
  75. exception = ex;
  76. }
  77. Assert.Null(exception);
  78. }
  79. [Fact]
  80. public async Task GetReceivedMessageAsync_Test()
  81. {
  82. var sql = $@"
  83. INSERT INTO [Cap].[Received]([Name],[Group],[Content],[Retries],[Added],[ExpiresAt],[StatusName]) OUTPUT INSERTED.Id
  84. VALUES(@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);";
  85. var receivedMessage = new CapReceivedMessage
  86. {
  87. Name = "SqlServerStorageConnectionTest",
  88. Content = "",
  89. Group = "mygroup",
  90. StatusName = StatusName.Scheduled
  91. };
  92. var insertedId = default(int);
  93. using (var connection = ConnectionUtil.CreateConnection())
  94. {
  95. insertedId = connection.QueryFirst<int>(sql, receivedMessage);
  96. }
  97. var message = await _storage.GetReceivedMessageAsync(insertedId);
  98. Assert.NotNull(message);
  99. Assert.Equal(StatusName.Scheduled, message.StatusName);
  100. Assert.Equal("SqlServerStorageConnectionTest", message.Name);
  101. Assert.Equal("mygroup", message.Group);
  102. }
  103. [Fact]
  104. public async Task GetNextReceviedMessageToBeEnqueuedAsync_Test()
  105. {
  106. var receivedMessage = new CapReceivedMessage
  107. {
  108. Name = "SqlServerStorageConnectionTest",
  109. Content = "",
  110. Group = "mygroup",
  111. StatusName = StatusName.Scheduled
  112. };
  113. await _storage.StoreReceivedMessageAsync(receivedMessage);
  114. var message = await _storage.GetNextReceivedMessageToBeEnqueuedAsync();
  115. Assert.NotNull(message);
  116. Assert.Equal(StatusName.Scheduled, message.StatusName);
  117. Assert.Equal("SqlServerStorageConnectionTest", message.Name);
  118. Assert.Equal("mygroup", message.Group);
  119. }
  120. }
  121. }