|
| 1 | +#if NET8_0_OR_GREATER |
| 2 | +using System; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using FluentAssertions; |
| 6 | +using Halibut.Diagnostics; |
| 7 | +using Halibut.Logging; |
| 8 | +using Halibut.Queue; |
| 9 | +using Halibut.Queue.Redis; |
| 10 | +using Halibut.Queue.Redis.RedisDataLossDetection; |
| 11 | +using Halibut.Queue.Redis.RedisHelpers; |
| 12 | +using Halibut.ServiceModel; |
| 13 | +using Halibut.Tests.Queue.Redis.Utils; |
| 14 | +using Halibut.Tests.Support; |
| 15 | +using Halibut.Tests.Support.Logging; |
| 16 | +using Halibut.Tests.TestServices; |
| 17 | +using Halibut.Tests.TestServices.Async; |
| 18 | +using Halibut.TestUtils.Contracts; |
| 19 | +using NUnit.Framework; |
| 20 | +using DisposableCollection = Halibut.Util.DisposableCollection; |
| 21 | + |
| 22 | +namespace Halibut.Tests |
| 23 | +{ |
| 24 | + public class RPCOverQueueExecutionModeFixture : BaseTest |
| 25 | + { |
| 26 | + [RedisTest] |
| 27 | + [Test] |
| 28 | + public async Task SimpleRPCOverQueueExecutionExample() |
| 29 | + { |
| 30 | + var services = GetDelegateServiceFactory(); |
| 31 | + var timeoutsAndLimits = new HalibutTimeoutsAndLimitsForTestsBuilder().Build(); |
| 32 | + |
| 33 | + var logFactory = new CachingLogFactory(new TestContextLogCreator("", LogLevel.Trace)); |
| 34 | + |
| 35 | + var log = new TestContextLogCreator("Redis", LogLevel.Fatal); |
| 36 | + |
| 37 | + var preSharedGuid = Guid.NewGuid(); |
| 38 | + |
| 39 | + await using var disposables = new DisposableCollection(); |
| 40 | + |
| 41 | + await using var client = new HalibutRuntimeBuilder() |
| 42 | + .WithServerCertificate(Certificates.Octopus) |
| 43 | + .WithPendingRequestQueueFactory(RedisFactory(preSharedGuid, disposables, log, logFactory)) |
| 44 | + .WithHalibutTimeoutsAndLimits(timeoutsAndLimits) |
| 45 | + .Build(); |
| 46 | + |
| 47 | + await using var worker = new HalibutRuntimeBuilder() |
| 48 | + .WithServerCertificate(Certificates.TentaclePolling) |
| 49 | + .WithServiceFactory(services) |
| 50 | + .WithPendingRequestQueueFactory(RedisFactory(preSharedGuid, disposables, log, logFactory)) |
| 51 | + .WithHalibutTimeoutsAndLimits(timeoutsAndLimits) |
| 52 | + .Build(); |
| 53 | + |
| 54 | + // Start worker polling |
| 55 | + using var workerCts = new CancellationTokenSource(); |
| 56 | + var pollingTask = Task.Run(async () => |
| 57 | + { |
| 58 | + await worker.PollForRPCOverQueueAsync(new Uri(HalibutRuntime.QueueEndpointScheme + "://test-worker"), workerCts.Token); |
| 59 | + }, workerCts.Token); |
| 60 | + |
| 61 | + // Client creates proxy and makes request |
| 62 | + var echo = client.CreateAsyncClient<IEchoService, IAsyncClientEchoService>( |
| 63 | + new ServiceEndPoint(HalibutRuntime.QueueEndpointScheme + "://test-worker", null, client.TimeoutsAndLimits)); |
| 64 | + |
| 65 | + var result = await echo.SayHelloAsync("World"); |
| 66 | + result.Should().Be("World..."); |
| 67 | + |
| 68 | + await workerCts.CancelAsync(); |
| 69 | + |
| 70 | + await pollingTask; |
| 71 | + } |
| 72 | + |
| 73 | + Func<QueueMessageSerializer, IPendingRequestQueueFactory> RedisFactory( |
| 74 | + Guid preSharedGuid, |
| 75 | + DisposableCollection disposables, |
| 76 | + TestContextLogCreator log, |
| 77 | + CachingLogFactory logFactory) |
| 78 | + { |
| 79 | + return msgSer => |
| 80 | + { |
| 81 | + var redisFacade = RedisFacadeBuilder.CreateRedisFacade(prefix: preSharedGuid); |
| 82 | + disposables.AddAsyncDisposable(redisFacade); |
| 83 | + var watchForRedisLosingAllItsData = new WatchForRedisLosingAllItsData(redisFacade, log.CreateNewForPrefix("watcher")); |
| 84 | + disposables.AddAsyncDisposable(watchForRedisLosingAllItsData); |
| 85 | + |
| 86 | + return new RedisPendingRequestQueueFactory(msgSer, |
| 87 | + new InMemoryStoreDataStreamsForDistributedQueues(), |
| 88 | + watchForRedisLosingAllItsData, |
| 89 | + new HalibutRedisTransport(redisFacade), |
| 90 | + new HalibutTimeoutsAndLimitsForTestsBuilder().Build(), |
| 91 | + logFactory); |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + static DelegateServiceFactory GetDelegateServiceFactory() |
| 96 | + { |
| 97 | + var services = new DelegateServiceFactory(); |
| 98 | + services.Register<IEchoService, IAsyncEchoService>(() => new AsyncEchoService()); |
| 99 | + return services; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | +#endif |
0 commit comments