From ee3095b35e76dc57e0fd19c4d8d886015483d509 Mon Sep 17 00:00:00 2001 From: Jakub Konecki Date: Sun, 7 Jan 2024 06:43:04 +0000 Subject: [PATCH] Upgrade Orleans to version 8.0.0 (#150) Co-authored-by: Jakub Konecki Co-authored-by: Kyle Dodson --- Directory.Build.props | 2 +- global.json | 2 +- src/OrleansTestKit/OrleansTestKit.csproj | 10 +-- .../Services/TestServiceProvider.cs | 74 ++++++++++++++++++- .../Streams/TestStreamProviderManager.cs | 16 ++-- src/OrleansTestKit/TestKitSilo.cs | 3 +- .../OrleansTestKit.Tests.csproj | 4 +- 7 files changed, 90 insertions(+), 21 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index bc3d423..326fc54 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -17,7 +17,7 @@ enable latest enable - net7.0 + net8.0 latest-Recommended diff --git a/global.json b/global.json index 57d1c3a..4ec8993 100644 --- a/global.json +++ b/global.json @@ -2,6 +2,6 @@ "sdk": { "allowPrerelease": false, "rollForward": "latestFeature", - "version": "7.0.404" + "version": "8.0.100" } } diff --git a/src/OrleansTestKit/OrleansTestKit.csproj b/src/OrleansTestKit/OrleansTestKit.csproj index 2d1cb75..1940da3 100644 --- a/src/OrleansTestKit/OrleansTestKit.csproj +++ b/src/OrleansTestKit/OrleansTestKit.csproj @@ -1,4 +1,4 @@ - + @@ -12,7 +12,7 @@ Orleans Cloud-Computing Actor-Model Actors Distributed-Systems C# .NET Test Testing true snupkg - 4.0.0 + 8.0.0 @@ -24,9 +24,9 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + runtime; build; native; contentfiles; analyzers all diff --git a/src/OrleansTestKit/Services/TestServiceProvider.cs b/src/OrleansTestKit/Services/TestServiceProvider.cs index b7c21a6..8c98d0a 100644 --- a/src/OrleansTestKit/Services/TestServiceProvider.cs +++ b/src/OrleansTestKit/Services/TestServiceProvider.cs @@ -1,14 +1,18 @@ -using Moq; +using Microsoft.Extensions.DependencyInjection; +using Moq; +using Orleans.Streams; +using Orleans.TestKit.Streams; namespace Orleans.TestKit.Services; /// /// The test service provider /// -public sealed class TestServiceProvider : IServiceProvider +public sealed class TestServiceProvider : IServiceProvider, IKeyedServiceProvider { private readonly TestKitOptions _options; private readonly Dictionary _services = new(); + private readonly Dictionary<(object?, Type), object> _keyedServices = new(); /// /// Initializes a new instance of the class. @@ -35,6 +39,22 @@ public T AddService(T instance) return instance; } + /// + /// Adds or updates a keyed service to the provider + /// + /// The service type + /// The service key + /// The instance to add + /// The instance + /// Instance must be not null + public T AddKeyedService(string name, T instance) + { + ArgumentNullException.ThrowIfNull(instance); + + _keyedServices[(name, typeof(T))] = instance; + return instance; + } + /// /// Adds a mock to the service provider /// @@ -85,4 +105,54 @@ public object GetService(Type serviceType) return service; } } + + public object? GetKeyedService(Type serviceType, object? serviceKey) + { + ArgumentNullException.ThrowIfNull(serviceType); + + if (_keyedServices.TryGetValue((serviceKey, serviceType), out var service)) + { + return service; + } + + // If using strict service probes, throw the exception + if (_options.StrictServiceProbes) + { + throw new Exception($"Service probe does not exist for type {serviceType.Name} and key {serviceKey}. Ensure that it is added before the grain is tested."); + } + else + { + if (serviceType == typeof(IStreamProvider)) + { + service = new TestStreamProvider(_options); + } + else + { + // Create a new mock + if (Activator.CreateInstance(typeof(Mock<>).MakeGenericType(serviceType)) is not IMock mock) + { + throw new Exception($"Failed to instantiate {serviceType.Name}."); + } + + service = mock.Object; + } + + // Save the newly created grain for the next call + _keyedServices.Add((serviceKey, serviceType), service); + + return service; + } + } + + public object GetRequiredKeyedService(Type serviceType, object? serviceKey) + { + var service = GetKeyedService(serviceType, serviceKey); + + if (service is not object) + { + throw new Exception($"Service probe does not exist for type {serviceType.Name} and key {serviceKey}. Ensure that it is added before the grain is tested."); + } + + return service; + } } diff --git a/src/OrleansTestKit/Streams/TestStreamProviderManager.cs b/src/OrleansTestKit/Streams/TestStreamProviderManager.cs index 42b803a..689d41e 100644 --- a/src/OrleansTestKit/Streams/TestStreamProviderManager.cs +++ b/src/OrleansTestKit/Streams/TestStreamProviderManager.cs @@ -1,16 +1,21 @@ using Orleans.Runtime; using Orleans.Streams; +using Orleans.TestKit.Services; namespace Orleans.TestKit.Streams; -public sealed class TestStreamProviderManager : IKeyedServiceCollection +public sealed class TestStreamProviderManager { private readonly TestKitOptions _options; + private readonly TestServiceProvider _serviceProvider; private readonly Dictionary _streamProviders = new(); - public TestStreamProviderManager(TestKitOptions options) => + public TestStreamProviderManager(TestServiceProvider serviceProvider, TestKitOptions options) + { + _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); _options = options ?? throw new ArgumentNullException(nameof(options)); + } public TestStream AddStreamProbe(Guid streamId, string ns, string providerName) => AddStreamProbe(StreamId.Create(ns, streamId), providerName); @@ -44,17 +49,12 @@ public IStreamProvider GetProvider(string name) return Add(name); } - IStreamProvider IKeyedServiceCollection.GetService(IServiceProvider services, string key) => - GetProvider(key); - - IEnumerable> IKeyedServiceCollection.GetServices(IServiceProvider services) => - (IEnumerable>)_streamProviders; - private TestStreamProvider Add(string name) { var provider = new TestStreamProvider(_options); provider.Init(name).Wait(); _streamProviders.Add(name, provider); + _serviceProvider.AddKeyedService(name, provider); return provider; } diff --git a/src/OrleansTestKit/TestKitSilo.cs b/src/OrleansTestKit/TestKitSilo.cs index d80d433..f2d5d2f 100644 --- a/src/OrleansTestKit/TestKitSilo.cs +++ b/src/OrleansTestKit/TestKitSilo.cs @@ -45,8 +45,7 @@ public TestKitSilo() StorageManager = new StorageManager(Options); TimerRegistry = new TestTimerRegistry(); ReminderRegistry = new TestReminderRegistry(); - StreamProviderManager = new TestStreamProviderManager(Options); - ServiceProvider.AddService>(StreamProviderManager); + StreamProviderManager = new TestStreamProviderManager(ServiceProvider, Options); ServiceProvider.AddService(ReminderRegistry); GrainRuntime = new TestGrainRuntime(GrainFactory, TimerRegistry, ReminderRegistry, ServiceProvider, StorageManager); ServiceProvider.AddService(GrainRuntime); diff --git a/test/OrleansTestKit.Tests/OrleansTestKit.Tests.csproj b/test/OrleansTestKit.Tests/OrleansTestKit.Tests.csproj index 0c6497b..7c76e58 100644 --- a/test/OrleansTestKit.Tests/OrleansTestKit.Tests.csproj +++ b/test/OrleansTestKit.Tests/OrleansTestKit.Tests.csproj @@ -1,4 +1,4 @@ - + @@ -14,7 +14,7 @@ - +