|
| 1 | +using Akka.Actor; |
| 2 | +using Akka.Configuration; |
| 3 | +using Akka.Streams; |
| 4 | +using TurboHttp.Internal; |
| 5 | +using TurboHttp.Transport.Connection; |
| 6 | + |
| 7 | +namespace TurboHttp.StreamTests.Dispatchers; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Verifies that TurboHttp's custom Akka.NET dispatchers are correctly configured, |
| 11 | +/// resolvable from ActorSystem, and applied to the right actors. |
| 12 | +/// </summary> |
| 13 | +public sealed class DispatcherConfigurationSpec : IAsyncDisposable |
| 14 | +{ |
| 15 | + private readonly ActorSystem _system; |
| 16 | + |
| 17 | + public DispatcherConfigurationSpec() |
| 18 | + { |
| 19 | + var config = TurboHttpDispatchers.CreateConfig(TurboClientOptions.DefaultMaxEndpointSubstreams); |
| 20 | + _system = ActorSystem.Create("dispatcher-spec-" + Guid.NewGuid(), config); |
| 21 | + } |
| 22 | + |
| 23 | + public async ValueTask DisposeAsync() |
| 24 | + { |
| 25 | + await _system.Terminate(); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact(Timeout = 5000)] |
| 29 | + public void CreateConfig_should_produce_ForkJoinDispatcher_for_io_dispatcher() |
| 30 | + { |
| 31 | + var config = TurboHttpDispatchers.CreateConfig(256); |
| 32 | + var type = config.GetString("akka.actor.turbohttp-io-dispatcher.type"); |
| 33 | + |
| 34 | + Assert.Equal("ForkJoinDispatcher", type); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact(Timeout = 5000)] |
| 38 | + public void CreateConfig_should_produce_ForkJoinDispatcher_for_stream_dispatcher() |
| 39 | + { |
| 40 | + var config = TurboHttpDispatchers.CreateConfig(256); |
| 41 | + var type = config.GetString("akka.actor.turbohttp-stream-dispatcher.type"); |
| 42 | + |
| 43 | + Assert.Equal("ForkJoinDispatcher", type); |
| 44 | + } |
| 45 | + |
| 46 | + [Theory(Timeout = 5000)] |
| 47 | + [InlineData(32u)] |
| 48 | + [InlineData(64u)] |
| 49 | + [InlineData(128u)] |
| 50 | + [InlineData(256u)] |
| 51 | + [InlineData(512u)] |
| 52 | + [InlineData(1024u)] |
| 53 | + public void CreateConfig_should_clamp_stream_thread_count(uint maxEndpointSubstreams) |
| 54 | + { |
| 55 | + var config = TurboHttpDispatchers.CreateConfig(maxEndpointSubstreams); |
| 56 | + var threadCount = config.GetInt( |
| 57 | + "akka.actor.turbohttp-stream-dispatcher.dedicated-thread-pool.thread-count"); |
| 58 | + |
| 59 | + Assert.InRange(threadCount, Environment.ProcessorCount, 64); |
| 60 | + } |
| 61 | + |
| 62 | + [Theory(Timeout = 5000)] |
| 63 | + [InlineData(32u)] |
| 64 | + [InlineData(256u)] |
| 65 | + [InlineData(1024u)] |
| 66 | + public void CreateConfig_should_clamp_io_thread_count(uint maxEndpointSubstreams) |
| 67 | + { |
| 68 | + var config = TurboHttpDispatchers.CreateConfig(maxEndpointSubstreams); |
| 69 | + var threadCount = config.GetInt( |
| 70 | + "akka.actor.turbohttp-io-dispatcher.dedicated-thread-pool.thread-count"); |
| 71 | + |
| 72 | + Assert.InRange(threadCount, 4, 16); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact(Timeout = 5000)] |
| 76 | + public void IoDispatcher_should_be_resolvable_from_ActorSystem() |
| 77 | + { |
| 78 | + Assert.True( |
| 79 | + _system.Dispatchers.HasDispatcher(TurboHttpDispatchers.IoDispatcher)); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact(Timeout = 5000)] |
| 83 | + public void StreamDispatcher_should_be_resolvable_from_ActorSystem() |
| 84 | + { |
| 85 | + Assert.True( |
| 86 | + _system.Dispatchers.HasDispatcher(TurboHttpDispatchers.StreamDispatcher)); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact(Timeout = 5000)] |
| 90 | + public void WithIoDispatcher_should_apply_when_available() |
| 91 | + { |
| 92 | + var props = Props.Create(() => new ConnectionManagerActor(TimeSpan.FromMinutes(5))); |
| 93 | + |
| 94 | + Assert.Equal(TurboHttpDispatchers.IoDispatcher, props.WithIoDispatcher(_system).Dispatcher); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact(Timeout = 5000)] |
| 98 | + public void WithIoDispatcher_should_fall_back_to_default_when_missing() |
| 99 | + { |
| 100 | + using var bareSystem = ActorSystem.Create("bare-" + Guid.NewGuid()); |
| 101 | + var props = Props.Create(() => new ConnectionManagerActor(TimeSpan.FromMinutes(5))); |
| 102 | + |
| 103 | + // Should NOT have the custom dispatcher — falls back to default |
| 104 | + Assert.NotEqual(TurboHttpDispatchers.IoDispatcher, props.WithIoDispatcher(bareSystem).Dispatcher); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact(Timeout = 5000)] |
| 108 | + public void WithStreamDispatcher_should_apply_when_available() |
| 109 | + { |
| 110 | + var props = Props.Create(() => new ConnectionManagerActor(TimeSpan.FromMinutes(5))); |
| 111 | + |
| 112 | + Assert.Equal(TurboHttpDispatchers.StreamDispatcher, props.WithStreamDispatcher(_system).Dispatcher); |
| 113 | + } |
| 114 | + |
| 115 | + [Fact(Timeout = 5000)] |
| 116 | + public void WithStreamDispatcher_should_fall_back_to_default_when_missing() |
| 117 | + { |
| 118 | + using var bareSystem = ActorSystem.Create("bare-" + Guid.NewGuid()); |
| 119 | + var settings = ActorMaterializerSettings.Create(bareSystem); |
| 120 | + |
| 121 | + // Should NOT have the custom dispatcher — falls back to default |
| 122 | + Assert.NotEqual(TurboHttpDispatchers.StreamDispatcher, settings.WithStreamDispatcher(bareSystem).Dispatcher); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact(Timeout = 5000)] |
| 126 | + public void CreateConfig_should_use_background_threads() |
| 127 | + { |
| 128 | + var config = TurboHttpDispatchers.CreateConfig(256); |
| 129 | + |
| 130 | + Assert.Equal("background", |
| 131 | + config.GetString("akka.actor.turbohttp-io-dispatcher.dedicated-thread-pool.threadtype")); |
| 132 | + Assert.Equal("background", |
| 133 | + config.GetString("akka.actor.turbohttp-stream-dispatcher.dedicated-thread-pool.threadtype")); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact(Timeout = 5000)] |
| 137 | + public void CreateConfig_should_set_expected_throughput_values() |
| 138 | + { |
| 139 | + var config = TurboHttpDispatchers.CreateConfig(256); |
| 140 | + |
| 141 | + Assert.Equal(32, config.GetInt("akka.actor.turbohttp-io-dispatcher.throughput")); |
| 142 | + Assert.Equal(64, config.GetInt("akka.actor.turbohttp-stream-dispatcher.throughput")); |
| 143 | + } |
| 144 | + |
| 145 | + [Fact(Timeout = 5000)] |
| 146 | + public void CreateConfig_should_scale_stream_threads_with_substreams() |
| 147 | + { |
| 148 | + // With high MaxEndpointSubstreams, thread count should be higher than with low |
| 149 | + var configLow = TurboHttpDispatchers.CreateConfig(32); |
| 150 | + var configHigh = TurboHttpDispatchers.CreateConfig(512); |
| 151 | + |
| 152 | + var threadsLow = configLow.GetInt( |
| 153 | + "akka.actor.turbohttp-stream-dispatcher.dedicated-thread-pool.thread-count"); |
| 154 | + var threadsHigh = configHigh.GetInt( |
| 155 | + "akka.actor.turbohttp-stream-dispatcher.dedicated-thread-pool.thread-count"); |
| 156 | + |
| 157 | + Assert.True(threadsHigh >= threadsLow, |
| 158 | + $"Expected high substreams ({threadsHigh}) >= low substreams ({threadsLow})"); |
| 159 | + } |
| 160 | +} |
0 commit comments