|
1 |
| -namespace Carter.Tests |
2 |
| -{ |
3 |
| - using System; |
4 |
| - using System.Linq; |
5 |
| - using System.Net.Http; |
6 |
| - using System.Threading.Tasks; |
7 |
| - using Microsoft.AspNetCore.Hosting; |
8 |
| - using Microsoft.AspNetCore.Http; |
9 |
| - using Microsoft.AspNetCore.TestHost; |
10 |
| - using Microsoft.Extensions.DependencyInjection; |
11 |
| - using Xunit; |
12 |
| - |
13 |
| - public class ModuleLifetimeTests |
14 |
| - { |
15 |
| - /* |
16 |
| - |
17 |
| - * Transient objects are always different; a new instance is provided to every controller and every service. |
18 |
| -
|
19 |
| - * Scoped objects are the same within a request, but different across different requests. |
20 |
| -
|
21 |
| - * Singleton objects are the same for every object and every request. |
22 |
| - |
23 |
| - */ |
24 |
| - private TestServer server; |
25 |
| - |
26 |
| - private HttpClient httpClient; |
27 |
| - |
28 |
| - private void ConfigureServer() |
29 |
| - { |
30 |
| - this.server = new TestServer( |
31 |
| - new WebHostBuilder() |
32 |
| - .ConfigureServices(x => |
33 |
| - { |
34 |
| - x.AddScoped<ScopedRequestDependency>(); |
35 |
| - x.AddScoped<ScopedServiceDependency>(); |
36 |
| - x.AddTransient<TransientRequestDependency>(); |
37 |
| - x.AddTransient<TransientServiceDependency>(); |
38 |
| - x.AddCarter(configurator: c => |
39 |
| - c.WithModule<ScopedRequestDependencyModule>() |
40 |
| - .WithModule<TransientRequestDependencyModule>()); |
41 |
| - }) |
42 |
| - .Configure(x => x.UseCarter())); |
43 |
| - this.httpClient = this.server.CreateClient(); |
44 |
| - } |
45 |
| - |
46 |
| - [Fact] |
47 |
| - public async Task Should_resolve_new_module_foreach_request() |
48 |
| - { |
49 |
| - this.ConfigureServer(); |
50 |
| - var first = await this.httpClient.GetStringAsync("/instanceid"); |
51 |
| - var second = await this.httpClient.GetStringAsync("/instanceid"); |
52 |
| - |
53 |
| - Assert.NotEqual(first, second); |
54 |
| - } |
55 |
| - |
56 |
| - [Fact] |
57 |
| - public async Task Should_resolve_new_scoped_dependency_per_request() |
58 |
| - { |
59 |
| - this.ConfigureServer(); |
60 |
| - |
61 |
| - //Scoped uses the same instance per request so each value separated by a : should be the same |
62 |
| - var first = await this.httpClient.GetStringAsync("/scopedreqdep"); |
63 |
| - Assert.Single(first.Split(":").Distinct()); |
64 |
| - |
65 |
| - var second = await this.httpClient.GetStringAsync("/scopedreqdep"); |
66 |
| - Assert.Single(second.Split(":").Distinct()); |
67 |
| - |
68 |
| - Assert.NotEqual(first, second); |
69 |
| - } |
70 |
| - |
71 |
| - [Fact] |
72 |
| - public async Task Should_resolve_new_transient_dependency_per_request() |
73 |
| - { |
74 |
| - this.ConfigureServer(); |
75 |
| - |
76 |
| - //Transient uses a different instance in each object per request so each value separated by a : should be different |
77 |
| - var first = await this.httpClient.GetStringAsync("/transientreqdep"); |
78 |
| - Assert.Equal(2, first.Split(":").Distinct().Count()); |
79 |
| - |
80 |
| - var second = await this.httpClient.GetStringAsync("/transientreqdep"); |
81 |
| - Assert.Equal(2, second.Split(":").Distinct().Count()); |
82 |
| - |
83 |
| - Assert.NotEqual(first, second); |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - public class ScopedRequestDependency |
88 |
| - { |
89 |
| - private string instanceId; |
90 |
| - |
91 |
| - public ScopedRequestDependency() |
92 |
| - { |
93 |
| - this.instanceId = Guid.NewGuid().ToString(); |
94 |
| - } |
95 |
| - |
96 |
| - public string GetGuid() |
97 |
| - { |
98 |
| - return this.instanceId; |
99 |
| - } |
100 |
| - } |
101 |
| - |
102 |
| - public class ScopedServiceDependency |
103 |
| - { |
104 |
| - public ScopedServiceDependency(ScopedRequestDependency scopedRequestDependency) |
105 |
| - { |
106 |
| - this.Guid = scopedRequestDependency.GetGuid(); |
107 |
| - } |
108 |
| - |
109 |
| - public string Guid { get; set; } |
110 |
| - } |
111 |
| - |
112 |
| - public class TransientRequestDependency |
113 |
| - { |
114 |
| - private readonly string instanceId; |
115 |
| - |
116 |
| - public TransientRequestDependency() |
117 |
| - { |
118 |
| - this.instanceId = Guid.NewGuid().ToString(); |
119 |
| - } |
120 |
| - |
121 |
| - public string GetGuid() |
122 |
| - { |
123 |
| - return instanceId; |
124 |
| - } |
125 |
| - } |
126 |
| - |
127 |
| - public class TransientServiceDependency |
128 |
| - { |
129 |
| - public TransientServiceDependency(TransientRequestDependency transientRequestDependency) |
130 |
| - { |
131 |
| - this.TheGuid = transientRequestDependency.GetGuid(); |
132 |
| - } |
133 |
| - |
134 |
| - public string TheGuid { get; } |
135 |
| - } |
136 |
| - |
137 |
| - public class ScopedRequestDependencyModule : CarterModule |
138 |
| - { |
139 |
| - private readonly Guid instanceId; |
140 |
| - |
141 |
| - public ScopedRequestDependencyModule(ScopedRequestDependency scopedRequestDependency, |
142 |
| - ScopedServiceDependency scopedServiceDependency) |
143 |
| - { |
144 |
| - this.instanceId = Guid.NewGuid(); |
145 |
| - this.Get("/scopedreqdep", |
146 |
| - ctx => ctx.Response.WriteAsync(scopedRequestDependency.GetGuid() + ":" + scopedServiceDependency.Guid)); |
147 |
| - this.Get("/instanceid", ctx => |
148 |
| - ctx.Response.WriteAsync(this.instanceId.ToString())); |
149 |
| - } |
150 |
| - } |
151 |
| - |
152 |
| - public class TransientRequestDependencyModule : CarterModule |
153 |
| - { |
154 |
| - public TransientRequestDependencyModule(TransientRequestDependency transientRequestDependency, |
155 |
| - TransientServiceDependency transientServiceDependency) |
156 |
| - { |
157 |
| - this.Get("/transientreqdep", |
158 |
| - ctx => ctx.Response.WriteAsync(transientRequestDependency.GetGuid() + ":" + |
159 |
| - transientServiceDependency.TheGuid)); |
160 |
| - } |
161 |
| - } |
162 |
| -} |
| 1 | +namespace Carter.Tests.DependencyInjection |
| 2 | +{ |
| 3 | + using System.Linq; |
| 4 | + using System.Net.Http; |
| 5 | + using System.Threading.Tasks; |
| 6 | + using Microsoft.AspNetCore.Hosting; |
| 7 | + using Microsoft.AspNetCore.TestHost; |
| 8 | + using Microsoft.Extensions.DependencyInjection; |
| 9 | + using Xunit; |
| 10 | + |
| 11 | + public class ModuleLifetimeTests |
| 12 | + { |
| 13 | + /* |
| 14 | + |
| 15 | + * Transient objects are always different; a new instance is provided to every controller and every service. |
| 16 | +
|
| 17 | + * Scoped objects are the same within a request, but different across different requests. |
| 18 | +
|
| 19 | + * Singleton objects are the same for every object and every request. |
| 20 | + |
| 21 | + */ |
| 22 | + private TestServer server; |
| 23 | + |
| 24 | + private HttpClient httpClient; |
| 25 | + |
| 26 | + private void ConfigureServer() |
| 27 | + { |
| 28 | + this.server = new TestServer( |
| 29 | + new WebHostBuilder() |
| 30 | + .ConfigureServices(x => |
| 31 | + { |
| 32 | + x.AddScoped<ScopedRequestDependency>(); |
| 33 | + x.AddScoped<ScopedServiceDependency>(); |
| 34 | + x.AddTransient<TransientRequestDependency>(); |
| 35 | + x.AddTransient<TransientServiceDependency>(); |
| 36 | + x.AddCarter(configurator: c => |
| 37 | + c.WithModule<ScopedRequestDependencyModule>() |
| 38 | + .WithModule<TransientRequestDependencyModule>()); |
| 39 | + }) |
| 40 | + .Configure(x => x.UseCarter())); |
| 41 | + this.httpClient = this.server.CreateClient(); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public async Task Should_resolve_new_module_foreach_request() |
| 46 | + { |
| 47 | + this.ConfigureServer(); |
| 48 | + var first = await this.httpClient.GetStringAsync("/instanceid"); |
| 49 | + var second = await this.httpClient.GetStringAsync("/instanceid"); |
| 50 | + |
| 51 | + Assert.NotEqual(first, second); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public async Task Should_resolve_new_scoped_dependency_per_request() |
| 56 | + { |
| 57 | + this.ConfigureServer(); |
| 58 | + |
| 59 | + //Scoped uses the same instance per request so each value separated by a : should be the same |
| 60 | + var first = await this.httpClient.GetStringAsync("/scopedreqdep"); |
| 61 | + Assert.Single(first.Split(":").Distinct()); |
| 62 | + |
| 63 | + var second = await this.httpClient.GetStringAsync("/scopedreqdep"); |
| 64 | + Assert.Single(second.Split(":").Distinct()); |
| 65 | + |
| 66 | + Assert.NotEqual(first, second); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task Should_resolve_new_transient_dependency_per_request() |
| 71 | + { |
| 72 | + this.ConfigureServer(); |
| 73 | + |
| 74 | + //Transient uses a different instance in each object per request so each value separated by a : should be different |
| 75 | + var first = await this.httpClient.GetStringAsync("/transientreqdep"); |
| 76 | + Assert.Equal(2, first.Split(":").Distinct().Count()); |
| 77 | + |
| 78 | + var second = await this.httpClient.GetStringAsync("/transientreqdep"); |
| 79 | + Assert.Equal(2, second.Split(":").Distinct().Count()); |
| 80 | + |
| 81 | + Assert.NotEqual(first, second); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments