Path: redb-route/tests/redb.Route.Tests.Elasticsearch/ElasticsearchConsumerUnitTests.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Elasticsearch/ElasticsearchConsumerUnitTests.cs ---- using System.Reflection; using System.Text; using Elastic.Clients.Elasticsearch; using Elastic.Transport; using Microsoft.Extensions.DependencyInjection; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Elasticsearch; namespace redb.Route.Tests.Elasticsearch; /// /// The Elasticsearch consumer against an in-memory transport, no server: a polled hit runs in an exchange with its own /// DI scope, as every other consumer's message does, and a failed poll is recorded on the endpoint with its text. /// public sealed class ElasticsearchConsumerUnitTests { private const string Uri = "elasticsearch://orders?delay=100&initialDelay=0"; private static readonly TimeSpan Wait = TimeSpan.FromSeconds(10); private const string OneHit = "{\"took\":1,\"timed_out\":false,\"_shards\":{\"total\":1,\"successful\":1,\"skipped\":0,\"failed\":0}," + "\"hits\":{\"total\":{\"value\":1,\"relation\":\"eq\"},\"max_score\":1.0," + "\"hits\":[{\"_index\":\"orders\",\"_id\":\"1\",\"_score\":1.0,\"_source\":{\"v\":1},\"sort\":[1]}]}}"; private sealed class Marker; private static Dictionary> ProductHeader() => new() { ["x-elastic-product"] = ["Elasticsearch"] }; private static void UseClient(ElasticsearchEndpoint endpoint, ElasticsearchClient client) => typeof(ElasticsearchEndpoint).GetField("_client", BindingFlags.Instance | BindingFlags.NonPublic)! .SetValue(endpoint, client); [Fact] public async Task A_polled_hit_runs_in_an_exchange_with_its_own_DI_scope() { var services = new ServiceCollection().AddScoped().BuildServiceProvider(); await using var context = new RouteContext(services); context.AddComponent(new ElasticsearchComponent()); var endpoint = (ElasticsearchEndpoint)context.GetEndpoint(Uri); UseClient(endpoint, new ElasticsearchClient(new ElasticsearchClientSettings( new InMemoryRequestInvoker(Encoding.UTF8.GetBytes(OneHit), 200, null, "application/vnd.elasticsearch+json; compatible-with=8", ProductHeader())))); var scoped = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); context.AddRoutes(r => r.From(Uri).Process(e => scoped.TrySetResult(e.ServiceProvider?.GetService() is not null))); await context.Start(); var hadScope = await scoped.Task.WaitAsync(Wait); hadScope.Should().BeTrue("the consumer creates the exchange with the endpoint's scope factory"); } [Fact] public async Task A_failed_poll_is_recorded_with_its_text() { await using var context = new RouteContext(); context.AddComponent(new ElasticsearchComponent()); var endpoint = (ElasticsearchEndpoint)context.GetEndpoint(Uri); var settings = new ElasticsearchClientSettings(new InMemoryRequestInvoker( null, 500, new HttpRequestException("search refused by the test"), "application/vnd.elasticsearch+json; compatible-with=8", ProductHeader())) .ThrowExceptions(); UseClient(endpoint, new ElasticsearchClient(settings)); context.AddRoutes(r => r.From(Uri).Process(_ => { })); await context.Start(); var deadline = DateTime.UtcNow + Wait; while (endpoint.Errors == 0 && DateTime.UtcNow < deadline) await Task.Delay(20); endpoint.Errors.Should().BeGreaterThan(0, "the failed poll is recorded on the endpoint"); endpoint.LastErrorMessage.Should().NotBeNullOrEmpty("the dashboard shows what failed"); } }