|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Algolia.Search.Clients; |
| 8 | +using Algolia.Search.Exceptions; |
| 9 | +using Algolia.Search.Http; |
| 10 | +using Algolia.Search.Transport; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | +using Microsoft.Extensions.Logging.Abstractions; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace Algolia.Search.Tests; |
| 16 | + |
| 17 | +public class TimeoutIntegrationTests |
| 18 | +{ |
| 19 | + private static (AlgoliaConfig, StatefulHost) CreateConfigWithHost(string hostUrl) |
| 20 | + { |
| 21 | + var config = new SearchConfig("test-app", "test-key"); |
| 22 | + var host = new StatefulHost { Url = hostUrl, Accept = CallType.Read | CallType.Write }; |
| 23 | + config.CustomHosts = new List<StatefulHost> { host }; |
| 24 | + return (config, host); |
| 25 | + } |
| 26 | + |
| 27 | + private static StatefulHost CreateServerHost() |
| 28 | + { |
| 29 | + var serverHost = |
| 30 | + Environment.GetEnvironmentVariable("CI") == "true" ? "localhost" : "host.docker.internal"; |
| 31 | + |
| 32 | + return new StatefulHost |
| 33 | + { |
| 34 | + Url = serverHost, |
| 35 | + Port = 6676, |
| 36 | + Scheme = HttpScheme.Http, |
| 37 | + Accept = CallType.Read | CallType.Write, |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public async Task RetryCountStateful() |
| 43 | + { |
| 44 | + // connect timeout increases across failed requests: 2s -> 4s -> 6s |
| 45 | + var (config, _) = CreateConfigWithHost("10.255.255.1"); |
| 46 | + var transport = new HttpTransport( |
| 47 | + config, |
| 48 | + new AlgoliaHttpRequester(NullLoggerFactory.Instance), |
| 49 | + NullLoggerFactory.Instance |
| 50 | + ); |
| 51 | + |
| 52 | + var times = new List<double>(); |
| 53 | + for (int i = 0; i < 3; i++) |
| 54 | + { |
| 55 | + var sw = Stopwatch.StartNew(); |
| 56 | + try |
| 57 | + { |
| 58 | + await transport.ExecuteRequestAsync( |
| 59 | + HttpMethod.Get, |
| 60 | + "/test", |
| 61 | + new InternalRequestOptions { UseReadTransporter = true } |
| 62 | + ); |
| 63 | + } |
| 64 | + catch (Exception) |
| 65 | + { |
| 66 | + sw.Stop(); |
| 67 | + times.Add(sw.Elapsed.TotalSeconds); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Connect timeout scales: ConnectTimeout (2s default) * (RetryCount+1) |
| 72 | + // Request 1: 2s * 1 = 2s |
| 73 | + // Request 2: 2s * 2 = 4s |
| 74 | + // Request 3: 2s * 3 = 6s |
| 75 | + Assert.True(times[0] > 1.5 && times[0] < 2.5, $"Request 1 should be ~2s, got {times[0]:F2}s"); |
| 76 | + Assert.True(times[1] > 3.5 && times[1] < 4.5, $"Request 2 should be ~4s, got {times[1]:F2}s"); |
| 77 | + Assert.True(times[2] > 5.5 && times[2] < 7.0, $"Request 3 should be ~6s, got {times[2]:F2}s"); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public async Task RetryCountResets() |
| 82 | + { |
| 83 | + // retry_count resets to 0 after successful request |
| 84 | + var (config, badHost) = CreateConfigWithHost("10.255.255.1"); |
| 85 | + var goodHost = CreateServerHost(); |
| 86 | + var transport = new HttpTransport( |
| 87 | + config, |
| 88 | + new AlgoliaHttpRequester(NullLoggerFactory.Instance), |
| 89 | + NullLoggerFactory.Instance |
| 90 | + ); |
| 91 | + |
| 92 | + // fail twice to increment retry_count |
| 93 | + for (int i = 0; i < 2; i++) |
| 94 | + { |
| 95 | + try |
| 96 | + { |
| 97 | + await transport.ExecuteRequestAsync( |
| 98 | + HttpMethod.Get, |
| 99 | + "/test", |
| 100 | + new InternalRequestOptions { UseReadTransporter = true } |
| 101 | + ); |
| 102 | + } |
| 103 | + catch (Exception) |
| 104 | + { |
| 105 | + // expected to fail |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // switch to good host and succeed |
| 110 | + config.CustomHosts = new List<StatefulHost> { goodHost }; |
| 111 | + goodHost.RetryCount = badHost.RetryCount; |
| 112 | + transport = new HttpTransport( |
| 113 | + config, |
| 114 | + new AlgoliaHttpRequester(NullLoggerFactory.Instance), |
| 115 | + NullLoggerFactory.Instance |
| 116 | + ); |
| 117 | + |
| 118 | + var response = await transport.ExecuteRequestAsync<AlgoliaHttpResponse>( |
| 119 | + HttpMethod.Get, |
| 120 | + "/1/test/instant", |
| 121 | + new InternalRequestOptions { UseReadTransporter = true } |
| 122 | + ); |
| 123 | + |
| 124 | + Assert.Equal(200, response.HttpStatusCode); |
| 125 | + Assert.True( |
| 126 | + goodHost.RetryCount == 0, |
| 127 | + $"retry_count should reset to 0, got {goodHost.RetryCount}" |
| 128 | + ); |
| 129 | + |
| 130 | + // point to bad host again, should timeout at 2s (not 6s) |
| 131 | + goodHost.Url = "10.255.255.1"; |
| 132 | + goodHost.Port = null; |
| 133 | + goodHost.Scheme = HttpScheme.Https; |
| 134 | + |
| 135 | + var sw = Stopwatch.StartNew(); |
| 136 | + try |
| 137 | + { |
| 138 | + await transport.ExecuteRequestAsync( |
| 139 | + HttpMethod.Get, |
| 140 | + "/test", |
| 141 | + new InternalRequestOptions { UseReadTransporter = true } |
| 142 | + ); |
| 143 | + Assert.Fail("Request should have timed out"); |
| 144 | + } |
| 145 | + catch (Exception) |
| 146 | + { |
| 147 | + sw.Stop(); |
| 148 | + var elapsed = sw.Elapsed.TotalSeconds; |
| 149 | + Assert.True(elapsed > 1.5 && elapsed < 2.5, $"After reset should be ~2s, got {elapsed:F2}s"); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments