Skip to content

Commit

Permalink
SNOW-1276398 Fix not thrown exception when OpenAsync http request fai…
Browse files Browse the repository at this point in the history
…ls (#999)

Co-authored-by: Krzysztof Nozderko <[email protected]>
  • Loading branch information
1 parent 982a1e4 commit 288b310
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
39 changes: 39 additions & 0 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2271,6 +2271,45 @@ public void TestUseMultiplePoolsConnectionPoolByDefault()
// assert
Assert.AreEqual(ConnectionPoolType.MultipleConnectionPool, poolVersion);
}

[Test]
[TestCase("connection_timeout=5;")]
[TestCase("")]
public void TestOpenAsyncThrowExceptionWhenConnectToUnreachableHost(string extraParameters)
{
// arrange
var connectionString = "account=testAccount;user=testUser;password=testPassword;useProxy=true;proxyHost=no.such.pro.xy;proxyPort=8080;" +
extraParameters;
using (var connection = new SnowflakeDbConnection(connectionString))
{
// act
var thrown = Assert.Throws<AggregateException>(() => connection.OpenAsync().Wait());

// assert
Assert.IsTrue(thrown.InnerException is TaskCanceledException || thrown.InnerException is SnowflakeDbException);
if (thrown.InnerException is SnowflakeDbException)
SnowflakeDbExceptionAssert.HasErrorCode(thrown.InnerException, SFError.INTERNAL_ERROR);
Assert.AreEqual(ConnectionState.Closed, connection.State);
}
}

[Test]
public void TestOpenAsyncThrowExceptionWhenOperationIsCancelled()
{
// arrange
var connectionString = "account=testAccount;user=testUser;password=testPassword;useProxy=true;proxyHost=no.such.pro.xy;proxyPort=8080;";
using (var connection = new SnowflakeDbConnection(connectionString))
{
var shortCancellation = new CancellationTokenSource(TimeSpan.FromSeconds(5));

// act
var thrown = Assert.Throws<AggregateException>(() => connection.OpenAsync(shortCancellation.Token).Wait());

// assert
Assert.IsInstanceOf<TaskCanceledException>(thrown.InnerException);
Assert.AreEqual(ConnectionState.Closed, connection.State);
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Snowflake.Data/Client/SnowflakeDbConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ public override Task OpenAsync(CancellationToken cancellationToken)
{
_connectionState = ConnectionState.Closed;
logger.Debug("Connection canceled");
throw new TaskCanceledException("Connecting was cancelled");
}
else
{
Expand All @@ -330,8 +331,7 @@ public override Task OpenAsync(CancellationToken cancellationToken)
logger.Debug($"Connection open with pooled session: {SfSession.sessionId}");
OnSessionEstablished();
}
},
cancellationToken);
}, TaskContinuationOptions.None); // this continuation should be executed always (even if the whole operation was canceled) because it sets the proper state of the connection
}

public Mutex GetArrayBindingMutex()
Expand Down

0 comments on commit 288b310

Please sign in to comment.