Example #1
0
        private static void Send(IConcurrentObject @object, string name, bool wait, object[] args)
        {
            var method = @object
                         .GetType()
                         .GetMethods()
                         .Where(m => m.Name == name &&
                                m
                                .ReturnType
                                .ToString()
                                .Contains("Task"))
                         .SingleOrDefault();

            if (method != null)
            {
                args = args.Union(new object[] { default(CancellationToken) }).ToArray();
                var tsk = method.Invoke(@object, args);

                if (wait)
                {
                    ((Task)tsk).Wait();
                }
            }
            else
            {
                var property = @object
                               .GetType()
                               .GetProperty(name);

                Debug.Assert(property != null && property.CanWrite);
                property.SetValue(@object, args[0]);
            }
        }
Example #2
0
        private static void RunStep(IConcurrentObject @object, string methodName, Func <string> moveNext, Action <Exception> failure)
        {
            var method = @object.GetType().GetMethod(methodName, new[]
            {
                typeof(CancellationToken),
                typeof(Action <object>),
                typeof(Action <Exception>),
            });

            Action <object> success = (res) =>
            {
                try
                {
                    var next = moveNext();
                    if (next != null)
                    {
                        RunStep(@object, next, moveNext, failure);
                    }
                }
                catch (Exception ex)
                {
                    failure(ex);
                }
            };

            method.Invoke(@object, new object[] { default(CancellationToken), success, failure });
        }
Example #3
0
 public override void Schedule(IConcurrentObject who, Action what, Action <Exception> failure)
 {
     base.Schedule(who, what, failure);
     if (_blockUntilNextEvent != null)
     {
         _blockUntilNextEvent.Set();
     }
 }
Example #4
0
 public virtual void Schedule(IConcurrentObject who, Action what, Action <Exception> failure)
 {
     _queue.Enqueue(new Event
     {
         Who     = who,
         What    = what,
         Failure = failure,
     });
 }
Example #5
0
        public virtual void Spawn(IConcurrentObject @object)
        {
            //td: consider the policies based on the type list
            //var type = @object.GetType();
            //if (_types != null && !_types.ContainsKey(type.Name))
            //    throw new ArgumentException($"{type.Name} is not registered to run in ths app");

            spawnObject(@object);
        }
Example #6
0
        private void onSpawn(Guid id, IConcurrentObject @object)
        {
            _objects[id] = @object;

            if (Send != null)
            {
                outgoingMessage(Guid.Empty, "__instance", $"{{\"__ID\" : \"{id}\", \"__IID\" : \"{_iid}\"}}", Guid.Empty);
            }
        }
Example #7
0
        public static void AssertFails(IConcurrentObject @object, params string[] steps)
        {
            var result = RunSteps(@object, steps);

            if (result == null)
            {
                throw new InvalidOperationException("Expecting failure");
            }
        }
Example #8
0
        public static void Succeeds(IConcurrentObject @object, params string[] steps)
        {
            var result = RunSteps(@object, steps);

            if (result != null)
            {
                throw new InvalidOperationException("Expecting success");
            }
        }
Example #9
0
 public override void Schedule(IConcurrentObject who, Action what, Action <Exception> failure)
 {
     lock (this)
     {
         try
         {
             what();
         }
         catch (Exception ex)
         {
             failure(ex);
         }
     }
 }
Example #10
0
 public override void Schedule(IConcurrentObject who, Action what, Action <Exception> failure, TimeSpan when)
 {
     _scheduleCount++;
     Task.Delay(when)
     .ContinueWith(task =>
     {
         //this will run in a different thread, make synchronous
         lock (_locker)
         {
             Schedule(who, what, failure);
             _scheduleCount--;
         }
     });
 }
Example #11
0
        private static bool isConcurrentSingleton(Type type, out Guid id, out IConcurrentObject @object)
        {
            var attribute = type
                            .CustomAttributes
                            .Where(attr => attr.AttributeType.Name == "ConcurrentSingleton")
                            .SingleOrDefault();

            if (attribute != null && attribute.ConstructorArguments.Count == 1)
            {
                id      = Guid.Parse((string)attribute.ConstructorArguments[0].Value);
                @object = (IConcurrentObject)Activator.CreateInstance(type);
                return(true);
            }

            id      = Guid.Empty;
            @object = null;
            return(false);
        }
Example #12
0
        private static Exception RunSteps(IConcurrentObject @object, string[] steps)
        {
            int stepCount = steps.Length;
            int stepIdx   = 1;

            Func <string> nextStep = () =>
            {
                if (stepIdx < stepCount)
                {
                    return(steps[stepIdx++]);
                }
                return(null);
            };

            var ex = null as Exception;

            RunStep(@object, steps[0], nextStep, (__ex) => { ex = __ex; });
            while (ex == null && stepIdx < stepCount)
            {
                Thread.Sleep(100);
            }

            return(ex);
        }
Example #13
0
 public abstract void Schedule(IConcurrentObject who, Action what, Action <Exception> failure, TimeSpan when);
Example #14
0
 public void RegisterInstance(Guid id, IConcurrentObject @object)
 {
     ConcurrentApp.Spawn(@object);
     _objects[id] = @object;
 }
Example #15
0
 public static void SendAsync(IConcurrentObject @object, string name, params object[] args)
 {
     Send(@object, name, false, args);
 }
Example #16
0
 public void AddSingleton(string typeName, IConcurrentObject concurrentObject)
 {
     _singletons[typeName] = concurrentObject;
     spawnObject(concurrentObject);
 }
Example #17
0
 public override void Schedule(IConcurrentObject who, Action what, Action <Exception> failure, TimeSpan when)
 {
     Task.Delay(when, _stop.Token)
     .ContinueWith(task => Schedule(who, what, failure));
 }