Skip to content

Commit

Permalink
SNOW-902588: Tests parallelization (#772)
Browse files Browse the repository at this point in the history
### Description
Enable the parallelization of the test cases.

### Checklist
- [x] Code compiles correctly
- [x] Code is formatted according to [Coding
Conventions](../CodingConventions.md)
- [x] Created tests which fail without the change (if possible)
- [x] All tests passing (`dotnet test`)
- [x] Extended the README / documentation, if necessary
- [x] Provide JIRA issue id (if possible) or GitHub issue id in PR name
  • Loading branch information
sfc-gh-pbulawa authored Sep 12, 2023
1 parent a175090 commit 4609451
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ concurrency:
env:
DOTNET_VERSION: 6.0
DOTNET_LEGACY_VERSION: 4.7.1
# uncomment to run the tests sequentially
#SEQUENTIAL_ENV: SEQUENTIAL_TEST_RUN

jobs:
test-windows:
Expand Down
40 changes: 20 additions & 20 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ public void TestConnectString()
//cmd.CommandText = "drop database \"dlTest\"";
cmd.CommandText = $"drop schema \"{schemaName}\"";
cmd.ExecuteNonQuery();
cmd.CommandText = "use database "+ testConfig.database;
cmd.CommandText = "use database " + testConfig.database;
cmd.ExecuteNonQuery();
cmd.CommandText = "use schema " + testConfig.schema;
cmd.ExecuteNonQuery();
}
conn.Close();
Expand Down Expand Up @@ -379,7 +381,7 @@ public void TestConnectViaSecureString()
}
}

[Test]
[Test, NonParallelizable]
public void TestLoginTimeout()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand Down Expand Up @@ -417,7 +419,7 @@ public void TestLoginTimeout()
}
}

[Test]
[Test, NonParallelizable]
public void TestLoginWithMaxRetryReached()
{
using (IDbConnection conn = new MockSnowflakeDbConnection())
Expand Down Expand Up @@ -445,7 +447,7 @@ public void TestLoginWithMaxRetryReached()
}
}

[Test]
[Test, NonParallelizable]
[Ignore("Disable unstable test cases for now")]
public void TestDefaultLoginTimeout()
{
Expand Down Expand Up @@ -638,6 +640,7 @@ public void TestSwitchDb()
Assert.AreEqual("SNOWFLAKE_SAMPLE_DATA", conn.Database);
}

conn.ChangeDatabase(testConfig.database);
conn.Close();
}

Expand Down Expand Up @@ -1666,7 +1669,7 @@ class SFConnectionITAsync : SFBaseTestAsync
private static SFLogger logger = SFLoggerFactory.GetLogger<SFConnectionITAsync>();


[Test]
[Test, NonParallelizable]
public void TestCancelLoginBeforeTimeout()
{
using (var conn = new MockSnowflakeDbConnection())
Expand Down Expand Up @@ -1714,7 +1717,7 @@ public void TestCancelLoginBeforeTimeout()
}
}

[Test]
[Test, NonParallelizable]
public void TestAsyncLoginTimeout()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand All @@ -1728,11 +1731,10 @@ public void TestAsyncLoginTimeout()

Assert.AreEqual(conn.State, ConnectionState.Closed);

CancellationTokenSource connectionCancelToken = new CancellationTokenSource();
Stopwatch stopwatch = Stopwatch.StartNew();
try
{
Task connectTask = conn.OpenAsync(connectionCancelToken.Token);
Task connectTask = conn.OpenAsync(CancellationToken.None);
connectTask.Wait();
}
catch (AggregateException e)
Expand All @@ -1755,7 +1757,7 @@ public void TestAsyncLoginTimeout()
}
}

[Test]
[Test, NonParallelizable]
public void TestAsyncDefaultLoginTimeout()
{
using (var conn = new MockSnowflakeDbConnection())
Expand All @@ -1765,11 +1767,10 @@ public void TestAsyncDefaultLoginTimeout()

Assert.AreEqual(conn.State, ConnectionState.Closed);

CancellationTokenSource connectionCancelToken = new CancellationTokenSource();
Stopwatch stopwatch = Stopwatch.StartNew();
try
{
Task connectTask = conn.OpenAsync(connectionCancelToken.Token);
Task connectTask = conn.OpenAsync(CancellationToken.None);
connectTask.Wait();
}
catch (AggregateException e)
Expand Down Expand Up @@ -1802,11 +1803,10 @@ public void TestAsyncConnectionFailFast()
conn.ConnectionString = invalidConnectionString;

Assert.AreEqual(conn.State, ConnectionState.Closed);
CancellationTokenSource connectionCancelToken = new CancellationTokenSource();
Task connectTask = null;
try
{
connectTask = conn.OpenAsync(connectionCancelToken.Token);
connectTask = conn.OpenAsync(CancellationToken.None);
connectTask.Wait();
Assert.Fail();
}
Expand Down Expand Up @@ -1835,22 +1835,22 @@ public void TestCloseAsyncWithCancellation()
Task task = null;

// Close the connection. It's not opened yet, but it should not have any issue
task = conn.CloseAsync(new CancellationTokenSource().Token);
task = conn.CloseAsync(CancellationToken.None);
task.Wait();
Assert.AreEqual(conn.State, ConnectionState.Closed);

// Open the connection
task = conn.OpenAsync(new CancellationTokenSource().Token);
task = conn.OpenAsync(CancellationToken.None);
task.Wait();
Assert.AreEqual(conn.State, ConnectionState.Open);

// Close the opened connection
task = conn.CloseAsync(new CancellationTokenSource().Token);
task = conn.CloseAsync(CancellationToken.None);
task.Wait();
Assert.AreEqual(conn.State, ConnectionState.Closed);

// Close the connection again.
task = conn.CloseAsync(new CancellationTokenSource().Token);
task = conn.CloseAsync(CancellationToken.None);
task.Wait();
Assert.AreEqual(conn.State, ConnectionState.Closed);
}
Expand Down Expand Up @@ -1893,7 +1893,7 @@ public void TestCloseAsync()
}
#endif

