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]); } }
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 }); }
public override void Schedule(IConcurrentObject who, Action what, Action <Exception> failure) { base.Schedule(who, what, failure); if (_blockUntilNextEvent != null) { _blockUntilNextEvent.Set(); } }
public virtual void Schedule(IConcurrentObject who, Action what, Action <Exception> failure) { _queue.Enqueue(new Event { Who = who, What = what, Failure = failure, }); }
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); }
private void onSpawn(Guid id, IConcurrentObject @object) { _objects[id] = @object; if (Send != null) { outgoingMessage(Guid.Empty, "__instance", $"{{\"__ID\" : \"{id}\", \"__IID\" : \"{_iid}\"}}", Guid.Empty); } }
public static void AssertFails(IConcurrentObject @object, params string[] steps) { var result = RunSteps(@object, steps); if (result == null) { throw new InvalidOperationException("Expecting failure"); } }
public static void Succeeds(IConcurrentObject @object, params string[] steps) { var result = RunSteps(@object, steps); if (result != null) { throw new InvalidOperationException("Expecting success"); } }
public override void Schedule(IConcurrentObject who, Action what, Action <Exception> failure) { lock (this) { try { what(); } catch (Exception ex) { failure(ex); } } }
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--; } }); }
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); }
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); }
public abstract void Schedule(IConcurrentObject who, Action what, Action <Exception> failure, TimeSpan when);
public void RegisterInstance(Guid id, IConcurrentObject @object) { ConcurrentApp.Spawn(@object); _objects[id] = @object; }
public static void SendAsync(IConcurrentObject @object, string name, params object[] args) { Send(@object, name, false, args); }
public void AddSingleton(string typeName, IConcurrentObject concurrentObject) { _singletons[typeName] = concurrentObject; spawnObject(concurrentObject); }
public override void Schedule(IConcurrentObject who, Action what, Action <Exception> failure, TimeSpan when) { Task.Delay(when, _stop.Token) .ContinueWith(task => Schedule(who, what, failure)); }