コード例 #1
0
        internal static async Task <UserActor.UserActor> CreateUserActorAsync(ActorId id, IActorFactory actorFactory     = null,
                                                                              IServiceFactory serviceFactory             = null, IUsersServiceProxy usersServiceProxy = null,
                                                                              IInvoicesServiceProxy invoicesServiceProxy = null, bool invokeOnActivate                = true)
        {
            if (actorFactory == null)
            {
                actorFactory = new Mock <IActorFactory>().Object;
            }
            if (serviceFactory == null)
            {
                serviceFactory = new Mock <IServiceFactory>().Object;
            }
            if (usersServiceProxy == null)
            {
                usersServiceProxy = new Mock <IUsersServiceProxy>().Object;
            }
            if (invoicesServiceProxy == null)
            {
                invoicesServiceProxy = new Mock <IInvoicesServiceProxy>().Object;
            }

            Func <ActorService, ActorId, ActorBase> factory =
                (service, actorId) => new UserActor.UserActor(service, id, actorFactory, serviceFactory, usersServiceProxy, invoicesServiceProxy);

            var svc = MockActorServiceFactory.CreateActorServiceForActor <UserActor.UserActor>(factory);

            var actor = svc.Activate(id);

            if (invokeOnActivate)
            {
                await actor.InvokeOnActivateAsync();
            }

            return(actor);
        }
コード例 #2
0
        public static TestContext WithHappyPath()
        {
            var testContext = new TestContext();

            testContext.TelemetryClient = new TelemetryClient(new TelemetryConfiguration("", testContext.MockTelemetryChannel));
            testContext.Actors          = Enumerable.Range(0, 10).Select(x => Guid.NewGuid()).Select(x =>
            {
                var actorProxyFactory       = new MockActorProxyFactory();
                var mockServiceProxyFactory = new MockServiceProxyFactory();
                var actorServiceForActor    = MockActorServiceFactory.CreateActorServiceForActor <Instance>();
                var instance = new Instance(actorServiceForActor, new ActorId(x), testContext.ClusterClient.Object,
                                            testContext.TelemetryClient);
                return(instance);
            }).ToList();

            var setupSequentialResult = testContext.GuidGetter.SetupSequence(x => x.GetAGuid());

            foreach (var actor in testContext.Actors)
            {
                setupSequentialResult.Returns(actor.Id.GetGuidId());
                testContext.MockActorProxyFactory.RegisterActor(actor);
            }
            testContext.PoolActorService = CreatePoolActorService(testContext.TelemetryClient, testContext.MockActorProxyFactory,
                                                                  testContext.GuidGetter.Object);
            testContext.Pool = testContext.PoolActorService.Activate(new ActorId("fabric:/myapplicationname/myservicetypename"));
            return(testContext);
        }
コード例 #3
0
        public async Task TestMultipleActors()
        {
            Func <ActorService, ActorId, ActorBase> actorFactory = (service, actorId) => new MyStatefulActor(service, actorId);
            var svc    = MockActorServiceFactory.CreateActorServiceForActor <MyStatefulActor>(actorFactory);
            var actor1 = svc.Activate(new ActorId(Guid.NewGuid()));
            var actor2 = svc.Activate(new ActorId(Guid.NewGuid()));

            var stateManager1 = (MockActorStateManager)actor1.StateManager;
            var stateManager2 = (MockActorStateManager)actor2.StateManager;

            const string stateName = "test";
            var          payload1  = new Payload(StatePayload);
            var          payload2  = new Payload(OtherStatePayload);

            //create states
            await actor1.InsertAsync(stateName, payload1);

            await actor2.InsertAsync(stateName, payload2);

            //get states
            var actual1 = await stateManager1.GetStateAsync <Payload>(stateName);

            Assert.AreEqual(StatePayload, actual1.Content);
            var actual2 = await stateManager2.GetStateAsync <Payload>(stateName);

            Assert.AreEqual(OtherStatePayload, actual2.Content);
        }
