private static void Send(ConcurrentObject @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[] { true }).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(ConcurrentObject @object, string methodName, Func <string> moveNext, Action <Exception> failure) { var method = @object.GetType().GetMethod(methodName, new[] { 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[] { success, failure }); }
public static void Fails(ConcurrentObject @object, params string[] steps) { var result = RunSteps(@object, steps); if (result == null) { throw new InvalidOperationException("Expecting failure"); } }
public static void Succeeds(ConcurrentObject @object, params string[] steps) { var result = RunSteps(@object, steps); if (result != null) { throw new InvalidOperationException("Expecting success"); } }
public void Queue(ConcurrentObject who, Action what, Action <Exception> failure) { //_queue.Enqueue(new Event //{ // Tries = 0, // Target = who, // What = what, // Failure = failure //}); _queue.Enqueue(queueEvent(0, who, what, failure)); }
public void Queue(ConcurrentObject who, Action what, Action <Exception> failure) { _queue.Enqueue(new Event { Tries = 0, Target = who, What = what, Failure = failure }); //_queue.Enqueue(queueEvent(0, who, what, failure)); }
private Event queueEvent(int tries, ConcurrentObject target, Action action, Action <Exception> failure) { var result = null as Event; if (_cache.TryDequeue(out result)) { _cacheHits++; result.Tries = tries; result.Target = target; result.What = action; result.Failure = failure; return(result); } _cacheMisses++; return(new Event { Tries = tries, Target = target, What = action, Failure = failure }); }
private static Exception RunSteps(ConcurrentObject @object, string[] steps) { int stepCount = steps.Length; int stepIdx = 0; 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 void Start(ConcurrentObject @object, params object[] args) { @object.startRunning(this, args); }
public static void SendAsync(ConcurrentObject @object, string name, params object[] args) { Send(@object, name, false, args); }