|
5 | 5 |
|
6 | 6 | namespace Orleans.TestKit.Services; |
7 | 7 |
|
8 | | -/// <summary> |
9 | | -/// The test service provider |
10 | | -/// </summary> |
| 8 | +/// <summary>The test service provider</summary> |
11 | 9 | public sealed class TestServiceProvider : IServiceProvider, IKeyedServiceProvider |
12 | 10 | { |
| 11 | + private readonly Dictionary<(object?, Type), object> _keyedServices = new(); |
| 12 | + |
13 | 13 | private readonly TestKitOptions _options; |
| 14 | + |
14 | 15 | private readonly Dictionary<Type, object> _services = new(); |
15 | | - private readonly Dictionary<(object?, Type), object> _keyedServices = new(); |
16 | 16 |
|
17 | | - /// <summary> |
18 | | - /// Initializes a new instance of the <see cref="TestServiceProvider"/> class. |
19 | | - /// </summary> |
| 17 | + /// <summary>Initializes a new instance of the <see cref="TestServiceProvider"/> class.</summary> |
20 | 18 | /// <param name="options">The test kit options to use</param> |
21 | 19 | public TestServiceProvider(TestKitOptions options) |
22 | 20 | { |
23 | 21 | ArgumentNullException.ThrowIfNull(options); |
24 | 22 | _options = options; |
25 | 23 | } |
26 | 24 |
|
27 | | - /// <summary> |
28 | | - /// Adds or updates a service to the provider |
29 | | - /// </summary> |
| 25 | + /// <summary>Adds or updates a keyed service to the provider</summary> |
30 | 26 | /// <typeparam name="T">The service type</typeparam> |
| 27 | + /// <param name="serviceKey">The service key</param> |
31 | 28 | /// <param name="instance">The instance to add</param> |
32 | 29 | /// <returns>The instance</returns> |
33 | 30 | /// <exception cref="ArgumentNullException">Instance must be not null</exception> |
34 | | - public T AddService<T>(T instance) |
| 31 | + public T AddKeyedService<T>(object? serviceKey, T instance) |
35 | 32 | { |
36 | 33 | ArgumentNullException.ThrowIfNull(instance); |
37 | 34 |
|
38 | | - _services[typeof(T)] = instance; |
| 35 | + _keyedServices[(serviceKey, typeof(T))] = instance; |
39 | 36 | return instance; |
40 | 37 | } |
41 | 38 |
|
42 | | - /// <summary> |
43 | | - /// Adds or updates a keyed service to the provider |
44 | | - /// </summary> |
| 39 | + /// <summary>Adds or updates a keyed mock to the service provider</summary> |
| 40 | + /// <typeparam name="T">The underlying service type</typeparam> |
| 41 | + /// <param name="serviceKey">The service key</param> |
| 42 | + /// <returns>The newly created mock</returns> |
| 43 | + public Mock<T> AddKeyedServiceProbe<T>(object? serviceKey) |
| 44 | + where T : class => |
| 45 | + AddKeyedServiceProbe(serviceKey, new Mock<T>()); |
| 46 | + |
| 47 | + /// <summary>Adds or updates a keyed mock to the service provider</summary> |
| 48 | + /// <typeparam name="T">The underlying service type</typeparam> |
| 49 | + /// <param name="serviceKey">The service key</param> |
| 50 | + /// <param name="mock">The mock to add</param> |
| 51 | + /// <returns>The mock</returns> |
| 52 | + public Mock<T> AddKeyedServiceProbe<T>(object? serviceKey, Mock<T> mock) |
| 53 | + where T : class |
| 54 | + { |
| 55 | + AddKeyedService(serviceKey, mock.Object); |
| 56 | + return mock; |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary>Adds or updates a service to the provider</summary> |
45 | 60 | /// <typeparam name="T">The service type</typeparam> |
46 | | - /// <param name="name">The service key</param> |
47 | 61 | /// <param name="instance">The instance to add</param> |
48 | 62 | /// <returns>The instance</returns> |
49 | 63 | /// <exception cref="ArgumentNullException">Instance must be not null</exception> |
50 | | - public T AddKeyedService<T>(string name, T instance) |
| 64 | + public T AddService<T>(T instance) |
51 | 65 | { |
52 | 66 | ArgumentNullException.ThrowIfNull(instance); |
53 | 67 |
|
54 | | - _keyedServices[(name, typeof(T))] = instance; |
| 68 | + _services[typeof(T)] = instance; |
55 | 69 | return instance; |
56 | 70 | } |
57 | 71 |
|
58 | | - /// <summary> |
59 | | - /// Adds a mock to the service provider |
60 | | - /// </summary> |
| 72 | + /// <summary>Adds or updates a mock to the service provider</summary> |
61 | 73 | /// <typeparam name="T">The underlying service type</typeparam> |
62 | 74 | /// <param name="mock">The mock to add</param> |
63 | 75 | /// <returns>The mock</returns> |
64 | | - public Mock<T> AddServiceProbe<T>(Mock<T> mock) where T : class |
| 76 | + public Mock<T> AddServiceProbe<T>(Mock<T> mock) |
| 77 | + where T : class |
65 | 78 | { |
66 | 79 | AddService(mock.Object); |
67 | 80 | return mock; |
68 | 81 | } |
69 | 82 |
|
70 | | - /// <summary> |
71 | | - /// Adds a mock to the service provider |
72 | | - /// </summary> |
| 83 | + /// <summary>Adds a mock to the service provider</summary> |
73 | 84 | /// <typeparam name="T">The underlying service type</typeparam> |
74 | 85 | /// <returns>The newly created mock</returns> |
75 | | - public Mock<T> AddServiceProbe<T>() where T : class => AddServiceProbe(new Mock<T>()); |
76 | | - |
77 | | - /// <inheritdoc/> |
78 | | - public object GetService(Type serviceType) |
79 | | - { |
80 | | - ArgumentNullException.ThrowIfNull(serviceType); |
81 | | - |
82 | | - if (_services.TryGetValue(serviceType, out var service)) |
83 | | - { |
84 | | - return service; |
85 | | - } |
86 | | - |
87 | | - // If using strict service probes, throw the exception |
88 | | - if (_options.StrictServiceProbes) |
89 | | - { |
90 | | - throw new Exception($"Service probe does not exist for type {serviceType.Name}. Ensure that it is added before the grain is tested."); |
91 | | - } |
92 | | - else |
93 | | - { |
94 | | - // Create a new mock |
95 | | - if (Activator.CreateInstance(typeof(Mock<>).MakeGenericType(serviceType)) is not IMock<object> mock) |
96 | | - { |
97 | | - throw new Exception($"Failed to instantiate {serviceType.Name}."); |
98 | | - } |
99 | | - |
100 | | - service = mock.Object; |
101 | | - |
102 | | - // Save the newly created grain for the next call |
103 | | - _services.Add(serviceType, service); |
104 | | - |
105 | | - return service; |
106 | | - } |
107 | | - } |
| 86 | + public Mock<T> AddServiceProbe<T>() |
| 87 | + where T : class => |
| 88 | + AddServiceProbe(new Mock<T>()); |
108 | 89 |
|
109 | 90 | public object? GetKeyedService(Type serviceType, object? serviceKey) |
110 | 91 | { |
@@ -155,4 +136,36 @@ public object GetRequiredKeyedService(Type serviceType, object? serviceKey) |
155 | 136 |
|
156 | 137 | return service; |
157 | 138 | } |
| 139 | + |
| 140 | + /// <inheritdoc/> |
| 141 | + public object GetService(Type serviceType) |
| 142 | + { |
| 143 | + ArgumentNullException.ThrowIfNull(serviceType); |
| 144 | + |
| 145 | + if (_services.TryGetValue(serviceType, out var service)) |
| 146 | + { |
| 147 | + return service; |
| 148 | + } |
| 149 | + |
| 150 | + // If using strict service probes, throw the exception |
| 151 | + if (_options.StrictServiceProbes) |
| 152 | + { |
| 153 | + throw new Exception($"Service probe does not exist for type {serviceType.Name}. Ensure that it is added before the grain is tested."); |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + // Create a new mock |
| 158 | + if (Activator.CreateInstance(typeof(Mock<>).MakeGenericType(serviceType)) is not IMock<object> mock) |
| 159 | + { |
| 160 | + throw new Exception($"Failed to instantiate {serviceType.Name}."); |
| 161 | + } |
| 162 | + |
| 163 | + service = mock.Object; |
| 164 | + |
| 165 | + // Save the newly created grain for the next call |
| 166 | + _services.Add(serviceType, service); |
| 167 | + |
| 168 | + return service; |
| 169 | + } |
| 170 | + } |
158 | 171 | } |
0 commit comments