[Test]
[Test, NonParallelizable]
public void TestCloseAsyncFailure()
{
using (var conn = new MockSnowflakeDbConnection(new MockCloseSessionException()))
Expand All @@ -1904,12 +1904,12 @@ public void TestCloseAsyncFailure()
Task task = null;

// Open the connection
task = conn.OpenAsync(new CancellationTokenSource().Token);
task = conn.OpenAsync(CancellationToken.None);
task.Wait();
Assert.AreEqual(conn.State, ConnectionState.Open);

// Close the opened connection
task = conn.CloseAsync(new CancellationTokenSource().Token);
task = conn.CloseAsync(CancellationToken.None);
try
{
task.Wait();
Expand Down
21 changes: 18 additions & 3 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionPoolT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public void AfterTest()
previousPoolConfig.Reset();
}

[OneTimeTearDown]
public static void AfterAllTests()
{
SnowflakeDbConnectionPool.ClearAllPools();
}

[Test]
[Ignore("dummy test case for showing test progress.")]
public void ConnectionPoolTDone()
Expand Down Expand Up @@ -339,9 +345,12 @@ public void TestConnectionPoolMultiThreading()

t1.Start();
t2.Start();

t1.Join();
t2.Join();
}

static void ThreadProcess1(string connstr)
void ThreadProcess1(string connstr)
{
var conn1 = new SnowflakeDbConnection();
conn1.ConnectionString = connstr;
Expand All @@ -352,7 +361,7 @@ static void ThreadProcess1(string connstr)
Assert.AreEqual(ConnectionState.Closed, conn1.State);
}

static void ThreadProcess2(string connstr)
void ThreadProcess2(string connstr)
{
var conn1 = new SnowflakeDbConnection();
conn1.ConnectionString = connstr;
Expand Down Expand Up @@ -429,7 +438,7 @@ public void TestConnectionPoolTurnOff()
}
}