コード例 #4
0
        public void TestMultipleActorsForSingleActorId()
        {
            var sharedId = new ActorId(Guid.NewGuid());

            //mock out the called actors
            var statefulActorSvc = MockActorServiceFactory.CreateActorServiceForActor <MockTestStatefulActor>(
                (service, actorId) => new MockTestStatefulActor(service, actorId));
            var reminderActorService = MockActorServiceFactory.CreateActorServiceForActor <MockReminderTimerActor>(
                (service, actorId) => new MockReminderTimerActor(service, actorId));

            var statefulActor = statefulActorSvc.Activate(sharedId);
            var reminderActor = reminderActorService.Activate(sharedId);

            //prepare the service:
            var mockProxyFactory = new MockActorProxyFactory();

            mockProxyFactory.RegisterActor(statefulActor);
            mockProxyFactory.RegisterActor(reminderActor);

            //act:
            var a1 = mockProxyFactory.CreateActorProxy <IMyStatefulActor>(sharedId);
            var a2 = mockProxyFactory.CreateActorProxy <IReminderTimerActor>(sharedId);

            //assert:
            Assert.AreSame(typeof(MockTestStatefulActor), a1.GetType());
            Assert.AreSame(typeof(MockReminderTimerActor), a2.GetType());
        }
コード例 #5
0
        public async Task TestServiceProxyFactory()
        {
            //mock out the called service

            var mockProxyFactory = new MockActorProxyFactory();

            mockProxyFactory.MissingActor += MockProxyFactory_MisingActorId;


            //prepare the actor:
            Func <ActorService, ActorId, ActorBase> actorFactory = (service, actorId) => new ActorCallerActor(service, actorId, mockProxyFactory);
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <ActorCallerActor>(actorFactory);
            var actor = svc.Activate(ActorId.CreateRandom());

            //act:
            await actor.InsertAsync("test", new Payload("some other value"));

            //check if the other actor was called
            var statefulActorId = await actor.StateManager.GetStateAsync <ActorId>(ActorCallerActor.ChildActorIdKeyName);

            var statefulActor = mockProxyFactory.CreateActorProxy <IMyStatefulActor>(ActorCallerActor.CalledServiceName, statefulActorId);

            var payload = await((MyStatefulActor)statefulActor).StateManager.GetStateAsync <Payload>("test");

            //assert:
            Assert.AreEqual("some other value", payload.Content);
        }
コード例 #6
0
        internal static async Task <VehicleActor.VehicleActor> CreateVehicleActorAsync(ActorId id, IActorFactory actorFactory = null,
                                                                                       IServiceFactory serviceFactory         = null, IVehiclesServiceProxy vehiclesServiceProxy = null,
                                                                                       bool invokeOnActivate = true)
        {
            if (actorFactory == null)
            {
                actorFactory = new Mock <IActorFactory>().Object;
            }
            if (serviceFactory == null)
            {
                serviceFactory = new Mock <IServiceFactory>().Object;
            }
            if (vehiclesServiceProxy == null)
            {
                vehiclesServiceProxy = new Mock <IVehiclesServiceProxy>().Object;
            }

            Func <ActorService, ActorId, ActorBase> factory =
                (service, actorId) => new VehicleActor.VehicleActor(service, id, actorFactory, serviceFactory, vehiclesServiceProxy);

            var svc = MockActorServiceFactory.CreateActorServiceForActor <VehicleActor.VehicleActor>(factory);

            var actor = svc.Activate(id);

            if (invokeOnActivate)
            {
                await actor.InvokeOnActivateAsync();
            }

            return(actor);
        }
