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.
 
 
 

72 lines
2.3 KiB

  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using DotNetCore.CAP.Abstractions;
  5. using DotNetCore.CAP.Internal;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Logging;
  8. using Xunit;
  9. namespace DotNetCore.CAP.Test
  10. {
  11. public class ConsumerInvokerFactoryTest
  12. {
  13. private IConsumerInvokerFactory consumerInvokerFactory;
  14. public ConsumerInvokerFactoryTest()
  15. {
  16. var services = new ServiceCollection();
  17. services.AddLogging();
  18. var provider = services.BuildServiceProvider();
  19. var logFactory = provider.GetRequiredService<ILoggerFactory>();
  20. var binder = new ModelBinderFactory();
  21. consumerInvokerFactory = new ConsumerInvokerFactory(logFactory, binder, provider);
  22. }
  23. [Fact]
  24. public void CreateInvokerTest()
  25. {
  26. var methodInfo = typeof(Sample).GetRuntimeMethods()
  27. .Single(x => x.Name == nameof(Sample.ThrowException));
  28. var description = new ConsumerExecutorDescriptor
  29. {
  30. MethodInfo = methodInfo,
  31. ImplTypeInfo = typeof(Sample).GetTypeInfo()
  32. };
  33. var messageContext = new MessageContext();
  34. var context = new ConsumerContext(description, messageContext);
  35. var invoker = consumerInvokerFactory.CreateInvoker(context);
  36. Assert.NotNull(invoker);
  37. }
  38. [Theory]
  39. [InlineData(nameof(Sample.ThrowException))]
  40. [InlineData(nameof(Sample.AsyncMethod))]
  41. public async void InvokeMethodTest(string methodName)
  42. {
  43. var methodInfo = typeof(Sample).GetRuntimeMethods()
  44. .Single(x => x.Name == methodName);
  45. var description = new ConsumerExecutorDescriptor
  46. {
  47. MethodInfo = methodInfo,
  48. ImplTypeInfo = typeof(Sample).GetTypeInfo()
  49. };
  50. var messageContext = new MessageContext();
  51. var context = new ConsumerContext(description, messageContext);
  52. var invoker = consumerInvokerFactory.CreateInvoker(context);
  53. await Assert.ThrowsAsync(typeof(Exception), async () =>
  54. {
  55. await invoker.InvokeAsync();
  56. });
  57. }
  58. }
  59. }