Skip to content

Commit 288b310

Browse files
SNOW-1276398 Fix not thrown exception when OpenAsync http request fails (#999)
Co-authored-by: Krzysztof Nozderko <[email protected]>
1 parent 982a1e4 commit 288b310

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,6 +2271,45 @@ public void TestUseMultiplePoolsConnectionPoolByDefault()
22712271
// assert
22722272
Assert.AreEqual(ConnectionPoolType.MultipleConnectionPool, poolVersion);
22732273
}
2274+
2275+
[Test]
2276+
[TestCase("connection_timeout=5;")]
2277+
[TestCase("")]
2278+
public void TestOpenAsyncThrowExceptionWhenConnectToUnreachableHost(string extraParameters)
2279+
{
2280+
// arrange
2281+
var connectionString = "account=testAccount;user=testUser;password=testPassword;useProxy=true;proxyHost=no.such.pro.xy;proxyPort=8080;" +
2282+
extraParameters;
2283+
using (var connection = new SnowflakeDbConnection(connectionString))
2284+
{
2285+
// act
2286+
var thrown = Assert.Throws<AggregateException>(() => connection.OpenAsync().Wait());
2287+
2288+
// assert
2289+
Assert.IsTrue(thrown.InnerException is TaskCanceledException || thrown.InnerException is SnowflakeDbException);
2290+
if (thrown.InnerException is SnowflakeDbException)
2291+
SnowflakeDbExceptionAssert.HasErrorCode(thrown.InnerException, SFError.INTERNAL_ERROR);
2292+
Assert.AreEqual(ConnectionState.Closed, connection.State);
2293+
}
2294+
}
2295+
2296+
[Test]
2297+
public void TestOpenAsyncThrowExceptionWhenOperationIsCancelled()
2298+
{
2299+
// arrange
2300+
var connectionString = "account=testAccount;user=testUser;password=testPassword;useProxy=true;proxyHost=no.such.pro.xy;proxyPort=8080;";
2301+
using (var connection = new SnowflakeDbConnection(connectionString))
2302+
{
2303+
var shortCancellation = new CancellationTokenSource(TimeSpan.FromSeconds(5));
2304+
2305+
// act
2306+
var thrown = Assert.Throws<AggregateException>(() => connection.OpenAsync(shortCancellation.Token).Wait());
2307+
2308+
// assert
2309+
Assert.IsInstanceOf<TaskCanceledException>(thrown.InnerException);
2310+
Assert.AreEqual(ConnectionState.Closed, connection.State);
2311+
}
2312+
}
22742313
}
22752314
}
22762315

Snowflake.Data/Client/SnowflakeDbConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ public override Task OpenAsync(CancellationToken cancellationToken)
322322
{
323323
_connectionState = ConnectionState.Closed;
324324
logger.Debug("Connection canceled");
325+
throw new TaskCanceledException("Connecting was cancelled");
325326
}
326327
else
327328
{
@@ -330,8 +331,7 @@ public override Task OpenAsync(CancellationToken cancellationToken)
330331
logger.Debug($"Connection open with pooled session: {SfSession.sessionId}");
331332
OnSessionEstablished();
332333
}
333-
},
334-
cancellationToken);
334+
}, TaskContinuationOptions.None); // this continuation should be executed always (even if the whole operation was canceled) because it sets the proper state of the connection
335335
}
336336

337337
public Mutex GetArrayBindingMutex()

0 commit comments

Comments
 (0)