コード例 #7
0
        public async Task TestSubscribe_Doesnt_CrashAsync()
        {
            //var service = new CustomActorService(MockStatefulServiceContextFactory.Default, ActorTypeInformation.Get(typeof(MyStatefulActor)));
            //var factory = new MockActorServiceRemotingClientFactory(service);
            //var proxyFactory = new ActorProxyFactory(callbackClient => factory)

            var guid = Guid.NewGuid();
            var id   = new ActorId(guid);
            Func <ActorService, ActorId, ActorBase> factory = (service, actorId) => new ExampleActorMock(service, actorId);
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <ExampleActorMock>(factory);
            var actor = svc.Activate(id);

            var mockProxyFactory = new MockActorProxyFactory();

            mockProxyFactory.RegisterActor(actor);

            var eventSubscriptionHelper = new MockActorEventSubscriptionHelper();
            var exampleService          = new ExampleClient(MockStatefulServiceContextFactory.Default, new MockReliableStateManager(),
                                                            eventSubscriptionHelper, mockProxyFactory);
            await exampleService.DoSomething(guid, "message text");

            Assert.IsTrue(eventSubscriptionHelper.IsSubscribed <IExampleEvents>(exampleService));
            Assert.IsFalse(IsSuccess);
            //Subscribe doesn't crash the test, but the Event is not really fired and processed at this time
        }
コード例 #8
0
        private static MyStatefulActor CreateActor(ActorId id)
        {
            Func <ActorService, ActorId, ActorBase> actorFactory = (service, actorId) => new MyStatefulActor(service, id);
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <MyStatefulActor>(actorFactory);
            var actor = svc.Activate(id);

            return(actor);
        }
コード例 #9
0
        private static EventHandlerActor.EventHandlerActor CreateEventHandlerActor(ActorId id, IBigBrother bigBrother)
        {
            ActorBase ActorFactory(ActorService service, ActorId actorId) => new EventHandlerActor.EventHandlerActor(service, id, new Mock <IEventHandlerFactory>().Object, bigBrother);

            var svc   = MockActorServiceFactory.CreateActorServiceForActor <EventHandlerActor.EventHandlerActor>(ActorFactory);
            var actor = svc.Activate(id);

            return(actor);
        }
コード例 #10
0
 public void TestAnotherCustomActorService_CreateFails()
 {
     //an ActorService with a NON standard constructor can be created by the MockActorServiceFactory
     Assert.ThrowsException <InvalidOperationException>(() =>
     {
         var customActorService =
             MockActorServiceFactory.CreateCustomActorServiceForActor <AnotherCustomActorService, OnActivateActor>();
     });
 }
コード例 #11
0
        private void actorProxyFactory_MissingActor(object sender, MissingActorEventArgs args)
        {
            var registrar = (MockActorProxyFactory)sender;
            Func <ActorService, ActorId, ActorBase> actorFactory = (service, actorId) => new ApprovalActor(service, actorId, _actorProxyFactory, _serviceProxyFactory, _factories);
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <ApprovalActor>(actorFactory);
            var actor = svc.Activate(args.Id);

            registrar.RegisterActor(actor);
        }
コード例 #12
0
        private static IEventHandlerActor CreateMockEventHandlerActor(ActorId id)
        {
            ActorBase ActorFactory(ActorService service, ActorId actorId) => new MockEventHandlerActor(service, id);

            var svc   = MockActorServiceFactory.CreateActorServiceForActor <MockEventHandlerActor>(ActorFactory);
            var actor = svc.Activate(id);

            return(actor);
        }
コード例 #13
0
        public async Task InvokeOnDeactivateAsyncTest()
        {
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <InvokeOnActor>();
            var actor = svc.Activate(new ActorId(Guid.NewGuid()));

            await actor.InvokeOnDeactivateAsync();

            Assert.IsTrue(actor.OnDeactivateCalled);
        }
コード例 #14
0
        public void TestCustomActorServiceActivate()
        {
            //an ActorService with a standard constructor can be created by the MockActorServiceFactory
            var customActorService = MockActorServiceFactory.CreateCustomActorServiceForActor <CustomActorService, InvokeOnActor>();
            var actor = customActorService.Activate <InvokeOnActor>(new ActorId(123L));

            Assert.IsInstanceOfType(customActorService, typeof(CustomActorService));
            Assert.IsInstanceOfType(actor, typeof(InvokeOnActor));
            Assert.AreEqual(123L, actor.Id.GetLongId());
        }
