Skip to content

Commit c0767c3

Browse files
NH-4018 - Option for enlisting in system transaction.
1 parent f0bd141 commit c0767c3

29 files changed

+471
-50
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Diagnostics;
13+
using System.IO;
14+
using NUnit.Framework;
15+
16+
namespace NHibernate.Test.DynamicProxyTests
17+
{
18+
using System.Threading.Tasks;
19+
20+
/// <content>
21+
/// Contains generated async methods
22+
/// </content>
23+
24+
public partial class PeVerifier
25+
{
26+
27+
public async Task AssertIsValidAsync()
28+
{
29+
var process = new Process
30+
{
31+
StartInfo =
32+
{
33+
FileName = _peVerifyPath,
34+
RedirectStandardOutput = true,
35+
UseShellExecute = false,
36+
Arguments = "\"" + _assemlyLocation + "\" /VERBOSE",
37+
CreateNoWindow = true
38+
}
39+
};
40+
41+
process.Start();
42+
var processOutput = await (process.StandardOutput.ReadToEndAsync());
43+
process.WaitForExit();
44+
45+
var result = process.ExitCode + " code ";
46+
47+
if (process.ExitCode != 0)
48+
Assert.Fail("PeVerify reported error(s): " + Environment.NewLine + processOutput, result);
49+
}
50+
}
51+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.IO;
13+
using System.Reflection;
14+
using System.Reflection.Emit;
15+
using NUnit.Framework;
16+
using NHibernate.Proxy.DynamicProxy;
17+
18+
namespace NHibernate.Test.DynamicProxyTests
19+
{
20+
using System.Threading.Tasks;
21+
[TestFixture]
22+
public class PeVerifyFixtureAsync
23+
{
24+
private static bool wasCalled;
25+
26+
private const string assemblyName = "peVerifyAssembly";
27+
private const string assemblyFileName = "peVerifyAssembly.dll";
28+
29+
[Test]
30+
public Task VerifyClassWithPublicConstructorAsync()
31+
{
32+
try
33+
{
34+
var factory = new ProxyFactory(new SavingProxyAssemblyBuilder(assemblyName));
35+
var proxyType = factory.CreateProxyType(typeof(ClassWithPublicDefaultConstructor), null);
36+
37+
wasCalled = false;
38+
Activator.CreateInstance(proxyType);
39+
40+
Assert.That(wasCalled);
41+
return new PeVerifier(assemblyFileName).AssertIsValidAsync();
42+
}
43+
catch (Exception ex)
44+
{
45+
return Task.FromException<object>(ex);
46+
}
47+
}
48+
49+
[Test]
50+
public Task VerifyClassWithProtectedConstructorAsync()
51+
{
52+
try
53+
{
54+
var factory = new ProxyFactory(new SavingProxyAssemblyBuilder(assemblyName));
55+
var proxyType = factory.CreateProxyType(typeof(ClassWithProtectedDefaultConstructor), null);
56+
57+
wasCalled = false;
58+
Activator.CreateInstance(proxyType);
59+
60+
Assert.That(wasCalled);
61+
return new PeVerifier(assemblyFileName).AssertIsValidAsync();
62+
}
63+
catch (Exception ex)
64+
{
65+
return Task.FromException<object>(ex);
66+
}
67+
}
68+
69+
#region PeVerifyTypes
70+
71+
public class ClassWithPublicDefaultConstructor
72+
{
73+
public ClassWithPublicDefaultConstructor() { InitG<int>(1); }
74+
public ClassWithPublicDefaultConstructor(int unused) { }
75+
public virtual int Prop1 { get; set; }
76+
public virtual void InitG<T>(T value) { Init((int)(object)value); }
77+
public virtual void Init(int value) { Prop1 = value; if (Prop1 == 1) wasCalled = true; }
78+
}
79+
80+
public class ClassWithProtectedDefaultConstructor
81+
{
82+
protected ClassWithProtectedDefaultConstructor() { wasCalled = true; }
83+
}
84+
85+
public class ClassWithPrivateDefaultConstructor
86+
{
87+
private ClassWithPrivateDefaultConstructor() { wasCalled = true; }
88+
}
89+
90+
public class ClassWithNoDefaultConstructor
91+
{
92+
public ClassWithNoDefaultConstructor(int unused) { wasCalled = true; }
93+
public ClassWithNoDefaultConstructor(string unused) { wasCalled = true; }
94+
}
95+
96+
public class ClassWithInternalConstructor
97+
{
98+
internal ClassWithInternalConstructor() { wasCalled = true; }
99+
}
100+
101+
#endregion
102+
103+
#region ProxyFactory.IProxyAssemblyBuilder
104+
105+
public class SavingProxyAssemblyBuilder : IProxyAssemblyBuilder
106+
{
107+
private string assemblyName;
108+
109+
public SavingProxyAssemblyBuilder(string assemblyName)
110+
{
111+
this.assemblyName = assemblyName;
112+
}
113+
114+
public AssemblyBuilder DefineDynamicAssembly(AppDomain appDomain, AssemblyName name)
115+
{
116+
AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;
117+
return appDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), access, TestContext.CurrentContext.TestDirectory);
118+
}
119+
120+
public ModuleBuilder DefineDynamicModule(AssemblyBuilder assemblyBuilder, string moduleName)
121+
{
122+
return assemblyBuilder.DefineDynamicModule(moduleName, string.Format("{0}.mod", assemblyName), true);
123+
}
124+
125+
public void Save(AssemblyBuilder assemblyBuilder)
126+
{
127+
assemblyBuilder.Save(assemblyName + ".dll");
128+
}
129+
}
130+
131+
#endregion
132+
}
133+
}

