11using EfCoreTest ;
22using Microsoft . EntityFrameworkCore ;
33using Microsoft . Extensions . DependencyInjection ;
4+ using System . Runtime . InteropServices ;
45
56namespace EfCorePoolTest ;
67
7- internal sealed class BlogTest
8+ public sealed class BlogTest
89{
910 public BlogTest ( )
1011 {
@@ -19,57 +20,98 @@ public BlogTest()
1920
2021 #region Methods
2122
22- private async Task AddWithFactory ( )
23+ private async Task AddByScopeAsync ( long id , CancellationToken cancellationToken )
2324 {
24- await using var dbContext = await ServiceProvider . GetRequiredService < IDbContextFactory < TestDbContext > > ( )
25- . CreateDbContextAsync ( ) ;
25+ using var scope = ServiceProvider . CreateScope ( ) ;
26+ await using var dbContext = scope . ServiceProvider . GetRequiredService < TestDbContext > ( ) ;
27+ Console . WriteLine ( dbContext . Id ) ;
28+
29+ _ = await dbContext . Blogs . AddAsync ( new Blog ( id , "add1" , "test" ) , cancellationToken ) ;
2630
27- await dbContext . Blogs . AddAsync ( new Blog ( 1003 , "add3" , "test" ) ) ;
31+ await Task . Delay ( 100 , cancellationToken ) ;
2832
29- await dbContext . SaveChangesAsync ( ) ;
33+ _ = await dbContext . SaveChangesAsync ( cancellationToken ) ;
3034 }
3135
32- public async Task Add_Test ( )
36+ private async Task AddWithFactoryAsync ( CancellationToken cancellationToken = default )
3337 {
34- using var scope = ServiceProvider . CreateScope ( ) ;
35- await using var dbContext = scope . ServiceProvider . GetRequiredService < TestDbContext > ( ) ;
38+ await using var dbContext = await ServiceProvider . GetRequiredService < IDbContextFactory < TestDbContext > > ( )
39+ . CreateDbContextAsync ( cancellationToken ) ;
3640
37- await dbContext . Blogs . AddAsync ( new Blog ( 1001 , "add1" , "test" ) ) ;
38- await dbContext . Blogs . AddAsync ( new Blog ( 1002 , "add2" , "test" ) ) ;
41+ _ = await dbContext . Blogs . AddAsync ( new Blog ( 1003 , "add3" , "test" ) , cancellationToken ) ;
3942
40- await dbContext . SaveChangesAsync ( ) ;
43+ _ = await dbContext . SaveChangesAsync ( cancellationToken ) ;
4144 }
4245
43- public async Task Add_Thrice_Test ( )
46+ public async Task Add_TestAsync ( CancellationToken cancellationToken = default )
4447 {
45- var t1 = Add_Test ( ) ;
46- var t2 = Add_Test ( ) ;
48+ await AddByScopeAsync ( 1001 , cancellationToken ) ;
49+ }
4750
48- await Task . Delay ( 1_000 ) ;
51+ public async Task Add_Thrice_TestAsync ( CancellationToken cancellationToken = default )
52+ {
53+ // pool size 2
54+ var t1 = AddByScopeAsync ( 2001 , cancellationToken ) ;
55+ var t2 = AddByScopeAsync ( 2002 , cancellationToken ) ;
56+ var t3 = AddByScopeAsync ( 2003 , cancellationToken ) ;
4957
50- var t3 = Add_Test ( ) ;
58+ await Task . Delay ( 1_000 , cancellationToken ) ;
5159
52- await Task . WhenAll ( t1 , t2 , t3 ) ;
60+ var t4 = AddByScopeAsync ( 2004 , cancellationToken ) ;
61+ var t5 = AddByScopeAsync ( 2005 , cancellationToken ) ;
62+
63+ await Task . WhenAll ( t1 , t2 , t3 , t4 ) ;
5364 }
5465
55- public async Task IDbContextFactory_Test ( )
66+ public async Task IDbContextFactory_TestAsync ( CancellationToken cancellationToken = default )
5667 {
57- var t1 = AddWithFactory ( ) ;
58- var t2 = AddWithFactory ( ) ;
68+ var t1 = AddWithFactoryAsync ( cancellationToken ) ;
69+ var t2 = AddWithFactoryAsync ( cancellationToken ) ;
5970
60- await Task . Delay ( 1_000 ) ;
71+ await Task . Delay ( 1_000 , default ) ;
6172
62- var t3 = AddWithFactory ( ) ;
73+ var t3 = AddWithFactoryAsync ( cancellationToken ) ;
6374
6475 await Task . WhenAll ( t1 , t2 , t3 ) ;
6576 }
6677
67- public async Task Select_Test ( )
78+ public unsafe void ScopeInstance_Test ( )
79+ {
80+ using var scope = ServiceProvider . CreateScope ( ) ;
81+ var dbContext1 = scope . ServiceProvider . GetRequiredService < TestDbContext > ( ) ;
82+ var dbContext2 = scope . ServiceProvider . GetRequiredService < TestDbContext > ( ) ;
83+
84+ var handle1 = GCHandle . Alloc ( dbContext1 , GCHandleType . Normal ) ;
85+ var handle2 = GCHandle . Alloc ( dbContext2 , GCHandleType . Normal ) ;
86+ var ptr1 = GCHandle . ToIntPtr ( handle1 ) ;
87+ var ptr2 = GCHandle . ToIntPtr ( handle2 ) ;
88+
89+ // the variables location
90+ Console . WriteLine ( ptr1 ) ;
91+ Console . WriteLine ( ptr2 ) ;
92+
93+ handle2 . Free ( ) ;
94+ handle1 . Free ( ) ;
95+
96+ // the instance location in heap
97+ fixed ( long * p = & dbContext1 . Id )
98+ {
99+ Console . WriteLine ( * p ) ;
100+ }
101+ fixed ( long * p = & dbContext2 . Id )
102+ {
103+ Console . WriteLine ( * p ) ;
104+ }
105+
106+ Console . WriteLine ( ReferenceEquals ( dbContext1 , dbContext2 ) ) ;
107+ }
108+
109+ public async Task Select_TestAsync ( CancellationToken cancellationToken = default )
68110 {
69111 using var scope = ServiceProvider . CreateScope ( ) ;
70112 await using var dbContext = scope . ServiceProvider . GetRequiredService < TestDbContext > ( ) ;
71113
72- var list = await dbContext . Blogs . ToListAsync ( ) ;
114+ var list = await dbContext . Blogs . ToListAsync ( cancellationToken : cancellationToken ) ;
73115 }
74116
75117 #endregion
0 commit comments