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.3 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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`(`Id`,`Version`,`Name`,`Content`,`Retries`,`Added`,`ExpiresAt`,`StatusName`) VALUES(@Id,'v1',@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName);";
  23. var insertedId = SnowflakeId.Default().NextId();
  24. var publishMessage = new CapPublishedMessage
  25. {
  26. Id = insertedId,
  27. Name = "MySqlStorageConnectionTest",
  28. Content = "",
  29. StatusName = StatusName.Scheduled
  30. };
  31. using (var connection = ConnectionUtil.CreateConnection())
  32. {
  33. await connection.ExecuteAsync(sql, publishMessage);
  34. }
  35. var message = await _storage.GetPublishedMessageAsync(insertedId);
  36. Assert.NotNull(message);
  37. Assert.Equal("MySqlStorageConnectionTest", message.Name);
  38. Assert.Equal(StatusName.Scheduled, message.StatusName);
  39. }
  40. [Fact]
  41. public void StoreReceivedMessageAsync_Test()
  42. {
  43. var receivedMessage = new CapReceivedMessage
  44. {
  45. Id = SnowflakeId.Default().NextId(),
  46. Name = "MySqlStorageConnectionTest",
  47. Content = "",
  48. Group = "mygroup",
  49. StatusName = StatusName.Scheduled
  50. };
  51. Exception exception = null;
  52. try
  53. {
  54. _storage.StoreReceivedMessage(receivedMessage);
  55. }
  56. catch (Exception ex)
  57. {
  58. exception = ex;
  59. }
  60. Assert.Null(exception);
  61. }
  62. [Fact]
  63. public async Task GetReceivedMessageAsync_Test()
  64. {
  65. var sql = $@"
  66. INSERT INTO `cap.received`(`Id`,`Version`,`Name`,`Group`,`Content`,`Retries`,`Added`,`ExpiresAt`,`StatusName`)
  67. VALUES(@Id,'v1',@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);";
  68. var insertedId = SnowflakeId.Default().NextId();
  69. var receivedMessage = new CapReceivedMessage
  70. {
  71. Id = insertedId,
  72. Name = "MySqlStorageConnectionTest",
  73. Content = "",
  74. Group = "mygroup",
  75. StatusName = StatusName.Scheduled
  76. };
  77. using (var connection = ConnectionUtil.CreateConnection())
  78. {
  79. await connection.ExecuteAsync(sql, receivedMessage);
  80. }
  81. var message = await _storage.GetReceivedMessageAsync(insertedId);
  82. Assert.NotNull(message);
  83. Assert.Equal(StatusName.Scheduled, message.StatusName);
  84. Assert.Equal("MySqlStorageConnectionTest", message.Name);
  85. Assert.Equal("mygroup", message.Group);
  86. }
  87. }
  88. }