Skip to content

Commit 166e65c

Browse files
Test logging improvements (#670)
* Log test name before starting * Log to test output
1 parent 71f1c45 commit 166e65c

7 files changed

+42
-11
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Reflection;
6+
using Xunit.Sdk;
7+
8+
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Tests.Common
9+
{
10+
internal class LogTestNameAttribute : BeforeAfterTestAttribute
11+
{
12+
public override void Before(MethodInfo methodUnderTest)
13+
{
14+
Console.WriteLine($"Starting test {methodUnderTest.Name}");
15+
}
16+
17+
public override void After(MethodInfo methodUnderTest)
18+
{
19+
Console.WriteLine($"Completed test {methodUnderTest.Name}");
20+
}
21+
}
22+
}

test/Common/TestUtils.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ public static string GetUniqueDBName(string namePrefix)
4141
/// </summary>
4242
/// <param name="connection">The connection. This must be opened.</param>
4343
/// <param name="commandText">The scalar T-SQL command.</param>
44+
/// <param name="logger">The method to call for logging output</param>
4445
/// <param name="catchException">Optional exception handling. Pass back 'true' to handle the
4546
/// exception, 'false' to throw. If Null is passed in then all exceptions are thrown.</param>
4647
/// <param name="message">Optional message to write when this query is executed. Defaults to writing the query commandText</param>
4748
/// <returns>The number of rows affected</returns>
4849
public static int ExecuteNonQuery(
4950
IDbConnection connection,
5051
string commandText,
52+
Action<string> logger,
5153
Predicate<Exception> catchException = null,
5254
string message = null)
5355
{
@@ -69,7 +71,7 @@ public static int ExecuteNonQuery(
6971
cmd.CommandText = commandText;
7072
cmd.CommandType = CommandType.Text;
7173
cmd.CommandTimeout = 60; // Increase from default 30s to prevent timeouts while connecting to Azure SQL DB
72-
Console.WriteLine(message);
74+
logger.Invoke(message);
7375
return cmd.ExecuteNonQuery();
7476
}
7577
catch (Exception ex)
@@ -89,12 +91,14 @@ public static int ExecuteNonQuery(
8991
/// </summary>
9092
/// <param name="connection">The connection. This must be opened.</param>
9193
/// <param name="commandText">The scalar T-SQL command.</param>
94+
/// <param name="logger">The method to call for logging output</param>
9295
/// <param name="catchException">Optional exception handling. Pass back 'true' to handle the
9396
/// exception, 'false' to throw. If Null is passed in then all exceptions are thrown.</param>
9497
/// <returns>The scalar result</returns>
9598
public static object ExecuteScalar(
9699
IDbConnection connection,
97100
string commandText,
101+
Action<string> logger,
98102
Predicate<Exception> catchException = null)
99103
{
100104
if (connection == null)
@@ -112,7 +116,7 @@ public static object ExecuteScalar(
112116
{
113117
cmd.CommandText = commandText;
114118
cmd.CommandType = CommandType.Text;
115-
Console.WriteLine($"Executing scalar {commandText}");
119+
logger.Invoke($"Executing scalar {commandText}");
116120
return cmd.ExecuteScalar();
117121
}
118122
catch (Exception ex)
@@ -132,10 +136,11 @@ public static object ExecuteScalar(
132136
/// Retries the specified action, waiting for the specified duration in between each attempt
133137
/// </summary>
134138
/// <param name="action">The action to run</param>
139+
/// <param name="logger">The method to call for logging output</param>
135140
/// <param name="retryCount">The max number of retries to attempt</param>
136141
/// <param name="waitDurationMs">The duration in milliseconds between each attempt</param>
137142
/// <exception cref="AggregateException">Aggregate of all exceptions thrown if all retries failed</exception>
138-
public static void Retry(Action action, int retryCount = 3, int waitDurationMs = 10000)
143+
public static void Retry(Action action, Action<string> logger, int retryCount = 3, int waitDurationMs = 10000)
139144
{
140145
var exceptions = new List<Exception>();
141146
while (true)
@@ -153,7 +158,7 @@ public static void Retry(Action action, int retryCount = 3, int waitDurationMs =
153158
{
154159
throw new AggregateException($"Action failed all retries", exceptions);
155160
}
156-
Console.WriteLine($"Error running action, retrying after {waitDurationMs}ms. {retryCount} retries left. {ex}");
161+
logger.Invoke($"Error running action, retrying after {waitDurationMs}ms. {retryCount} retries left. {ex}");
157162
Thread.Sleep(waitDurationMs);
158163
}
159164
}

test/Integration/IntegrationTestBase.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ private void SetupDatabase()
127127
{
128128
using var masterConnection = new SqlConnection(this.MasterConnectionString);
129129
masterConnection.Open();
130-
TestUtils.ExecuteNonQuery(masterConnection, $"CREATE DATABASE [{this.DatabaseName}]");
131-
});
130+
TestUtils.ExecuteNonQuery(masterConnection, $"CREATE DATABASE [{this.DatabaseName}]", this.LogOutput);
131+
}, this.LogOutput);
132132

133133
// Setup connection
134134
connectionStringBuilder.InitialCatalog = this.DatabaseName;
@@ -289,7 +289,7 @@ private static string GetFunctionsCoreToolsPath()
289289
return funcPath;
290290
}
291291

292-
private void LogOutput(string output)
292+
protected void LogOutput(string output)
293293
{
294294
if (this.TestOutput != null)
295295
{
@@ -362,15 +362,15 @@ protected async Task<HttpResponseMessage> SendPostRequest(string requestUri, str
362362
/// <param name="message">Optional message to write when this query is executed. Defaults to writing the query commandText</param>
363363
protected void ExecuteNonQuery(string commandText, string message = null)
364364
{
365-
TestUtils.ExecuteNonQuery(this.Connection, commandText, message: message);
365+
TestUtils.ExecuteNonQuery(this.Connection, commandText, this.LogOutput, message: message);
366366
}
367367

368368
/// <summary>
369369
/// Executes a command against the current connection and the result is returned.
370370
/// </summary>
371371
protected object ExecuteScalar(string commandText)
372372
{
373-
return TestUtils.ExecuteScalar(this.Connection, commandText);
373+
return TestUtils.ExecuteScalar(this.Connection, commandText, this.LogOutput);
374374
}
375375

376376

@@ -394,7 +394,7 @@ public void Dispose()
394394
// Drop the test database
395395
using var masterConnection = new SqlConnection(this.MasterConnectionString);
396396
masterConnection.Open();
397-
TestUtils.ExecuteNonQuery(masterConnection, $"DROP DATABASE IF EXISTS {this.DatabaseName}");
397+
TestUtils.ExecuteNonQuery(masterConnection, $"DROP DATABASE IF EXISTS {this.DatabaseName}", this.LogOutput);
398398
}
399399
catch (Exception e4)
400400
{

test/Integration/MultipleSqlBindingsIntegrationTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Tests.Integration
1111
{
1212
[Collection(IntegrationTestsCollection.Name)]
13+
[LogTestName]
1314
public class MultipleSqlBindingsIntegrationTests : IntegrationTestBase
1415
{
1516
public MultipleSqlBindingsIntegrationTests(ITestOutputHelper output) : base(output)

test/Integration/SqlInputBindingIntegrationTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Tests.Integration
1414
{
1515
[Collection(IntegrationTestsCollection.Name)]
16+
[LogTestName]
1617
public class SqlInputBindingIntegrationTests : IntegrationTestBase
1718
{
1819
public SqlInputBindingIntegrationTests(ITestOutputHelper output) : base(output)

test/Integration/SqlOutputBindingIntegrationTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Tests.Integration
1717
{
1818
[Collection(IntegrationTestsCollection.Name)]
19+
[LogTestName]
1920
public class SqlOutputBindingIntegrationTests : IntegrationTestBase
2021
{
2122
public SqlOutputBindingIntegrationTests(ITestOutputHelper output) : base(output)

test/Integration/SqlTriggerBindingIntegrationTestBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Tests.Integration
1717
{
18+
[LogTestName]
1819
public class SqlTriggerBindingIntegrationTestBase : IntegrationTestBase
1920
{
2021
public SqlTriggerBindingIntegrationTestBase(ITestOutputHelper output = null) : base(output)
@@ -128,7 +129,7 @@ void MonitorOutputData(object sender, DataReceivedEventArgs e)
128129
await actions();
129130

130131
// Now wait until either we timeout or we've gotten all the expected changes, whichever comes first
131-
Console.WriteLine($"[{DateTime.UtcNow:u}] Waiting for {operation} changes ({timeoutMs}ms)");
132+
this.LogOutput($"[{DateTime.UtcNow:u}] Waiting for {operation} changes ({timeoutMs}ms)");
132133
await taskCompletion.Task.TimeoutAfter(TimeSpan.FromMilliseconds(timeoutMs), $"Timed out waiting for {operation} changes.");
133134

134135
// Unhook handler since we're done monitoring these changes so we aren't checking other changes done later

0 commit comments

Comments
 (0)