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.

MySqlStorageConnectionTest.cs 3.4 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
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Threading.Tasks;
  3. using Dapper;
  4. using DotNetCore.CAP.Infrastructure;
  5. using DotNetCore.CAP.Messages;
  6. using Microsoft.Extensions.Options;
  7. using Xunit;
  8. namespace DotNetCore.CAP.MySql.Test
  9. {
  10. [Collection("MySql")]
  11. public class MySqlStorageConnectionTest : DatabaseTestHost
  12. {
  13. private MySqlStorageConnection _storage;
  14. public MySqlStorageConnectionTest()
  15. {
  16. var options = GetService<IOptions<MySqlOptions>>();
  17. var capOptions = GetService<IOptions<CapOptions>>();
  18. _storage = new MySqlStorageConnection(options, capOptions);
  19. }
  20. [Fact]
  21. public async Task GetPublishedMessageAsync_Test()
  22. {
  23. var sql = "INSERT INTO `cap.published`(`Id`,`Version`,`Name`,`Content`,`Retries`,`Added`,`ExpiresAt`,`StatusName`) VALUES(@Id,'v1',@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName);";
  24. var insertedId = SnowflakeId.Default().NextId();
  25. var publishMessage = new CapPublishedMessage
  26. {
  27. Id = insertedId,
  28. Name = "MySqlStorageConnectionTest",
  29. Content = "",
  30. StatusName = StatusName.Scheduled
  31. };
  32. using (var connection = ConnectionUtil.CreateConnection())
  33. {
  34. await connection.ExecuteAsync(sql, publishMessage);
  35. }
  36. var message = await _storage.GetPublishedMessageAsync(insertedId);
  37. Assert.NotNull(message);
  38. Assert.Equal("MySqlStorageConnectionTest", message.Name);
  39. Assert.Equal(StatusName.Scheduled, message.StatusName);
  40. }
  41. [Fact]
  42. public void StoreReceivedMessageAsync_Test()
  43. {
  44. var receivedMessage = new CapReceivedMessage
  45. {
  46. Id = SnowflakeId.Default().NextId(),
  47. Name = "MySqlStorageConnectionTest",
  48. Content = "",
  49. Group = "mygroup",
  50. StatusName = StatusName.Scheduled
  51. };
  52. Exception exception = null;
  53. try
  54. {
  55. _storage.StoreReceivedMessage(receivedMessage);
  56. }
  57. catch (Exception ex)
  58. {
  59. exception = ex;
  60. }
  61. Assert.Null(exception);
  62. }
  63. [Fact]
  64. public async Task GetReceivedMessageAsync_Test()
  65. {
  66. var sql = $@"
  67. INSERT INTO `cap.received`(`Id`,`Version`,`Name`,`Group`,`Content`,`Retries`,`Added`,`ExpiresAt`,`StatusName`)
  68. VALUES(@Id,'v1',@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);";
  69. var insertedId = SnowflakeId.Default().NextId();
  70. var receivedMessage = new CapReceivedMessage
  71. {
  72. Id = insertedId,
  73. Name = "MySqlStorageConnectionTest",
  74. Content = "",
  75. Group = "mygroup",
  76. StatusName = StatusName.Scheduled
  77. };
  78. using (var connection = ConnectionUtil.CreateConnection())
  79. {
  80. await connection.ExecuteAsync(sql, receivedMessage);
  81. }
  82. var message = await _storage.GetReceivedMessageAsync(insertedId);
  83. Assert.NotNull(message);
  84. Assert.Equal(StatusName.Scheduled, message.StatusName);
  85. Assert.Equal("MySqlStorageConnectionTest", message.Name);
  86. Assert.Equal("mygroup", message.Group);
  87. }
  88. }
  89. }