private async Task <T> CreateGrainAsync <T>(IGrainIdentity identity) where T : Grain
        {
            if (_isGrainCreated)
            {
                throw new Exception(
                          "A grain has already been created in this silo. Only 1 grain per test silo should every be created. Add grain probes for supporting grains.");
            }

            _isGrainCreated = true;

            Grain grain;

            var grainContext = new TestGrainActivationContext
            {
                ActivationServices  = ServiceProvider,
                GrainIdentity       = identity,
                GrainType           = typeof(T),
                ObservableLifecycle = _grainLifecycle,
            };

            //Create a stateless grain
            grain = _grainCreator.CreateGrainInstance(grainContext) as T;

            if (grain == null)
            {
                throw new Exception($"Unable to instantiate grain {typeof(T)} properly");
            }

            //Check if there are any reminders for this grain
            var remindable = grain as IRemindable;

            //Set the reminder target
            if (remindable != null)
            {
                ReminderRegistry.SetGrainTarget(remindable);
            }

            //Trigger the lifecycle hook that will get the grain's state from the runtime
            await _grainLifecycle.TriggerStartAsync();

            return(grain as T);
        }
Beispiel #2
0
        private async Task <T> CreateGrainAsync <T>(IGrainIdentity identity)
            where T : Grain
        {
            if (_isGrainCreated)
            {
                throw new Exception("A grain has already been created in this silo. Only 1 grain per test silo should ever be created. Add grain probes for supporting grains.");
            }

            // Add state attribute mapping for storage facets
            this.AddService <IAttributeToFactoryMapper <PersistentStateAttribute> >(StorageManager.stateAttributeFactoryMapper);

            _isGrainCreated = true;
            Grain grain;
            var   grainContext = new TestGrainActivationContext
            {
                ActivationServices  = ServiceProvider,
                GrainIdentity       = identity,
                GrainType           = typeof(T),
                ObservableLifecycle = _grainLifecycle,
            };

            //Create a stateless grain
            grain = _grainCreator.CreateGrainInstance(grainContext) as T;
            if (grain == null)
            {
                throw new Exception($"Unable to instantiate grain {typeof(T)} properly");
            }

            //Check if there are any reminders for this grain and set the reminder target
            if (grain is IRemindable remindable)
            {
                ReminderRegistry.SetGrainTarget(remindable);
            }

            //Trigger the lifecycle hook that will get the grain's state from the runtime
            await _grainLifecycle.TriggerStartAsync().ConfigureAwait(false);

            return(grain as T);
        }
Beispiel #3
0
        private T CreateGrain <T>(IGrainIdentity identity) where T : Grain
        {
            if (_isGrainCreated)
            {
                throw new Exception(
                          "A grain has already been created in this silo. Only 1 grain per test silo should every be created. Add grain probes for supporting grains.");
            }

            _isGrainCreated = true;

            Grain grain;

            var grainContext = new TestGrainActivationContext()
            {
                ActivationServices = ServiceProvider,
                GrainIdentity      = identity,
                GrainType          = typeof(T),
            };

            //Check to see if the grain is stateful
            if (typeof(T).IsSubclassOfRawGeneric(typeof(Grain <>)))
            {
                var grainGenericArgs = typeof(T).BaseType?.GenericTypeArguments;

                if (grainGenericArgs == null || grainGenericArgs.Length == 0)
                {
                    throw new Exception($"Unable to get grain state type info for {typeof(T)}");
                }

                //Get the state type
                var stateType = grainGenericArgs[0];

                var storage = _storageManager.AddStorage <T>(identity);

                //Create a new stateful grain
                grain = _grainCreator.CreateGrainInstance(grainContext, stateType, storage);

                if (grain == null)
                {
                    throw new Exception($"Unable to instantiate stateful grain {typeof(T)} properly");
                }

                var stateProperty = TypeHelper.GetProperty(typeof(T), "State");

                var state = stateProperty?.GetValue(grain);

                _grainStateManager.AddState(grain, state);
            }
            else
            {
                //Create a stateless grain
                grain = _grainCreator.CreateGrainInstance(grainContext) as T;

                if (grain == null)
                {
                    throw new Exception($"Unable to instantiate grain {typeof(T)} properly");
                }
            }

            //Check if there are any reminders for this grain
            var remindable = grain as IRemindable;

            //Set the reminder target
            if (remindable != null)
            {
                ReminderRegistry.SetGrainTarget(remindable);
            }

            //Emulate the grain's lifecycle
            grain.OnActivateAsync().Wait(1000);

            return(grain as T);
        }