1+ using FluentAssertions ;
2+
3+ using System ;
4+ using System . Collections . Generic ;
5+ using System . IO ;
6+ using System . Linq ;
7+ using System . Text ;
8+ using System . Threading ;
9+ using System . Threading . Tasks ;
10+
11+ using Xunit ;
12+
13+ namespace LiteDB . Tests . Issues ;
14+
15+ public class Issue2471_Test
16+ {
17+ [ Fact ]
18+ public void TestFragmentDB_FindByIDException ( )
19+ {
20+ using var db = new LiteDatabase ( ":memory:" ) ;
21+ var collection = db . GetCollection < object > ( "fragtest" ) ;
22+
23+ var fragment = new object { } ;
24+ var id = collection . Insert ( fragment ) ;
25+
26+ id . Should ( ) . BeGreaterThan ( 0 ) ;
27+
28+ var frag2 = collection . FindById ( id ) ;
29+ frag2 . Should ( ) . NotBeNull ( ) ;
30+
31+ Action act = ( ) => db . Checkpoint ( ) ;
32+
33+ act . Should ( ) . NotThrow ( ) ;
34+ }
35+
36+ [ Fact ]
37+ public void MultipleReadCleansUpTransaction ( )
38+ {
39+ using var database = new LiteDatabase ( ":memory:" ) ;
40+
41+ var collection = database . GetCollection ( "test" ) ;
42+ collection . Insert ( new BsonDocument { [ "_id" ] = 1 } ) ;
43+
44+ for ( int i = 0 ; i < 500 ; i ++ )
45+ {
46+ collection . FindById ( 1 ) ;
47+ }
48+ }
49+
50+ #region Model
51+
52+ public class User
53+ {
54+ public int Id { get ; set ; }
55+ public string Name { get ; set ; }
56+ public int [ ] Phones { get ; set ; }
57+ public List < Address > Addresses { get ; set ; }
58+ }
59+
60+ public class Address
61+ {
62+ public string Street { get ; set ; }
63+ }
64+
65+ #endregion Model
66+
67+ // Copied from IndexMultiKeyIndex, but this time we ensure that the lock is released by calling db.Checkpoint()
68+ [ Fact ]
69+ public void Ensure_Query_GetPlan_Releases_Lock ( )
70+ {
71+ using var db = new LiteDatabase ( new MemoryStream ( ) ) ;
72+ var col = db . GetCollection < User > ( ) ;
73+
74+ col . Insert ( new User { Name = "John Doe" , Phones = new int [ ] { 1 , 3 , 5 } , Addresses = new List < Address > { new Address { Street = "Av.1" } , new Address { Street = "Av.3" } } } ) ;
75+ col . Insert ( new User { Name = "Joana Mark" , Phones = new int [ ] { 1 , 4 } , Addresses = new List < Address > { new Address { Street = "Av.3" } } } ) ;
76+
77+ // create indexes
78+ col . EnsureIndex ( x => x . Phones ) ;
79+ col . EnsureIndex ( x => x . Addresses . Select ( z => z . Street ) ) ;
80+
81+ // testing indexes expressions
82+ var indexes = db . GetCollection ( "$indexes" ) . FindAll ( ) . ToArray ( ) ;
83+
84+ indexes [ 1 ] [ "expression" ] . AsString . Should ( ) . Be ( "$.Phones[*]" ) ;
85+ indexes [ 2 ] [ "expression" ] . AsString . Should ( ) . Be ( "MAP($.Addresses[*]=>@.Street)" ) ;
86+
87+ // doing Phone query
88+ var queryPhone = col . Query ( )
89+ . Where ( x => x . Phones . Contains ( 3 ) ) ;
90+
91+ var planPhone = queryPhone . GetPlan ( ) ;
92+
93+ Action act = ( ) => db . Checkpoint ( ) ;
94+
95+ act . Should ( ) . NotThrow ( ) ;
96+ }
97+ }
0 commit comments