Skip to content

Commit 83603e0

Browse files
ngbrownfredericDelaporte
authored andcommitted
NH-4050 - Use Task.Run instead of BeginInvoke in Tests.
1 parent ba8db13 commit 83603e0

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Threading;
4+
using System.Threading.Tasks;
45
using NUnit.Framework;
56

67
namespace NHibernate.Test.NHSpecificTest.NH2192
@@ -73,7 +74,9 @@ public void HqlIsThreadsafe_UsingThreads()
7374
threads.ForEach(t => t.Join());
7475

7576
if (exceptions.Count > 0)
77+
{
7678
throw exceptions[0];
79+
}
7780

7881
results.ForEach(r => Assert.That(r, Is.EqualTo(2)));
7982
}
@@ -83,25 +86,29 @@ public void HqlIsThreadsafe_UsingPool()
8386
{
8487
List<Exception> exceptions = new List<Exception>();
8588
Func<int> result = FetchRowResults;
86-
List<IAsyncResult> results = new List<IAsyncResult>();
89+
List<Task<int>> tasks = new List<Task<int>>();
8790

88-
for (int i=0; i<_threadCount; i++)
89-
results.Add(result.BeginInvoke(null, null));
91+
for (int i = 0; i < _threadCount; i++)
92+
{
93+
tasks.Add(Task.Run<int>(result));
94+
}
9095

91-
results.ForEach(r =>
96+
tasks.ForEach(r =>
97+
{
98+
try
9299
{
93-
try
94-
{
95-
Assert.That(result.EndInvoke(r), Is.EqualTo(2));
96-
}
97-
catch (Exception e)
98-
{
99-
exceptions.Add(e);
100-
}
101-
});
100+
Assert.That(r.Result, Is.EqualTo(2));
101+
}
102+
catch (Exception e)
103+
{
104+
exceptions.Add(e);
105+
}
106+
});
102107

103108
if (exceptions.Count > 0)
109+
{
104110
throw exceptions[0];
111+
}
105112
}
106113

107114
private int FetchRowResults()

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Reflection;
55
using System.Threading;
6+
using System.Threading.Tasks;
67

78
using NHibernate.Engine.Query;
89
using NHibernate.Linq;
@@ -144,11 +145,11 @@ where personIds.Contains(person.Id)
144145
}
145146
};
146147

147-
var queryExecutorAsyncResult = queryExecutor.BeginInvoke(null, null);
148-
var cacheCleanerAsyncResult = cacheCleaner.BeginInvoke(null, null);
148+
var queryExecutorTask = Task.Run(queryExecutor);
149+
var cacheCleanerTask = Task.Run(cacheCleaner);
149150

150-
queryExecutor.EndInvoke(queryExecutorAsyncResult);
151-
cacheCleaner.EndInvoke(cacheCleanerAsyncResult);
151+
queryExecutorTask.Wait();
152+
cacheCleanerTask.Wait();
152153

153154
Assert.IsTrue(allLinqQueriesSucceeded);
154155
}

0 commit comments

Comments
 (0)