Skip to content

Commit

Permalink
Tests on core (#155)
Browse files Browse the repository at this point in the history
* Run tests on .net core

* disabled core build for unit tests until approval tests have core edition

* Updated to latest sql transport.

* Fixed connection string.

* Fixed inspections.

* Fixed dtc tests.

* Fixed last 2 tests.
  • Loading branch information
SzymonPobiega authored and SimonCropp committed Aug 23, 2017
1 parent dab4ff8 commit 3b234e1
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/AcceptanceTestHelper/AcceptanceTestHelper.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<TargetFrameworks>net452;netcoreapp2.0</TargetFrameworks>
<AssemblyName>AcceptanceTestHelper</AssemblyName>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(SolutionDir)Test.snk</AssemblyOriginatorKeyFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class DTCEndpoint : EndpointConfigurationBuilder
{
public DTCEndpoint()
{
EndpointSetup<DefaultServer>();
EndpointSetup<DefaultServer>(c => c.ConfigureTransport().Transactions(TransportTransactionMode.TransactionScope));
}

public class MyMessageHandler : IHandleMessages<MyMessage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class TransactionalEndpoint : EndpointConfigurationBuilder
{
public TransactionalEndpoint()
{
EndpointSetup<DefaultServer>();
EndpointSetup<DefaultServer>(c => c.ConfigureTransport().Transactions(TransportTransactionMode.TransactionScope));
}

public class MyMessageHandler : IHandleMessages<MyMessage>
Expand Down
173 changes: 173 additions & 0 deletions src/MsSqlAcceptanceTests/ConfigureEndpointSqlServerTransport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using System;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.AcceptanceTesting.Support;
using NServiceBus.Configuration.AdvancedExtensibility;
using NServiceBus.Transport;

public class ConfigureEndpointSqlServerTransport : IConfigureEndpointTestExecution
{
public Task Configure(string endpointName, EndpointConfiguration configuration, RunSettings settings, PublisherMetadata publisherMetadata)
{
queueBindings = configuration.GetSettings().Get<QueueBindings>();
var transportConfig = configuration.UseTransport<SqlServerTransport>();
connectionString = MsSqlConnectionBuilder.ConnectionString;
transportConfig.ConnectionString(connectionString);

transportConfig.Transactions(TransportTransactionMode.SendsAtomicWithReceive);

var routingConfig = transportConfig.Routing();

foreach (var publisher in publisherMetadata.Publishers)
{
foreach (var eventType in publisher.Events)
{
routingConfig.RegisterPublisher(eventType, publisher.PublisherName);
}
}

return Task.FromResult(0);
}

public Task Cleanup()
{
using (var conn = new SqlConnection(connectionString))
{
conn.Open();

var queueAddresses = queueBindings.ReceivingAddresses.Select(QueueAddress.Parse).ToList();
foreach (var address in queueAddresses)
{
TryDeleteTable(conn, address);
TryDeleteTable(conn, new QueueAddress(address.Table + ".Delayed", address.Schema, address.Catalog));
}
}
return Task.FromResult(0);
}

static void TryDeleteTable(SqlConnection conn, QueueAddress address)
{
try
{
using (var comm = conn.CreateCommand())
{
comm.CommandText = $"IF OBJECT_ID('{address.QualifiedTableName}', 'U') IS NOT NULL DROP TABLE {address.QualifiedTableName}";
comm.ExecuteNonQuery();
}
}
catch (Exception e)
{
if (!e.Message.Contains("it does not exist or you do not have permission"))
{
throw;
}
}
}

string connectionString;
QueueBindings queueBindings;

class QueueAddress
{
public QueueAddress(string table, string schemaName, string catalogName)
{
Table = table;
Catalog = SafeUnquote(catalogName);
Schema = SafeUnquote(schemaName);
}

public string Catalog { get; }
public string Table { get; }
public string Schema { get; }

public static QueueAddress Parse(string address)
{
var firstAtIndex = address.IndexOf("@", StringComparison.Ordinal);

if (firstAtIndex == -1)
{
return new QueueAddress(address, null, null);
}

var tableName = address.Substring(0, firstAtIndex);
address = firstAtIndex + 1 < address.Length ? address.Substring(firstAtIndex + 1) : string.Empty;

address = ExtractNextPart(address, out var schemaName);

string catalogName = null;

if (address != string.Empty)
{
ExtractNextPart(address, out catalogName);
}
return new QueueAddress(tableName, schemaName, catalogName);
}

public string QualifiedTableName => $"{Quote(Catalog)}.{Quote(Schema)}.{Quote(Table)}";

static string ExtractNextPart(string address, out string part)
{
var noRightBrackets = 0;
var index = 1;

while (true)
{
if (index >= address.Length)
{
part = address;
return string.Empty;
}

if (address[index] == '@' && (address[0] != '[' || noRightBrackets % 2 == 1))
{
part = address.Substring(0, index);
return index + 1 < address.Length ? address.Substring(index + 1) : string.Empty;
}

if (address[index] == ']')
{
noRightBrackets++;
}

index++;
}
}

static string Quote(string name)
{
if (name == null)
{
return null;
}
return prefix + name.Replace(suffix, suffix + suffix) + suffix;
}

static string SafeUnquote(string name)
{
var result = Unquote(name);
return string.IsNullOrWhiteSpace(result)
? null
: result;
}

const string prefix = "[";
const string suffix = "]";
static string Unquote(string quotedString)
{
if (quotedString == null)
{
return null;
}

if (!quotedString.StartsWith(prefix) || !quotedString.EndsWith(suffix))
{
return quotedString;
}

return quotedString
.Substring(prefix.Length, quotedString.Length - prefix.Length - suffix.Length).Replace(suffix + suffix, suffix);
}
}
}
19 changes: 12 additions & 7 deletions src/MsSqlAcceptanceTests/MsSqlAcceptanceTests.csproj
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<TargetFrameworks>net452;netcoreapp2.0</TargetFrameworks>
<AssemblyName>MsSqlAcceptanceTests</AssemblyName>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(SolutionDir)Test.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Transactions" />
<Reference Include="System.Configuration" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.*" />
<PackageReference Include="NServiceBus" Version="7.0.0-*" />
<PackageReference Include="NServiceBus.SqlServer" Version="4.0.0-alpha0032" />
<PackageReference Include="NServiceBus.AcceptanceTesting" Version="7.0.0-*" />
<PackageReference Include="NServiceBus.Transport.Msmq" Version="1.0.0-*" />
<PackageReference Include="NUnit" Version="3.7.*" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Messaging" />
<Reference Include="System.Transactions" />
<Reference Include="System.Configuration" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" PrivateAssets="All" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0-alpha1" PrivateAssets="All" />
<ProjectReference Include="..\AcceptanceTestHelper\AcceptanceTestHelper.csproj" />
<ProjectReference Include="..\TestHelper\TestHelper.csproj" />
<ProjectReference Include="..\ScriptBuilder\ScriptBuilder.csproj" />
<ProjectReference Include="..\SqlPersistence\SqlPersistence.csproj" />
<Compile Include="$(SolutionDir)\AcceptanceTestsHolder\App_Packages\**\*.cs" />
<Compile Include="$(SolutionDir)\AcceptanceTestsHolder\App_Packages\**\*.cs" Exclude="$(SolutionDir)\AcceptanceTestsHolder\App_Packages\**\ConfigureEndpointMsmqTransport.cs" />
</ItemGroup>
</Project>
6 changes: 5 additions & 1 deletion src/MsSqlAcceptanceTests/TestSuiteConstraints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

public partial class TestSuiteConstraints
{
#if NET452
public bool SupportsDtc => true;
#else
public bool SupportsDtc => false;
#endif
public bool SupportsCrossQueueTransactions => true;
public bool SupportsNativePubSub => false;
public bool SupportsNativeDeferral => false;
public bool SupportsOutbox => true;
public IConfigureEndpointTestExecution CreateTransportConfiguration()
{
return new ConfigureEndpointMsmqTransport();
return new ConfigureEndpointSqlServerTransport();
}

public IConfigureEndpointTestExecution CreatePersistenceConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class When_creating_new_transaction_scope_in_the_pipeline : NServiceBusAc
[Test]
public async Task Should_fail_when_creating_synchronized_storage_session()
{
Requires.DtcSupport();

var context = await Scenario.Define<Context>()
.WithEndpoint<Endpoint>(b => b
.When(async session =>
Expand Down Expand Up @@ -42,6 +44,7 @@ public Endpoint()
{
EndpointSetup<DefaultServer>(c =>
{
c.UseTransport<SqlServerTransport>().Transactions(TransportTransactionMode.TransactionScope);
c.Pipeline.Register(new TransactionScopeBehavior(), "Creates a new transaction scope");
c.Pipeline.Register(new SimulateFailureBehavior(), "Simulates failure before committing transport transaction");
});
Expand Down

This file was deleted.

Loading

0 comments on commit 3b234e1

Please sign in to comment.