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.
 
 
 

75 lines
2.4 KiB

  1. using System;
  2. using DotNetCore.CAP.Infrastructure;
  3. using DotNetCore.CAP.Models;
  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 async void StoreReceivedMessageAsync_TestAsync()
  17. {
  18. var id = await _connection.StoreReceivedMessageAsync(new CapReceivedMessage(new MessageContext
  19. {
  20. Group = "test",
  21. Name = "test",
  22. Content = "test-content"
  23. }));
  24. id.Should().BeGreaterThan(0);
  25. }
  26. [Fact]
  27. public void ChangeReceivedState_Test()
  28. {
  29. StoreReceivedMessageAsync_TestAsync();
  30. var collection = Database.GetCollection<CapReceivedMessage>(MongoDBOptions.ReceivedCollection);
  31. var msg = collection.Find(x => true).FirstOrDefault();
  32. _connection.ChangeReceivedState(msg.Id, StatusName.Scheduled).Should().BeTrue();
  33. collection.Find(x => x.Id == msg.Id).FirstOrDefault()?.StatusName.Should().Be(StatusName.Scheduled);
  34. }
  35. [Fact]
  36. public async void GetReceivedMessagesOfNeedRetry_TestAsync()
  37. {
  38. var msgs = await _connection.GetReceivedMessagesOfNeedRetry();
  39. msgs.Should().BeEmpty();
  40. var msg = new CapReceivedMessage
  41. {
  42. Group = "test",
  43. Name = "test",
  44. Content = "test-content",
  45. StatusName = StatusName.Failed
  46. };
  47. var id = await _connection.StoreReceivedMessageAsync(msg);
  48. var collection = Database.GetCollection<CapReceivedMessage>(MongoDBOptions.ReceivedCollection);
  49. var updateDef = Builders<CapReceivedMessage>
  50. .Update.Set(x => x.Added, DateTime.Now.AddMinutes(-5));
  51. await collection.UpdateOneAsync(x => x.Id == id, updateDef);
  52. msgs = await _connection.GetReceivedMessagesOfNeedRetry();
  53. msgs.Should().HaveCountGreaterThan(0);
  54. }
  55. [Fact]
  56. public void GetReceivedMessageAsync_Test()
  57. {
  58. var msg = _connection.GetReceivedMessageAsync(1);
  59. msg.Should().NotBeNull();
  60. }
  61. }
  62. }