src/NHibernate.Test/Async/NHSpecificTest/NH1632/Fixture.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ public async Task When_commiting_items_in_DTC_transaction_will_add_items_to_2nd_
100100
//closing the connection to ensure we can't really use it.
101101
var connection = await (Sfi.ConnectionProvider.GetConnectionAsync(CancellationToken.None));
102102
Sfi.ConnectionProvider.CloseConnection(connection);
103+
103104
// The session is supposed to succeed because the second level cache should have the
104105
// entity to load, allowing the session to not use the connection at all.
105-
// Will fail if transaction manager tries to enlist user supplied connection, due
106-
// to using a transaction scope below.
106+
// Will fail if a transaction manager tries to enlist user supplied connection. Do
107+
// not add a transaction scope below.
107108
using (var s = Sfi.WithOptions().Connection(connection).OpenSession())
108109
{
109110
Nums nums = null;

src/NHibernate.Test/Async/SessionBuilder/Fixture.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class FixtureAsync : TestCase
2424
{
2525
protected override string MappingsAssembly => "NHibernate.Test";
2626

27-
protected override IList Mappings => new [] { "SessionBuilder.Mappings.hbm.xml" };
27+
protected override IList Mappings => new[] { "SessionBuilder.Mappings.hbm.xml" };
2828

2929
protected override void Configure(Configuration configuration)
3030
{
@@ -37,12 +37,23 @@ private void CanSetAutoClose<T>(T sb) where T : ISessionBuilder<T>
3737
var options = DebugSessionFactory.GetCreationOptions(sb);
3838
CanSet(sb, sb.AutoClose, () => options.ShouldAutoClose,
3939
sb is ISharedSessionBuilder ssb ? ssb.AutoClose : default(Func<ISharedSessionBuilder>),
40-
// initial values
40+
// initial value
4141
false,
4242
// values
4343
true, false);
4444
}
4545

46+
private void CanSetAutoJoinTransaction<T>(T sb) where T : ISessionBuilder<T>
47+
{
48+
var options = DebugSessionFactory.GetCreationOptions(sb);
49+
CanSet(sb, sb.AutoJoinTransaction, () => options.ShouldAutoJoinTransaction,
50+
sb is ISharedSessionBuilder ssb ? ssb.AutoJoinTransaction : default(Func<ISharedSessionBuilder>),
51+
// initial value
52+
true,
53+
// values
54+
false, true);
55+
}
56+
4657
[Test]
4758
public async Task CanSetConnectionAsync()
4859
{
@@ -127,7 +138,7 @@ private void CanSetConnectionReleaseMode<T>(T sb) where T : ISessionBuilder<T>
127138
var options = DebugSessionFactory.GetCreationOptions(sb);
128139
CanSet(sb, sb.ConnectionReleaseMode, () => options.SessionConnectionReleaseMode,
129140
sb is ISharedSessionBuilder ssb ? ssb.ConnectionReleaseMode : default(Func<ISharedSessionBuilder>),
130-
// initial values
141+
// initial value
131142
Sfi.Settings.ConnectionReleaseMode,
132143
// values
133144
ConnectionReleaseMode.OnClose, ConnectionReleaseMode.AfterStatement, ConnectionReleaseMode.AfterTransaction);
@@ -138,7 +149,7 @@ private void CanSetFlushMode<T>(T sb) where T : ISessionBuilder<T>
138149
var options = DebugSessionFactory.GetCreationOptions(sb);
139150
CanSet(sb, sb.FlushMode, () => options.InitialSessionFlushMode,
140151
sb is ISharedSessionBuilder ssb ? ssb.FlushMode : default(Func<ISharedSessionBuilder>),
141-
// initial values
152+
// initial value
142153
Sfi.Settings.DefaultFlushMode,
143154
// values
144155
FlushMode.Always, FlushMode.Auto, FlushMode.Commit, FlushMode.Manual);

src/NHibernate.Test/Async/SystemTransactions/TransactionNotificationFixture.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,19 @@
88
//------------------------------------------------------------------------------
99

1010

11-
using System;
1211
using System.Collections;
13-
using System.Data.Common;
14-
using System.Threading;
1512
using System.Transactions;
1613
using NUnit.Framework;
1714

1815
namespace NHibernate.Test.SystemTransactions
1916
{
2017
using System.Threading.Tasks;
18+
using System.Threading;
2119
[TestFixture]
2220
public class TransactionNotificationFixtureAsync : TestCase
2321
{
2422
protected override IList Mappings
25-
{
26-
get { return new string[] {}; }
27-
}
23+
=> new string[] { };
2824

2925
[Test]
3026
public async Task TwoTransactionScopesInsideOneSessionAsync()

src/NHibernate.Test/DebugSessionFactory.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,12 @@ ISessionBuilder ISessionBuilder<ISessionBuilder>.AutoClose(bool autoClose)
441441
return this;
442442
}
443443

444+
ISessionBuilder ISessionBuilder<ISessionBuilder>.AutoJoinTransaction(bool autoJoinTransaction)
445+
{
446+
_actualBuilder.AutoJoinTransaction(autoJoinTransaction);
447+
return this;
448+
}
449+
444450
ISessionBuilder ISessionBuilder<ISessionBuilder>.FlushMode(FlushMode flushMode)
445451
{
446452
_actualBuilder.FlushMode(flushMode);
@@ -478,6 +484,12 @@ IStatelessSessionBuilder IStatelessSessionBuilder.Connection(DbConnection connec
478484
return this;
479485
}
480486

487+
IStatelessSessionBuilder IStatelessSessionBuilder.AutoJoinTransaction(bool autoJoinTransaction)
488+
{
489+
_actualBuilder.AutoJoinTransaction(autoJoinTransaction);
490+
return this;
491+
}
492+
481493
#endregion
482494
}
483495
}

src/NHibernate.Test/DynamicProxyTests/PeVerifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace NHibernate.Test.DynamicProxyTests
77
{
88
// utility class to run PEVerify.exe against a saved-to-disk assembly, similar to:
99
// http://stackoverflow.com/questions/7290893/is-there-an-api-for-verifying-the-msil-of-a-dynamic-assembly-at-runtime
10-
public class PeVerifier
10+
public partial class PeVerifier
1111
{
1212
private string _assemlyLocation;
1313
private string _peVerifyPath;

src/NHibernate.Test/NHSpecificTest/NH1054/DummyTransactionFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public void EnlistInSystemTransactionIfNeeded(ISessionImplementor session)
2222
throw new NotImplementedException();
2323
}
2424

25+
public void ExplicitJoinSystemTransaction(ISessionImplementor session)
26+
{
27+
throw new NotImplementedException();
28+
}
29+
2530
public bool IsInActiveSystemTransaction(ISessionImplementor session)
2631
{
2732
return false;

src/NHibernate.Test/NHSpecificTest/NH1632/Fixture.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ public void When_commiting_items_in_DTC_transaction_will_add_items_to_2nd_level_
105105
//closing the connection to ensure we can't really use it.
106106
var connection = Sfi.ConnectionProvider.GetConnection();
107107
Sfi.ConnectionProvider.CloseConnection(connection);
108+
108109
// The session is supposed to succeed because the second level cache should have the
109110
// entity to load, allowing the session to not use the connection at all.
110-
// Will fail if transaction manager tries to enlist user supplied connection, due
111-
// to using a transaction scope below.
111+
// Will fail if a transaction manager tries to enlist user supplied connection. Do
112+
// not add a transaction scope below.
112113
using (var s = Sfi.WithOptions().Connection(connection).OpenSession())
113114
{
114115
Nums nums = null;

0 commit comments

Comments
 (0)