Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

81 righe
2.5 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 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 CapReceivedMessage(messageContext)
  25. {
  26. Id = SnowflakeId.Default().NextId()
  27. });
  28. }
  29. [Fact]
  30. public void ChangeReceivedState_Test()
  31. {
  32. StoreReceivedMessageAsync_TestAsync();
  33. var collection = Database.GetCollection<CapReceivedMessage>(MongoDBOptions.ReceivedCollection);
  34. var msg = collection.Find(x => true).FirstOrDefault();
  35. _connection.ChangeReceivedState(msg.Id, StatusName.Scheduled).Should().BeTrue();
  36. collection.Find(x => x.Id == msg.Id).FirstOrDefault()?.StatusName.Should().Be(StatusName.Scheduled);
  37. }
  38. [Fact]
  39. public async void GetReceivedMessagesOfNeedRetry_TestAsync()
  40. {
  41. var msgs = await _connection.GetReceivedMessagesOfNeedRetry();
  42. msgs.Should().BeEmpty();
  43. var id = SnowflakeId.Default().NextId();
  44. var msg = new CapReceivedMessage
  45. {
  46. Id = id,
  47. Group = "test",
  48. Name = "test",
  49. Content = "test-content",
  50. StatusName = StatusName.Failed
  51. };
  52. _connection.StoreReceivedMessage(msg);
  53. var collection = Database.GetCollection<CapReceivedMessage>(MongoDBOptions.ReceivedCollection);
  54. var updateDef = Builders<CapReceivedMessage>
  55. .Update.Set(x => x.Added, DateTime.Now.AddMinutes(-5));
  56. await collection.UpdateOneAsync(x => x.Id == id, updateDef);
  57. msgs = await _connection.GetReceivedMessagesOfNeedRetry();
  58. msgs.Should().HaveCountGreaterThan(0);
  59. }
  60. [Fact]
  61. public void GetReceivedMessageAsync_Test()
  62. {
  63. var msg = _connection.GetReceivedMessageAsync(1);
  64. msg.Should().NotBeNull();
  65. }
  66. }
  67. }