Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1276398 Fix not thrown exception when OpenAsync http request fails #999

Merged
merged 7 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
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
Loading