TestMessageHandlerInvoker`1.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Abc.Zebus.Dispatch;
  5. using Abc.Zebus.Scan;
  6. using Abc.Zebus.Testing.Extensions;
  7. namespace Abc.Zebus.Testing.Dispatch;
  8. public class TestMessageHandlerInvoker<TMessage> : IMessageHandlerInvoker where TMessage : class, IMessage
  9. {
  10. private readonly bool _shouldBeSubscribedOnStartup;
  11. public TestMessageHandlerInvoker(bool shouldBeSubscribedOnStartup = true)
  12. {
  13. _shouldBeSubscribedOnStartup = shouldBeSubscribedOnStartup;
  14. }
  15. public bool Invoked { get; private set; }
  16. public Type MessageHandlerType => typeof(Handler);
  17. public Type MessageType => typeof(TMessage);
  18. public MessageTypeId MessageTypeId => new(MessageType);
  19. public string DispatchQueueName => DispatchQueueNameScanner.DefaultQueueName;
  20. public MessageHandlerInvokerMode Mode => MessageHandlerInvokerMode.Synchronous;
  21. public IEnumerable<Subscription> GetStartupSubscriptions()
  22. {
  23. return _shouldBeSubscribedOnStartup
  24. ? new[] { new Subscription(MessageTypeId) }
  25. : Array.Empty<Subscription>();
  26. }
  27. public void InvokeMessageHandler(IMessageHandlerInvocation invocation)
  28. {
  29. Invoked = true;
  30. using (invocation.SetupForInvocation())
  31. {
  32. var message = invocation.Messages.ExpectedSingle() as IExecutableMessage;
  33. message?.Execute(invocation);
  34. }
  35. }
  36. public Task InvokeMessageHandlerAsync(IMessageHandlerInvocation invocation)
  37. {
  38. throw new NotSupportedException();
  39. }
  40. public bool ShouldHandle(IMessage message)
  41. {
  42. return true;
  43. }
  44. public bool CanMergeWith(IMessageHandlerInvoker other)
  45. {
  46. return false;
  47. }
  48. public class Handler : IMessageHandler<TMessage>
  49. {
  50. public void Handle(TMessage message)
  51. {
  52. throw new NotSupportedException();
  53. }
  54. }
  55. }