コード例 #15
0
 public void TestAnotherCustomActorService_CreateFails()
 {
     //an ActorService with a NON standard constructor can be created by the MockActorServiceFactory
     Assert.ThrowsException <InvalidOperationException>(() =>
     {
         // ReSharper disable once UnusedVariable
         var customActorService =
             MockActorServiceFactory.CreateCustomActorServiceForActor <AnotherCustomActorService, InvokeOnActor>();
     });
 }
コード例 #16
0
        public async Task InvokeOnPostActorMethodAsyncTest()
        {
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <InvokeOnActor>();
            var actor = svc.Activate(new ActorId(Guid.NewGuid()));

            var context = MockActorMethodContextFactory.CreateForTimer(nameof(actor.ActorOperation));
            await actor.InvokeOnPostActorMethodAsync(context);

            Assert.IsTrue(actor.OnPostActorMethodCalled);
        }
コード例 #17
0
        private async Task <T> CreateActorConditional <T>(ActorId id, Func <ActorService, ActorId, ActorBase> funcOverride)
            where T : Actor, IActor
        {
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <T>(funcOverride);
            var actor = svc.Activate <T>(id);
            await actor.InvokeOnActivateAsync();

            _actorProxyFactory.RegisterActor(actor);
            return(actor);
        }
コード例 #18
0
        public void TestDefault()
        {
            var instance = MockActorServiceFactory.CreateActorServiceForActor <MockActor>();

            Assert.IsInstanceOfType(instance, typeof(MockActorService <MockActor>));
            Assert.AreEqual(MockStatefulServiceContextFactory.Default, instance.Context);
            Assert.AreEqual(ActorTypeInformation.Get(typeof(MockActor)).ImplementationType, instance.ActorTypeInformation.ImplementationType);
            Assert.IsNotNull(instance.StateProvider);
            Assert.IsNotNull(instance.Settings);
        }
コード例 #19
0
        public async Task CustomStatefulActor_ShouldHaveUniqueStateManagerPerId()
        {
            Dictionary <ActorId, IActorStateManager> stateManagerMap = new Dictionary <ActorId, IActorStateManager>();

            Func <ActorBase, IActorStateProvider, IActorStateManager> stateManagerFactory = (actr, stateProvider) =>
            {
                if (!stateManagerMap.TryGetValue(actr.Id, out var actorStateManager))
                {
                    actorStateManager        = new MockActorStateManager();
                    stateManagerMap[actr.Id] = actorStateManager;
                }
                return(actorStateManager);
            };

            // Every actor instance has its own state manager.
            Func <ActorService, ActorId, ActorBase> actorFactory = (service, actorId) => new MyStatefulActor(service, actorId);
            var customActorService = MockActorServiceFactory.CreateCustomActorServiceForActor <CustomActorService, MyStatefulActor>(actorFactory, stateManagerFactory: stateManagerFactory);

            var id1           = ActorId.CreateRandom();
            var id2           = ActorId.CreateRandom();
            var actor1        = customActorService.Activate <MyStatefulActor>(id1);
            var stateManager1 = (MockActorStateManager)actor1.StateManager;

            var actor2        = customActorService.Activate <MyStatefulActor>(id2);
            var stateManager2 = (MockActorStateManager)actor2.StateManager;

            var actor1_2        = customActorService.Activate <MyStatefulActor>(id1);
            var stateManager1_2 = (MockActorStateManager)actor1_2.StateManager;


            const string stateName   = "test";
            const string payloadText = "foo";
            var          payload     = new Payload(payloadText);

            //create state
            await actor1.InsertAsync(stateName, payload);

            //get state
            var actualState1 = await stateManager1.GetStateAsync <Payload>(stateName);

            Assert.AreEqual(payloadText, actualState1.Content);

            var actualState2 = await stateManager2.TryGetStateAsync <Payload>(stateName);

            Assert.IsFalse(actualState2.HasValue);

            var actualState1_2 = await stateManager1_2.GetStateAsync <Payload>(stateName);

            Assert.AreEqual(payloadText, actualState1_2.Content);

            Assert.AreNotSame(stateManager1, stateManager2);
            Assert.AreSame(stateManager1, stateManager1_2);
        }
