Skip to content

Commit e72502c

Browse files
committed
NH-3436 - add failing test for LINQ query with contains operator running in multiple threads.
1 parent a52c918 commit e72502c

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading;
5+
using NHibernate.Cfg.MappingSchema;
6+
using NHibernate.Linq;
7+
using NHibernate.Mapping.ByCode;
8+
using NUnit.Framework;
9+
10+
namespace NHibernate.Test.NHSpecificTest.NH3436
11+
{
12+
public class Fixture : TestCaseMappingByCode
13+
{
14+
protected override HbmMapping GetMappings()
15+
{
16+
var mapper = new ModelMapper();
17+
mapper.Class<TestEntity>(rc =>
18+
{
19+
rc.Table("TestEntity");
20+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
21+
});
22+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
23+
}
24+
25+
protected override void OnSetUp()
26+
{
27+
using (var session = OpenSession())
28+
using (var transaction = session.BeginTransaction())
29+
{
30+
var e1 = new TestEntity();
31+
session.Save(e1);
32+
33+
var e2 = new TestEntity();
34+
session.Save(e2);
35+
36+
session.Flush();
37+
transaction.Commit();
38+
}
39+
}
40+
41+
protected override void OnTearDown()
42+
{
43+
using (var session = OpenSession())
44+
using (var transaction = session.BeginTransaction())
45+
{
46+
session.Delete("from System.Object");
47+
48+
session.Flush();
49+
transaction.Commit();
50+
}
51+
}
52+
53+
[Test]
54+
public void TestQueryWithContainsInParallel()
55+
{
56+
var ids = new List<Guid>
57+
{
58+
Guid.NewGuid(),
59+
Guid.NewGuid(),
60+
};
61+
const int threadsToRun = 32;
62+
var events = new WaitHandle[threadsToRun];
63+
var exceptions = new List<Exception>();
64+
for (var i = 0; i < threadsToRun; i++)
65+
{
66+
var @event = new ManualResetEvent(false);
67+
events[i] = @event;
68+
ThreadPool.QueueUserWorkItem(s =>
69+
{
70+
try
71+
{
72+
Run(ids);
73+
}
74+
catch (Exception ex)
75+
{
76+
exceptions.Add(ex);
77+
}
78+
finally
79+
{
80+
@event.Set();
81+
}
82+
});
83+
}
84+
WaitHandle.WaitAll(events);
85+
Assert.IsEmpty(exceptions);
86+
}
87+
88+
[Test]
89+
public void TestQueryWithContains()
90+
{
91+
var ids = new List<Guid>
92+
{
93+
Guid.NewGuid(),
94+
Guid.NewGuid(),
95+
};
96+
Run(ids);
97+
}
98+
99+
private void Run(ICollection<Guid> ids)
100+
{
101+
using (var session = sessions.OpenSession())
102+
using (session.BeginTransaction())
103+
{
104+
var result = session.Query<TestEntity>()
105+
.Where(entity => ids.Contains(entity.Id))
106+
.ToList();
107+
108+
Assert.That(result.Count, Is.EqualTo(0));
109+
}
110+
}
111+
112+
public class TestEntity
113+
{
114+
public virtual Guid Id { get; set; }
115+
}
116+
}
117+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,7 @@
10231023
<Compile Include="NHSpecificTest\NH3153\Fixture.cs" />
10241024
<Compile Include="MappingByCode\IntegrationTests\NH3105\Model.cs" />
10251025
<Compile Include="MappingByCode\IntegrationTests\NH3105\Fixture.cs" />
1026+
<Compile Include="NHSpecificTest\NH3436\Fixture.cs" />
10261027
<Compile Include="NHSpecificTest\NH941\Domain.cs" />
10271028
<Compile Include="NHSpecificTest\NH941\Fixture.cs" />
10281029
<Compile Include="NHSpecificTest\NH941\FixtureUsingList.cs" />

0 commit comments

Comments
 (0)