[TestFixture]
[TestFixture, NonParallelizable]
class SFConnectionPoolITAsync : SFBaseTestAsync
{
private static SFLogger logger = SFLoggerFactory.GetLogger<SFConnectionPoolITAsync>();
Expand All @@ -448,6 +457,12 @@ public void AfterTest()
{
previousPoolConfig.Reset();
}

[OneTimeTearDown]
public static void AfterAllTests()
{
SnowflakeDbConnectionPool.ClearAllPools();
}

[Test]
public void TestConnectionPoolWithAsync()
Expand Down
8 changes: 3 additions & 5 deletions Snowflake.Data.Tests/IntegrationTests/SFDbCommandIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ public void DbCommandITAsyncDone()
[Test]
public void TestExecAsyncAPI()
{
SnowflakeDbConnectionPool.ClearAllPools();
using (DbConnection conn = new SnowflakeDbConnection())
{
SnowflakeDbConnectionPool.ClearAllPools();
conn.ConnectionString = ConnectionString;

Task connectTask = conn.OpenAsync(CancellationToken.None);
Assert.AreEqual(ConnectionState.Connecting, conn.State);

connectTask.Wait();
Assert.AreEqual(ConnectionState.Open, conn.State);

Expand Down Expand Up @@ -254,7 +252,7 @@ public void TestSimpleLargeResultSet()
/*
* Disabled to make sure that configuration changes does not cause problems with appveyor
*/
[Test]
[Test, NonParallelizable]
public void TestUseV1ResultParser()
{
var chunkParserVersion = SFConfiguration.Instance().ChunkParserVersion;
Expand Down Expand Up @@ -283,7 +281,7 @@ public void TestUseV1ResultParser()
SFConfiguration.Instance().ChunkDownloaderVersion = chunkDownloaderVersion;
}

[Test]
[Test, NonParallelizable]
public void TestUseV2ChunkDownloader()
{
var chunkParserVersion = SFConfiguration.Instance().ChunkParserVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public void testWithAllQueryTypes()
$"remove @%{TableName};" +
"create or replace temporary procedure P1() returns varchar language javascript as $$ return ''; $$;" +
"call p1();" +
"use role public";
$"use role {testConfig.role}";

// Set statement count
var stmtCountParam = cmd.CreateParameter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Snowflake.Data.Tests.IntegrationTests
using Client;
using System.Threading.Tasks;

[TestFixture]
[TestFixture, NonParallelizable]
class SFReusableChunkTest : SFBaseTest
{
[Test]
Expand Down
18 changes: 15 additions & 3 deletions Snowflake.Data.Tests/SFBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using NUnit.Framework;
using Snowflake.Data.Client;
using Snowflake.Data.Tests.Util;

[assembly:LevelOfParallelism(10)]

namespace Snowflake.Data.Tests
{
using NUnit.Framework;
Expand Down Expand Up @@ -48,14 +51,18 @@ public static void TearDownContext()
[TestFixture]
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
[SetCulture("en-US")]
#if !SEQUENTIAL_TEST_RUN
[Parallelizable(ParallelScope.All)]
#endif
public class SFBaseTestAsync
{
private const string ConnectionStringWithoutAuthFmt = "scheme={0};host={1};port={2};" +
"account={3};role={4};db={5};schema={6};warehouse={7}";
private const string ConnectionStringSnowflakeAuthFmt = ";user={0};password={1};";
protected readonly string TestName = TestContext.CurrentContext.Test.MethodName;

protected string TableName => TestName + TestContext.CurrentContext.WorkerId?.Replace("#", "_");
protected string TestNameWithWorker => TestName + TestContext.CurrentContext.WorkerId?.Replace("#", "_");
protected string TableName => TestNameWithWorker;


private Stopwatch _stopwatch;

Expand Down Expand Up @@ -151,9 +158,14 @@ public class TestEnvironment

private static Dictionary<string, TimeSpan> s_testPerformance;

private static readonly object s_testPerformanceLock = new object();

public static void RecordTestPerformance(string name, TimeSpan time)
{
s_testPerformance.Add(name, time);
lock (s_testPerformanceLock)
{
s_testPerformance.Add(name, time);
}
}

[OneTimeSetUp]
Expand Down
1 change: 1 addition & 0 deletions Snowflake.Data.Tests/Snowflake.Data.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Copyright>Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.</Copyright>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<LangVersion>7.3</LangVersion>
<DefineConstants>$(SEQUENTIAL_ENV)</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private SFResultSet mockSFResultSet(QueryExecResponseData responseData, Cancella
return new SFResultSet(responseData, new SFStatement(session), token);
}

[Test]
[Test, NonParallelizable]
public void TestGetDownloader([Values(false, true)] bool useV2ChunkDownloader, [Values(1, 2, 3, 4)] int chunkDownloaderVersion)
{
// Set configuration settings
Expand Down
10 changes: 7 additions & 3 deletions Snowflake.Data.Tests/UnitTests/SFAzureClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/

using System;

namespace Snowflake.Data.Tests.UnitTests
{
using NUnit.Framework;
Expand Down Expand Up @@ -45,7 +47,7 @@ class SFAzureClientTest : SFBaseTest
const int Parallel = 0;

// File name for download tests
const string DownloadFileName = "mockFileName.txt";
[ThreadStatic] private static string t_downloadFileName;

// Token for async tests
CancellationToken _cancellationToken;
Expand All @@ -60,6 +62,8 @@ class SFAzureClientTest : SFBaseTest
[SetUp]
public void BeforeTest()
{
t_downloadFileName = TestNameWithWorker + "_mockFileName.txt";

_fileMetadata = new SFFileMetadata()
{
stageInfo = new PutGetStageInfo()
Expand Down Expand Up @@ -329,7 +333,7 @@ public void TestDownloadFile(HttpStatusCode httpStatusCode, ResultStatus expecte
_fileMetadata.stageInfo.location = httpStatusCode.ToString();

// Act
_client.DownloadFile(_fileMetadata, DownloadFileName, Parallel);
_client.DownloadFile(_fileMetadata, t_downloadFileName, Parallel);

// Assert
Assert.AreEqual(expectedResultStatus.ToString(), _fileMetadata.resultStatus);
Expand Down Expand Up @@ -376,7 +380,7 @@ public async Task TestDownloadFileAsync(HttpStatusCode httpStatusCode, ResultSta
_fileMetadata.stageInfo.location = httpStatusCode.ToString();

// Act
await _client.DownloadFileAsync(_fileMetadata, DownloadFileName, Parallel, _cancellationToken).ConfigureAwait(false);
await _client.DownloadFileAsync(_fileMetadata, t_downloadFileName, Parallel, _cancellationToken).ConfigureAwait(false);

// Assert
Assert.AreEqual(expectedResultStatus.ToString(), _fileMetadata.resultStatus);
Expand Down
Loading

0 comments on commit 4609451

Please sign in to comment.