コード例 #20
0
        public void TestActorMethodContexts()
        {
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <InvokeOnActor>();
            var actor = svc.Activate(new ActorId(Guid.NewGuid()));

            var context = MockActorMethodContextFactory.CreateForActor(nameof(actor.ActorOperation));

            Assert.IsInstanceOfType(context, typeof(ActorMethodContext));
            context = MockActorMethodContextFactory.CreateForTimer(nameof(actor.ActorOperation));
            Assert.IsInstanceOfType(context, typeof(ActorMethodContext));
            context = MockActorMethodContextFactory.CreateForReminder(nameof(actor.ActorOperation));
            Assert.IsInstanceOfType(context, typeof(ActorMethodContext));
        }
コード例 #21
0
        private static void MockProxyFactory_MisingActorId(object sender, MissingActorEventArgs args)
        {
            if (args.ActorType != typeof(IMyStatefulActor))
            {
                return;
            }

            Func <ActorService, ActorId, ActorBase> actorFactory = (service, actorId) => new MyStatefulActor(service, actorId);
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <MyStatefulActor>(actorFactory);
            var actor = svc.Activate(args.Id);

            args.ActorInstance = actor;
        }
コード例 #22
0
        public async Task TestActorTimerRegistration()
        {
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <ReminderTimerActor>();
            var actor = svc.Activate(new ActorId(Guid.NewGuid()));

            //setup
            await actor.RegisterTimerAsync();

            //assert
            var  timers   = actor.GetActorTimers(); //extension method
            bool hasTimer = timers.Any();

            Assert.IsTrue(hasTimer);
        }
コード例 #23
0
        public void TestCustomContext()
        {
            var newUri          = new Uri("fabric:/MockApp/OtherMockStatefulService");
            var serviceTypeName = "OtherMockServiceType";
            var partitionId     = Guid.NewGuid();
            var replicaId       = long.MaxValue;
            var context         = new MockCodePackageActivationContext("fabric:/MyApp", "MyAppType", "Code", "Ver", "Context", "Log", "Temp", "Work", "Man", "ManVer");

            var serviceContext = MockStatefulServiceContextFactory.Create(context, serviceTypeName, newUri, partitionId, replicaId);

            var instance = MockActorServiceFactory.CreateActorServiceForActor <MockActor>(context: serviceContext);

            Assert.IsInstanceOfType(instance, typeof(MockActorService <MockActor>));
            Assert.AreEqual(serviceContext, instance.Context);
        }
コード例 #24
0
        public async Task TestActorReminderRegistration()
        {
            var    svc          = MockActorServiceFactory.CreateActorServiceForActor <ReminderTimerActor>();
            var    actor        = svc.Activate(new ActorId(Guid.NewGuid()));
            string reminderName = "reminder";

            //setup
            await actor.RegisterReminderAsync(reminderName);

            //assert
            var  reminderCollection = actor.GetActorReminders();
            bool hasReminder        = reminderCollection.Any(r => string.Equals(r.Name, reminderName));

            Assert.IsTrue(hasReminder);
        }
