Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Aug 26, 2024
1 parent 4e2094b commit af4daf7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
19 changes: 9 additions & 10 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2342,22 +2342,21 @@ public void TestHangingCloseIsNotBlocking()
var restRequester = new MockCloseHangingRestRequester();
var session = new SFSession("account=test;user=test;password=test", null, restRequester);
session.Open();
var watch = new Stopwatch();
var watchClose = new Stopwatch();
var watchClosedFinished = new Stopwatch();

// act
watch.Start();
session.closeNonBlocking();
watch.Stop();
var closeDuration = watch.Elapsed.Duration();
watch.Restart();
watchClose.Start();
watchClosedFinished.Start();
session.CloseNonBlocking();
watchClose.Stop();
Awaiter.WaitUntilConditionOrTimeout(() => restRequester.CloseRequests.Count > 0, TimeSpan.FromSeconds(15));
watch.Stop();
var backgroundTaskDuration = watch.Elapsed.Duration();
watchClosedFinished.Stop();

// assert
Assert.AreEqual(1, restRequester.CloseRequests.Count);
Assert.Less(closeDuration, TimeSpan.FromSeconds(5)); // close executed immediately
Assert.GreaterOrEqual(backgroundTaskDuration, TimeSpan.FromSeconds(10)); // while background task took more time
Assert.Less(watchClose.Elapsed.Duration(), TimeSpan.FromSeconds(5)); // close executed immediately
Assert.GreaterOrEqual(watchClosedFinished.Elapsed.Duration(), TimeSpan.FromSeconds(10)); // while background task took more time
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Snowflake.Data.Tests/UnitTests/SFSessionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void TestSessionGoneWhenCloseNonBlocking()
var restRequester = new MockCloseSessionGone();
SFSession sfSession = new SFSession("account=test;user=test;password=test", null, restRequester);
sfSession.Open();
Assert.DoesNotThrow(() => sfSession.closeNonBlocking());
Assert.DoesNotThrow(() => sfSession.CloseNonBlocking());
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion Snowflake.Data/Client/SnowflakeDbConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ protected override void Dispose(bool disposing)
}
else
{
SfSession?.closeNonBlocking();
SfSession?.CloseNonBlocking();
SfSession = null;
_connectionState = ConnectionState.Closed;
}
Expand Down
15 changes: 6 additions & 9 deletions Snowflake.Data/Core/Session/SFSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,19 +281,17 @@ internal void close()
stopHeartBeatForThisSession();
var closeSessionRequest = PrepareCloseSessionRequest();
PostCloseSession(closeSessionRequest, restRequester);
// Just in case the session won't be closed twice
sessionToken = null;
}

internal void closeNonBlocking()
internal void CloseNonBlocking()
{
// Nothing to do if the session is not open
if (!IsEstablished()) return;
logger.Debug($"Closing session with id: {sessionId}, user: {_user}, database: {database}, schema: {schema}, role: {role}, warehouse: {warehouse}, connection start timestamp: {_startTime}");
stopHeartBeatForThisSession();
var closeSessionRequest = PrepareCloseSessionRequest();
Task.Run(() => PostCloseSession(closeSessionRequest, restRequester));
// Just in case the session won't be closed twice
sessionToken = null;
}

Expand All @@ -306,34 +304,33 @@ internal async Task CloseAsync(CancellationToken cancellationToken)

var closeSessionRequest = PrepareCloseSessionRequest();

logger.Debug($"Send async closeSessionRequest");
logger.Debug($"Closing session async");
var response = await restRequester.PostAsync<CloseResponse>(closeSessionRequest, cancellationToken).ConfigureAwait(false);
if (!response.success)
{
logger.Error($"Failed to delete session {sessionId}, error ignored. Code: {response.code} Message: {response.message}");
logger.Error($"Failed to close session {sessionId}, error ignored. Code: {response.code} Message: {response.message}");

Check warning on line 311 in Snowflake.Data/Core/Session/SFSession.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Session/SFSession.cs#L311

Added line #L311 was not covered by tests
}

logger.Debug($"Session closed: {sessionId}");
// Just in case the session won't be closed twice
sessionToken = null;
}

private static void PostCloseSession(SFRestRequest closeSessionRequest, IRestRequester restRequester)
{
try
{
logger.Debug($"Send closeSessionRequest");
logger.Debug($"Closing session");
var response = restRequester.Post<CloseResponse>(closeSessionRequest);
if (!response.success)
{
logger.Error($"Failed to delete session: {closeSessionRequest.sid}, error ignored. Code: {response.code} Message: {response.message}");
logger.Error($"Failed to close session: {closeSessionRequest.sid}, error ignored. Code: {response.code} Message: {response.message}");
}

logger.Debug($"Session closed: {closeSessionRequest.sid}");
}
catch (Exception)
{
logger.Error($"Failed to delete session: {closeSessionRequest.sid}, because of exception.");
logger.Error($"Failed to close session: {closeSessionRequest.sid}, because of exception.");
throw;
}
}
Expand Down

0 comments on commit af4daf7

Please sign in to comment.