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.

MongoDBStorageConnectionTest.cs 2.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using DotNetCore.CAP.Infrastructure;
  3. using DotNetCore.CAP.Messages;
  4. using FluentAssertions;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using MongoDB.Driver;
  7. using Xunit;
  8. namespace DotNetCore.CAP.MongoDB.Test
  9. {
  10. [Collection("MongoDB")]
  11. public class MongoDBStorageConnectionTest : DatabaseTestHost
  12. {
  13. private IStorageConnection _connection =>
  14. Provider.GetService<MongoDBStorage>().GetConnection();
  15. [Fact]
  16. public void StoreReceivedMessageAsync_TestAsync()
  17. {
  18. var messageContext = new MessageContext
  19. {
  20. Group = "test",
  21. Name = "test",
  22. Content = "test-content"
  23. };
  24. _connection.StoreReceivedMessage(new ReceivedMessage()
  25. {
  26. Id = SnowflakeId.Default().NextId(),
  27. Group=messageContext.Group,
  28. Content=messageContext.Content,
  29. Name=messageContext.Name,
  30. Version="v1"
  31. });
  32. }
  33. [Fact]
  34. public void ChangeReceivedState_Test()
  35. {
  36. StoreReceivedMessageAsync_TestAsync();
  37. var collection = Database.GetCollection<ReceivedMessage>(MongoDBOptions.Value.ReceivedCollection);
  38. var msg = collection.Find(x => true).FirstOrDefault();
  39. _connection.ChangeReceivedState(msg.Id, StatusName.Scheduled).Should().BeTrue();
  40. collection.Find(x => x.Id == msg.Id).FirstOrDefault()?.StatusName.Should().Be(StatusName.Scheduled);
  41. }
  42. [Fact]
  43. public async void GetReceivedMessagesOfNeedRetry_TestAsync()
  44. {
  45. var msgs = await _connection.GetReceivedMessagesOfNeedRetry();
  46. msgs.Should().BeEmpty();
  47. var id = SnowflakeId.Default().NextId();
  48. var msg = new CapReceivedMessage
  49. {
  50. Id = id,
  51. Group = "test",
  52. Name = "test",
  53. Content = "test-content",
  54. StatusName = StatusName.Failed
  55. };
  56. _connection.StoreReceivedMessage(msg);
  57. var collection = Database.GetCollection<ReceivedMessage>(MongoDBOptions.Value.ReceivedCollection);
  58. var updateDef = Builders<ReceivedMessage>
  59. .Update.Set(x => x.Added, DateTime.Now.AddMinutes(-5));
  60. await collection.UpdateOneAsync(x => x.Id == id, updateDef);
  61. msgs = await _connection.GetReceivedMessagesOfNeedRetry();
  62. msgs.Should().HaveCountGreaterThan(0);
  63. }
  64. [Fact]
  65. public void GetReceivedMessageAsync_Test()
  66. {
  67. var msg = _connection.GetReceivedMessageAsync(1);
  68. msg.Should().NotBeNull();
  69. }
  70. }
  71. }