Skip to content

Commit

Permalink
Replace ILogger with SFLogger in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Dec 6, 2024
1 parent 26a5d53 commit 247df19
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Snowflake.Data.Client;
Expand All @@ -18,7 +17,7 @@ namespace Snowflake.Data.Tests.IntegrationTests
public class ConnectionMultiplePoolsAsyncIT: SFBaseTestAsync
{
private readonly PoolConfig _previousPoolConfig = new PoolConfig();
private readonly ILogger logger = SFLoggerFactory.GetCustomLogger<SFConnectionIT>();
private readonly SFLogger logger = SFLoggerFactory.GetLogger<SFConnectionIT>();

[SetUp]
public new void BeforeTest()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/

Expand All @@ -11,7 +11,6 @@
using Snowflake.Data.Core.Session;
using Snowflake.Data.Log;
using Snowflake.Data.Tests.Util;
using Microsoft.Extensions.Logging;

namespace Snowflake.Data.Tests.IntegrationTests
{
Expand All @@ -21,7 +20,7 @@ namespace Snowflake.Data.Tests.IntegrationTests
class ConnectionPoolCommonIT : SFBaseTest
{
private readonly ConnectionPoolType _connectionPoolTypeUnderTest;
private static readonly ILogger s_logger = SFLoggerFactory.GetCustomLogger<ConnectionPoolManager>();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<ConnectionPoolManager>();
private readonly PoolConfig _previousPoolConfig;

public ConnectionPoolCommonIT(ConnectionPoolType connectionPoolTypeUnderTest)
Expand All @@ -39,8 +38,8 @@ public ConnectionPoolCommonIT(ConnectionPoolType connectionPoolTypeUnderTest)
{
SnowflakeDbConnectionPool.SetPooling(true);
}
s_logger.LogDebug($"---------------- BeforeTest ---------------------");
s_logger.LogDebug($"Testing Pool Type: {SnowflakeDbConnectionPool.GetConnectionPoolVersion()}");
s_logger.Debug($"---------------- BeforeTest ---------------------");
s_logger.Debug($"Testing Pool Type: {SnowflakeDbConnectionPool.GetConnectionPoolVersion()}");
}

[TearDown]
Expand Down
12 changes: 6 additions & 6 deletions Snowflake.Data.Tests/IntegrationTests/SFBindTestIT.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
*/
#nullable enable
Expand All @@ -24,7 +24,7 @@ namespace Snowflake.Data.Tests.IntegrationTests
[TestFixture]
class SFBindTestIT : SFBaseTest
{
private static readonly ILogger s_logger = SFLoggerFactory.GetCustomLogger<SFBindTestIT>();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SFBindTestIT>();

[Test]
public void TestArrayBind()
Expand Down Expand Up @@ -906,7 +906,7 @@ public void TestDateTimeBinding(ResultFormat resultFormat, SFTableType tableType
var bindingThreshold = 65280; // when exceeded enforces bindings via file on stage
var smallBatchRowCount = 2;
var bigBatchRowCount = bindingThreshold / 2;
s_logger.LogInformation(testCase);
s_logger.Info(testCase);

using (IDbConnection conn = new SnowflakeDbConnection(ConnectionString))
{
Expand Down Expand Up @@ -945,7 +945,7 @@ public void TestDateTimeBinding(ResultFormat resultFormat, SFTableType tableType
var row = 0;
using (var select = conn.CreateCommand($"select {sql_columns} from {TableName} order by id"))
{
s_logger.LogDebug(select.CommandText);
s_logger.Debug(select.CommandText);
var reader = select.ExecuteReader();
while (reader.Read())
{
Expand Down Expand Up @@ -992,7 +992,7 @@ private void InsertSingleRecord(IDbConnection conn, string sqlInsert, DbType bin
}

// Act
s_logger.LogInformation(sqlInsert);
s_logger.Info(sqlInsert);
var rowsAffected = insert.ExecuteNonQuery();

// Assert
Expand Down Expand Up @@ -1031,7 +1031,7 @@ private void InsertMultipleRecords(IDbConnection conn, string sqlInsert, DbType
}

// Act
s_logger.LogDebug(sqlInsert);
s_logger.Debug(sqlInsert);
var rowsAffected = insert.ExecuteNonQuery();

// Assert
Expand Down
20 changes: 10 additions & 10 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/

Expand All @@ -25,7 +25,7 @@ namespace Snowflake.Data.Tests.IntegrationTests
[TestFixture]
class SFConnectionIT : SFBaseTest
{
private static readonly ILogger s_logger = SFLoggerFactory.GetCustomLogger<SFConnectionIT>();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SFConnectionIT>();

[Test]
public void TestBasicConnection()
Expand Down Expand Up @@ -83,14 +83,14 @@ public void TestApplicationName()
try
{
conn.Open();
s_logger.LogDebug("{appName}");
s_logger.Debug("{appName}");
Assert.Fail();

}
catch (SnowflakeDbException e)
{
// Expected
s_logger.LogDebug("Failed opening connection ", e);
s_logger.Debug("Failed opening connection ", e);
AssertIsConnectionFailure(e);
}

Expand Down Expand Up @@ -127,7 +127,7 @@ public void TestIncorrectUserOrPasswordBasicConnection()
catch (SnowflakeDbException e)
{
// Expected
s_logger.LogDebug("Failed opening connection ", e);
s_logger.Debug("Failed opening connection ", e);
AssertIsConnectionFailure(e);
}

Expand All @@ -142,7 +142,7 @@ public void TestConnectionIsNotMarkedAsOpenWhenWasNotCorrectlyOpenedBefore(bool
{
for (int i = 0; i < 2; ++i)
{
s_logger.LogDebug($"Running try #{i}");
s_logger.Debug($"Running try #{i}");
SnowflakeDbConnection snowflakeConnection = null;
try
{
Expand All @@ -168,7 +168,7 @@ public void TestConnectionIsNotMarkedAsOpenWhenWasNotCorrectlyOpenedWithUsingCla
{
for (int i = 0; i < 2; ++i)
{
s_logger.LogDebug($"Running try #{i}");
s_logger.Debug($"Running try #{i}");
SnowflakeDbConnection snowflakeConnection = null;
try
{
Expand Down Expand Up @@ -1559,7 +1559,7 @@ public void TestInvalidProxySettingFromConnectionString()
catch (SnowflakeDbException e)
{
// Expected
s_logger.LogDebug("Failed opening connection ", e);
s_logger.Debug("Failed opening connection ", e);
Assert.AreEqual(270001, e.ErrorCode); //Internal error
AssertIsConnectionFailure(e);
}
Expand Down Expand Up @@ -1868,7 +1868,7 @@ public void TestKeepAlive()
[TestFixture]
class SFConnectionITAsync : SFBaseTestAsync
{
private static ILogger logger = SFLoggerFactory.GetCustomLogger<SFConnectionITAsync>();
private static SFLogger logger = SFLoggerFactory.GetLogger<SFConnectionITAsync>();


[Test]
Expand Down Expand Up @@ -1898,7 +1898,7 @@ public void TestCancelLoginBeforeTimeout()

// Cancel the connection because it will never succeed since there is no
// connection_timeout defined
logger.LogDebug("connectionCancelToken.Cancel ");
logger.Debug("connectionCancelToken.Cancel ");
connectionCancelToken.Cancel();

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Snowflake.Data.Tests.IntegrationTests
[TestFixture, NonParallelizable]
class SFConnectionWithTomlIT : SFBaseTest
{
private static readonly ILogger s_logger = SFLoggerFactory.GetCustomLogger<SFConnectionIT>();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SFConnectionIT>();

private static string s_workingDirectory;

Expand Down
12 changes: 6 additions & 6 deletions Snowflake.Data.Tests/Mock/MockSnowflakeDbConnection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2021 Snowflake Computing Inc. All rights reserved.
*/

Expand All @@ -15,7 +15,7 @@ namespace Snowflake.Data.Tests.Mock
{
class MockSnowflakeDbConnection : SnowflakeDbConnection
{
private ILogger logger = SFLoggerFactory.GetCustomLogger<MockSnowflakeDbConnection>();
private SFLogger logger = SFLoggerFactory.GetLogger<MockSnowflakeDbConnection>();

private IMockRestRequester _restRequester;

Expand All @@ -32,7 +32,7 @@ public MockSnowflakeDbConnection()

public override void Open()
{
logger.LogDebug("Open Connection.");
logger.Debug("Open Connection.");
SetMockSession();
try
{
Expand All @@ -42,7 +42,7 @@ public override void Open()
{
// Otherwise when Dispose() is called, the close request would timeout.
_connectionState = System.Data.ConnectionState.Closed;
logger.LogError("Unable to connect", e);
logger.Error("Unable to connect", e);
throw;
}
OnSessionEstablished();
Expand All @@ -62,14 +62,14 @@ public override Task OpenAsync(CancellationToken cancellationToken)
// Exception from SfSession.OpenAsync
Exception sfSessionEx = previousTask.Exception;
_connectionState = ConnectionState.Closed;
logger.LogError("Unable to connect", sfSessionEx);
logger.Error("Unable to connect", sfSessionEx);
throw //sfSessionEx.InnerException;
new SnowflakeDbException(sfSessionEx, SFError.INTERNAL_ERROR, "Unable to connect");
}
if (previousTask.IsCanceled)
{
_connectionState = ConnectionState.Closed;
logger.LogDebug("Connection canceled");
logger.Debug("Connection canceled");
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions Snowflake.Data.Tests/SFBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void TearDownContext()
#endif
public class SFBaseTestAsync
{
private static readonly ILogger s_logger = SFLoggerFactory.GetCustomLogger<SFBaseTestAsync>();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SFBaseTestAsync>();

private const string ConnectionStringWithoutAuthFmt = "scheme={0};host={1};port={2};" +
"account={3};role={4};db={5};schema={6};warehouse={7}";
Expand Down Expand Up @@ -119,7 +119,7 @@ protected void CreateOrReplaceTable(IDbConnection conn, string tableName, string
var columnsStr = string.Join(", ", columns);
var cmd = conn.CreateCommand();
cmd.CommandText = $"CREATE OR REPLACE {tableType} TABLE {tableName}({columnsStr}) {additionalQueryStr}";
s_logger.LogDebug(cmd.CommandText);
s_logger.Debug(cmd.CommandText);
cmd.ExecuteNonQuery();

_tablesToRemove.Add(tableName);
Expand Down
22 changes: 11 additions & 11 deletions Snowflake.Data.Tests/Util/ConnectingThreads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public ConnectingThreads JoinAll()

public void Enqueue(ThreadEvent threadEvent) => _events.Enqueue(threadEvent);

public static ILogger Logger() => SFLoggerFactory.GetCustomLogger<IConnectionManager>(); // we have to choose a class from Snowflake.Data package otherwise it will be visible in GH build output
public static SFLogger Logger() => SFLoggerFactory.GetLogger<IConnectionManager>(); // we have to choose a class from Snowflake.Data package otherwise it will be visible in GH build output
}

class ConnectingThread
{
private static readonly ILogger s_logger = ConnectingThreads.Logger();
private static readonly SFLogger s_logger = ConnectingThreads.Logger();

private string _name;

Expand Down Expand Up @@ -104,21 +104,21 @@ private void Execute()
{
var connection = new SnowflakeDbConnection();
connection.ConnectionString = _connectionString;
s_logger.LogDebug($"Execution started, will sleep for {_waitBeforeConnectMillis} ms");
s_logger.Debug($"Execution started, will sleep for {_waitBeforeConnectMillis} ms");
Sleep(_waitBeforeConnectMillis);
var watch = new StopWatch();
watch.Start();
var connected = false;
try
{
s_logger.LogDebug("Opening the connection");
s_logger.Debug("Opening the connection");
connection.Open();
connected = true;
}
catch (Exception exception)
{
watch.Stop();
s_logger.LogError($"Execution failed because of the error: {exception}");
s_logger.Error($"Execution failed because of the error: {exception}");
_events.Enqueue(ThreadEvent.EventConnectingFailed(_name, exception, watch.ElapsedMilliseconds));
}
if (connected)
Expand All @@ -129,7 +129,7 @@ private void Execute()
Sleep(_waitAfterConnectMillis);
if (_closeOnExit)
{
s_logger.LogDebug($"Closing the connection");
s_logger.Debug($"Closing the connection");
connection.Close();
}
}
Expand Down Expand Up @@ -185,7 +185,7 @@ public static ThreadEvent EventWaitingForSessionStarted(string threadName) =>

class SessionPoolThreadEventHandler: SessionPoolEventHandler
{
private static readonly ILogger s_logger = ConnectingThreads.Logger();
private static readonly SFLogger s_logger = ConnectingThreads.Logger();
private readonly ConnectingThreads _connectingThreads;

public SessionPoolThreadEventHandler(ConnectingThreads connectingThreads)
Expand All @@ -198,24 +198,24 @@ public override void OnWaitingForSessionStarted(SessionPool sessionPool)
var threadName = Thread.CurrentThread.Name;
var realThreadName = threadName.StartsWith(ConnectingThread.NamePrefix)
? threadName.Substring(ConnectingThread.NamePrefix.Length) : threadName;
s_logger.LogWarning($"Thread is going to wait for an available session. Current time in milliseconds: {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}");
s_logger.Warn($"Thread is going to wait for an available session. Current time in milliseconds: {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}");
var waitingStartedEvent = ThreadEvent.EventWaitingForSessionStarted(realThreadName);
_connectingThreads.Enqueue(waitingStartedEvent);
}

public override void OnWaitingForSessionStarted(SessionPool sessionPool, long millisLeft)
{
s_logger.LogWarning($"Thread is going to wait with milliseconds timeout of {millisLeft}. Current time in milliseconds: {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}");
s_logger.Warn($"Thread is going to wait with milliseconds timeout of {millisLeft}. Current time in milliseconds: {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}");
}

public override void OnWaitingForSessionSuccessful(SessionPool sessionPool)
{
s_logger.LogWarning($"Thread has been woken with a session granted. Current time in milliseconds: {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}");
s_logger.Warn($"Thread has been woken with a session granted. Current time in milliseconds: {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}");
}

public override void OnSessionProvided(SessionPool sessionPool)
{
s_logger.LogWarning($"Thread has got a session. Current time in milliseconds: {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}");
s_logger.Warn($"Thread has got a session. Current time in milliseconds: {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}");
}
}
}
6 changes: 3 additions & 3 deletions Snowflake.Data.Tests/Util/DbConnectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ namespace Snowflake.Data.Tests.Util
{
public static class DbConnectionExtensions
{
private static readonly ILogger s_logger = SFLoggerFactory.GetCustomLogger<TestIcebergTable>();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<TestIcebergTable>();

internal static IDbCommand CreateCommand(this IDbConnection connection, string commandText)
{
var command = connection.CreateCommand();
command.Connection = connection;
command.CommandText = commandText;
s_logger.LogDebug(commandText);
s_logger.Debug(commandText);
return command;
}

internal static int ExecuteNonQuery(this IDbConnection connection, string commandText)
{
var rowsAffected = connection.CreateCommand(commandText).ExecuteNonQuery();
s_logger.LogDebug($"Affected row(s): {rowsAffected}");
s_logger.Debug($"Affected row(s): {rowsAffected}");
return rowsAffected;
}

Expand Down

0 comments on commit 247df19

Please sign in to comment.