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.
 
 
 

61 lines
2.0 KiB

  1. using System;
  2. using DotNetCore.CAP.Infrastructure;
  3. using DotNetCore.CAP.Messages;
  4. using DotNetCore.CAP.Processor.States;
  5. using Moq;
  6. using Xunit;
  7. namespace DotNetCore.CAP.Test
  8. {
  9. public class StateChangerTest
  10. {
  11. [Fact]
  12. public void ChangeState()
  13. {
  14. // Arrange
  15. var fixture = Create();
  16. var message = new CapPublishedMessage
  17. {
  18. Id = SnowflakeId.Default().NextId(),
  19. StatusName = StatusName.Scheduled
  20. };
  21. var state = Mock.Of<IState>(s => s.Name == "s" && s.ExpiresAfter == null);
  22. var mockTransaction = new Mock<IStorageTransaction>();
  23. // Act
  24. fixture.ChangeState(message, state, mockTransaction.Object);
  25. // Assert
  26. Assert.Equal("s", message.StatusName);
  27. Assert.Null(message.ExpiresAt);
  28. Mock.Get(state).Verify(s => s.Apply(message, mockTransaction.Object), Times.Once);
  29. mockTransaction.Verify(t => t.UpdateMessage(message), Times.Once);
  30. mockTransaction.Verify(t => t.CommitAsync(), Times.Never);
  31. }
  32. [Fact]
  33. public void ChangeState_ExpiresAfter()
  34. {
  35. // Arrange
  36. var fixture = Create();
  37. var message = new CapPublishedMessage
  38. {
  39. Id = SnowflakeId.Default().NextId(),
  40. StatusName = StatusName.Scheduled
  41. };
  42. var state = Mock.Of<IState>(s => s.Name == "s" && s.ExpiresAfter == TimeSpan.FromHours(1));
  43. var mockTransaction = new Mock<IStorageTransaction>();
  44. // Act
  45. fixture.ChangeState(message, state, mockTransaction.Object);
  46. // Assert
  47. Assert.Equal("s", message.StatusName);
  48. Assert.NotNull(message.ExpiresAt);
  49. mockTransaction.Verify(t => t.UpdateMessage(message), Times.Once);
  50. mockTransaction.Verify(t => t.CommitAsync(), Times.Never);
  51. }
  52. private StateChanger Create() => new StateChanger();
  53. }
  54. }