コード例 #25
0
        internal static InvoiceActor CreateActor(ActorId id, IActorFactory actorFactory = null, IServiceFactory serviceFactory = null)
        {
            if (actorFactory == null)
            {
                actorFactory = new Mock <IActorFactory>().Object;
            }
            if (serviceFactory == null)
            {
                serviceFactory = new Mock <IServiceFactory>().Object;
            }

            Func <ActorService, ActorId, ActorBase> factory =
                (service, actorId) => new InvoiceActor(service, id, actorFactory, serviceFactory);
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <InvoiceActor>(factory);
            var actor = svc.Activate(id);

            return(actor);
        }
コード例 #26
0
        public async Task TestServiceProxyFactory()
        {
            //mock out the called service
            var mockProxyFactory = new MockServiceProxyFactory();
            var mockService      = new MockTestStatefulService();

            mockProxyFactory.RegisterService(ServiceCallerActor.CalledServiceName, mockService);

            //prepare the actor:
            Func <ActorService, ActorId, ActorBase> actorFactory = (service, actorId) => new ServiceCallerActor(service, actorId, mockProxyFactory);
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <ServiceCallerActor>(actorFactory);
            var actor = svc.Activate(ActorId.CreateRandom());

            //act:
            await actor.InsertAsync("test", new Payload("some other value"));

            //assert:
            Assert.IsTrue(mockService.InsertAsyncCalled);
        }
コード例 #27
0
        public async Task TestActorProxyFactory()
        {
            //mock out the called actor
            var id = new ActorId(ActorCallerService.CalledActorId);
            Func <ActorService, ActorId, ActorBase> actorFactory = (service, actorId) => new MockTestStatefulActor(service, id);
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <MockTestStatefulActor>(actorFactory);
            var actor = svc.Activate(id);

            //prepare the service:
            var mockProxyFactory = new MockActorProxyFactory();

            mockProxyFactory.RegisterActor(actor);
            var serviceInstance = new ActorCallerService(MockStatelessServiceContextFactory.Default, mockProxyFactory);

            //act:
            await serviceInstance.CallActorAsync();

            //assert:
            Assert.IsTrue(actor.InsertAsyncCalled);
        }
コード例 #28
0
        public void ResolveTest()
        {
            using (var container = new WindsorContainer())
            {
                var resolver = new ActorResolver(container.Kernel, typeof(TestActor));

                var actorService = MockActorServiceFactory.CreateActorServiceForActor <TestActor>();

                Assert.Throws <ComponentNotFoundException>(() => resolver.Resolve(actorService, new ActorId(1)));

                container.Register(
                    Component.For <TestActor>().LifestyleTransient());

                var actor = resolver.Resolve(actorService, new ActorId(1));

                Assert.NotNull(actor);
                var testActor = Assert.IsType <TestActor>(actor);

                Assert.Equal(new ActorId(1), testActor.ActorId);
            }
        }
コード例 #29
0
        public async Task TestGetActorRemindersExtensionMethod()
        {
            var    svc          = MockActorServiceFactory.CreateActorServiceForActor <ReminderTimerActor>();
            var    actor        = svc.Activate(new ActorId(Guid.NewGuid()));
            string reminderName = "reminder";
            IEnumerable <IActorReminder> reminderCollection = null;

            // Test empty when no reminders
            reminderCollection = actor.GetActorReminders();
            Assert.IsFalse(reminderCollection.Any());

            // Test non-empty when reminder registered
            await actor.RegisterReminderAsync(reminderName);

            reminderCollection = actor.GetActorReminders();
            Assert.IsTrue(reminderCollection.Any(r => string.Equals(r.Name, reminderName)));

            // Test non-empty when reminder registered
            await actor.UnregisterReminderAsync(reminderName);

            reminderCollection = actor.GetActorReminders();
            Assert.IsFalse(reminderCollection.Any(r => string.Equals(r.Name, reminderName)));
        }
コード例 #30
0
 public ReproActorTest()
 {
     _actorService = MockActorServiceFactory.CreateActorServiceForActor <ReproActor>();
     _actor        = _actorService.Activate(new ActorId(string.Empty));
     _actor.InvokeOnActivateAsync().Wait();
 }