|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Net; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.SqlServer.TDS.Servers; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Microsoft.Data.SqlClient.Tests |
| 13 | +{ |
| 14 | + public class SqlConnectionReadOnlyRoutingTests |
| 15 | + { |
| 16 | + [Fact] |
| 17 | + public void NonRoutedConnection() |
| 18 | + { |
| 19 | + using TestTdsServer server = TestTdsServer.StartTestServer(); |
| 20 | + SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(server.ConnectionString) { ApplicationIntent = ApplicationIntent.ReadOnly }; |
| 21 | + using SqlConnection connection = new SqlConnection(builder.ConnectionString); |
| 22 | + connection.Open(); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public async Task NonRoutedAsyncConnection() |
| 27 | + { |
| 28 | + using TestTdsServer server = TestTdsServer.StartTestServer(); |
| 29 | + SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(server.ConnectionString) { ApplicationIntent = ApplicationIntent.ReadOnly }; |
| 30 | + using SqlConnection connection = new SqlConnection(builder.ConnectionString); |
| 31 | + await connection.OpenAsync(); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void RoutedConnection() |
| 36 | + => RecursivelyRoutedConnection(1); |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public async Task RoutedAsyncConnection() |
| 40 | + => await RecursivelyRoutedAsyncConnection(1); |
| 41 | + |
| 42 | + [Theory] |
| 43 | + [InlineData(2)] |
| 44 | + [InlineData(9)] |
| 45 | + [InlineData(11)] // The driver rejects more than 10 redirects (11 layers of redirecting servers) |
| 46 | + public void RecursivelyRoutedConnection(int layers) |
| 47 | + { |
| 48 | + TestTdsServer innerServer = TestTdsServer.StartTestServer(); |
| 49 | + IPEndPoint lastEndpoint = innerServer.Endpoint; |
| 50 | + Stack<GenericTDSServer> routingLayers = new(layers + 1); |
| 51 | + string lastConnectionString = innerServer.ConnectionString; |
| 52 | + |
| 53 | + try |
| 54 | + { |
| 55 | + routingLayers.Push(innerServer); |
| 56 | + for (int i = 0; i < layers; i++) |
| 57 | + { |
| 58 | + TestRoutingTdsServer router = TestRoutingTdsServer.StartTestServer(lastEndpoint); |
| 59 | + |
| 60 | + routingLayers.Push(router); |
| 61 | + lastEndpoint = router.Endpoint; |
| 62 | + lastConnectionString = router.ConnectionString; |
| 63 | + } |
| 64 | + |
| 65 | + SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(lastConnectionString) { ApplicationIntent = ApplicationIntent.ReadOnly }; |
| 66 | + using SqlConnection connection = new SqlConnection(builder.ConnectionString); |
| 67 | + connection.Open(); |
| 68 | + } |
| 69 | + finally |
| 70 | + { |
| 71 | + while (routingLayers.Count > 0) |
| 72 | + { |
| 73 | + GenericTDSServer layer = routingLayers.Pop(); |
| 74 | + |
| 75 | + if (layer is IDisposable disp) |
| 76 | + { |
| 77 | + disp.Dispose(); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + [Theory] |
| 84 | + [InlineData(2)] |
| 85 | + [InlineData(9)] |
| 86 | + [InlineData(11)] // The driver rejects more than 10 redirects (11 layers of redirecting servers) |
| 87 | + public async Task RecursivelyRoutedAsyncConnection(int layers) |
| 88 | + { |
| 89 | + TestTdsServer innerServer = TestTdsServer.StartTestServer(); |
| 90 | + IPEndPoint lastEndpoint = innerServer.Endpoint; |
| 91 | + Stack<GenericTDSServer> routingLayers = new(layers + 1); |
| 92 | + string lastConnectionString = innerServer.ConnectionString; |
| 93 | + |
| 94 | + try |
| 95 | + { |
| 96 | + routingLayers.Push(innerServer); |
| 97 | + for (int i = 0; i < layers; i++) |
| 98 | + { |
| 99 | + TestRoutingTdsServer router = TestRoutingTdsServer.StartTestServer(lastEndpoint); |
| 100 | + |
| 101 | + routingLayers.Push(router); |
| 102 | + lastEndpoint = router.Endpoint; |
| 103 | + lastConnectionString = router.ConnectionString; |
| 104 | + } |
| 105 | + |
| 106 | + SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(lastConnectionString) { ApplicationIntent = ApplicationIntent.ReadOnly }; |
| 107 | + using SqlConnection connection = new SqlConnection(builder.ConnectionString); |
| 108 | + await connection.OpenAsync(); |
| 109 | + } |
| 110 | + finally |
| 111 | + { |
| 112 | + while (routingLayers.Count > 0) |
| 113 | + { |
| 114 | + GenericTDSServer layer = routingLayers.Pop(); |
| 115 | + |
| 116 | + if (layer is IDisposable disp) |
| 117 | + { |
| 118 | + disp.Dispose(); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void ConnectionRoutingLimit() |
| 126 | + { |
| 127 | + SqlException sqlEx = Assert.Throws<SqlException>(() => RecursivelyRoutedConnection(12)); // This will fail on the 11th redirect |
| 128 | + |
| 129 | + Assert.Contains("Too many redirections have occurred.", sqlEx.Message, StringComparison.InvariantCultureIgnoreCase); |
| 130 | + } |
| 131 | + |
| 132 | + [Fact] |
| 133 | + public async Task AsyncConnectionRoutingLimit() |
| 134 | + { |
| 135 | + SqlException sqlEx = await Assert.ThrowsAsync<SqlException>(() => RecursivelyRoutedAsyncConnection(12)); // This will fail on the 11th redirect |
| 136 | + |
| 137 | + Assert.Contains("Too many redirections have occurred.", sqlEx.Message, StringComparison.InvariantCultureIgnoreCase); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments