Skip to content

Commit 724d1be

Browse files
committed
Cached query captures object in closure
1 parent 29d716d commit 724d1be

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (C) 2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System;
6+
using System.Collections;
7+
using System.Diagnostics;
8+
using System.Linq;
9+
using System.Linq.Dynamic;
10+
using System.Reflection;
11+
using System.Threading;
12+
using NUnit.Framework;
13+
using Xtensive.Caching;
14+
using Xtensive.Orm.Configuration;
15+
using Xtensive.Orm.Tests.Issues.Issue0628_ExecuteFutureScalarError_Model;
16+
17+
namespace Xtensive.Orm.Tests.Issues
18+
{
19+
namespace Issue_DelayedQueryCapture_Model
20+
{
21+
[HierarchyRoot]
22+
class Item : Entity
23+
{
24+
[Field, Key]
25+
public int Id { get; private set; }
26+
27+
[Field]
28+
public int Tag { get; set; }
29+
}
30+
}
31+
32+
[Serializable]
33+
public class Issue_DelayedQueryCapture : AutoBuildTest
34+
{
35+
public class OtherService
36+
{
37+
public static volatile int InstanceCount;
38+
39+
public int N;
40+
41+
public OtherService()
42+
{
43+
Interlocked.Increment(ref InstanceCount);
44+
}
45+
46+
~OtherService()
47+
{
48+
Interlocked.Decrement(ref InstanceCount);
49+
}
50+
}
51+
52+
53+
protected override DomainConfiguration BuildConfiguration()
54+
{
55+
var config = base.BuildConfiguration();
56+
config.Types.Register(typeof(Item).Assembly, typeof(Item).Namespace);
57+
return config;
58+
}
59+
60+
[Test]
61+
public void DelayedQueryCapture()
62+
{
63+
using (var session = Domain.OpenSession())
64+
using (var t = session.OpenTransaction()) {
65+
var item = new Item() { Tag = 10 };
66+
DelayedQuery(session);
67+
t.Complete();
68+
}
69+
GC.Collect();
70+
Thread.Sleep(1000);
71+
GC.Collect();
72+
Assert.AreEqual(0, OtherService.InstanceCount);
73+
}
74+
75+
private void DelayedQuery(Session session)
76+
{
77+
var ids = new[] { 1, 2 };
78+
var otherService = new OtherService();
79+
80+
var items = session.Query.CreateDelayedQuery(q =>
81+
from t in q.All<Item>()
82+
where t.Id.In(ids)
83+
select t).ToArray();
84+
85+
var bb1 = items
86+
.Select(a => new {
87+
a.Id,
88+
A = new {
89+
B = otherService.N == a.Id
90+
},
91+
});
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)