BusManualTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Abc.Zebus.Core;
  6. using Abc.Zebus.Dispatch;
  7. using Abc.Zebus.Testing;
  8. using Abc.Zebus.Util;
  9. using Moq;
  10. using NUnit.Framework;
  11. using ProtoBuf;
  12. namespace Abc.Zebus.Tests.Core
  13. {
  14. [TestFixture]
  15. [Explicit]
  16. [Category("ManualOnly")]
  17. public class BusManualTests
  18. {
  19. // this must be a valid directory endpoint
  20. private const string _directoryEndPoint = "tcp://localhost:129";
  21. [Test]
  22. public void StartBusAndGetRidOfItLikeABadBoy()
  23. {
  24. CreateBusFactory().CreateAndStartBus();
  25. }
  26. [Test]
  27. public void StartBusAndStopItLikeABoyScout()
  28. {
  29. var bus = CreateBusFactory().CreateAndStartBus();
  30. Thread.Sleep(1000);
  31. bus.Stop();
  32. }
  33. [Test]
  34. public void PublishEventWithoutLocalDispatch()
  35. {
  36. using (var bus = CreateBusFactory().WithHandlers(typeof(ManualEventHandler)).CreateAndStartBus())
  37. {
  38. using (LocalDispatch.Disable())
  39. {
  40. bus.Publish(new ManualEvent(42));
  41. }
  42. Wait.Until(() => ManualEventHandler.ReceivedEventCount == 1, 300.Seconds());
  43. }
  44. }
  45. [Test]
  46. public void SendCommandWithoutLocalDispatch()
  47. {
  48. using (var bus = CreateBusFactory().WithHandlers(typeof(ManualCommandHandler)).CreateAndStartBus())
  49. {
  50. using (LocalDispatch.Disable())
  51. {
  52. bus.Send(new ManualCommand(42)).Wait();
  53. }
  54. Console.WriteLine(ManualCommandHandler.LastId);
  55. }
  56. }
  57. [Test]
  58. public void SendSleepCommands()
  59. {
  60. var tasks = new List<Task>();
  61. using (var bus = CreateBusFactory().WithHandlers(typeof(SleepCommandHandler)).CreateAndStartBus())
  62. {
  63. for (var i = 0; i < 20; ++i)
  64. {
  65. tasks.Add(bus.Send(new SleepCommand()));
  66. }
  67. Task.WaitAll(tasks.ToArray());
  68. }
  69. }
  70. [Test]
  71. public void should_generate_unacked_messages()
  72. {
  73. var targetConfig = new Mock<IBusConfiguration>();
  74. targetConfig.SetupGet(x => x.DirectoryServiceEndPoints).Returns(new[] { _directoryEndPoint });
  75. targetConfig.SetupGet(x => x.IsPersistent).Returns(true);
  76. targetConfig.SetupGet(x => x.RegistrationTimeout).Returns(30.Seconds());
  77. targetConfig.SetupGet(x => x.StartReplayTimeout).Returns(30.Seconds());
  78. var target = CreateBusFactory().WithHandlers(typeof(ManualEventHandler))
  79. .WithConfiguration(targetConfig.Object, "Demo")
  80. .WithPeerId("Some.Random.Persistent.Peer.0")
  81. .CreateAndStartBus();
  82. using (var source = CreateBusFactory().CreateAndStartBus())
  83. {
  84. source.Publish(new ManualEvent(42));
  85. Thread.Sleep(2000);
  86. target.Dispose();
  87. for (int i = 0; i < 1_000; i++)
  88. {
  89. source.Publish(new ManualEvent(42));
  90. }
  91. }
  92. }
  93. private static BusFactory CreateBusFactory()
  94. {
  95. return new BusFactory().WithConfiguration(_directoryEndPoint, "Demo");
  96. }
  97. [ProtoContract]
  98. public class ManualEvent : IEvent
  99. {
  100. [ProtoMember(1, IsRequired = true)]
  101. public readonly int Id;
  102. public ManualEvent(int id)
  103. {
  104. Id = id;
  105. }
  106. }
  107. [ProtoContract]
  108. public class ManualCommand : ICommand
  109. {
  110. [ProtoMember(1, IsRequired = true)]
  111. public readonly int Id;
  112. public ManualCommand(int id)
  113. {
  114. Id = id;
  115. }
  116. }
  117. public class ManualEventHandler : IMessageHandler<ManualEvent>
  118. {
  119. public static int ReceivedEventCount;
  120. public void Handle(ManualEvent message)
  121. {
  122. Interlocked.Increment(ref ReceivedEventCount);
  123. Console.WriteLine(message.Id);
  124. }
  125. }
  126. public class ManualCommandHandler : IMessageHandler<ManualCommand>
  127. {
  128. public static int LastId;
  129. public void Handle(ManualCommand message)
  130. {
  131. LastId = message.Id;
  132. }
  133. }
  134. public class SleepCommand : ICommand
  135. {
  136. }
  137. public class SleepCommandHandler : IMessageHandler<SleepCommand>
  138. {
  139. public void Handle(SleepCommand message)
  140. {
  141. Thread.Sleep(1000);
  142. }
  143. }
  144. }
  145. }