changeset 196:40d7fed4a09e

fixed promise chaining behavior, the error handler doesn't handle result or cancellation handlers exceptions these exceptions are propagated to the next handlers.
author cin
date Mon, 29 Aug 2016 23:15:51 +0300
parents 0d69c0d6de0d
children 86187b01c4e0
files Implab.Test/RunnableComponentTests.cs Implab/ActionChainTask.cs Implab/ActionChainTaskBase.cs Implab/ActionChainTaskT.cs Implab/ActionTask.cs Implab/ActionTaskBase.cs Implab/ActionTaskT.cs Implab/Components/RunnableComponent.cs Implab/FuncChainTask.cs Implab/FuncChainTaskBase.cs Implab/FuncChainTaskT.cs Implab/FuncTask.cs Implab/FuncTaskBase.cs Implab/FuncTaskT.cs
diffstat 14 files changed, 35 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/Implab.Test/RunnableComponentTests.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab.Test/RunnableComponentTests.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -132,7 +132,7 @@
             ShouldThrow(() => p.Join(1000));
             Assert.AreEqual(ExecutionState.Failed, comp.State);
 
-            Assert.IsInstanceOfType(comp.LastError, typeof(OperationCanceledException));
+            Assert.IsTrue(comp.LastError is OperationCanceledException);
 
             comp.Dispose();
         }
@@ -185,7 +185,7 @@
             p.Cancel();
             ShouldThrow(() => p.Join(1000));
             Assert.AreEqual(ExecutionState.Failed, comp.State);
-            Assert.IsInstanceOfType(comp.LastError, typeof(OperationCanceledException));
+            Assert.IsTrue(comp.LastError is OperationCanceledException);
 
             comp.Dispose();
         }
--- a/Implab/ActionChainTask.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/ActionChainTask.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -23,10 +23,8 @@
                     var p = m_task();
                     p.On(SetResult, HandleErrorInternal, HandleCancelInternal);
                     CancellationRequested(p.Cancel);
-                } catch (OperationCanceledException reason){
-                    HandleCancelInternal(reason);
                 } catch(Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             }
         }
--- a/Implab/ActionChainTaskBase.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/ActionChainTaskBase.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -36,10 +36,10 @@
                     // отдавать ли результат или подтвердить отмену (или вернуть ошибку).
                     CancellationRequested(p.Cancel);
                 } catch (Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             } else {
-                HandleErrorInternal(reason ?? new OperationCanceledException());
+                SetCancelledInternal(reason);
             }
         }
 
--- a/Implab/ActionChainTaskT.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/ActionChainTaskT.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -14,10 +14,8 @@
                     var p = m_task(value);
                     p.On(SetResult, HandleErrorInternal, HandleCancelInternal);
                     CancellationRequested(p.Cancel);
-                } catch (OperationCanceledException reason) {
-                    HandleCancelInternal(reason);
                 } catch(Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             }
         }
--- a/Implab/ActionTask.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/ActionTask.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -12,10 +12,8 @@
                 try {
                     m_task();
                     SetResult();
-                } catch(OperationCanceledException reason) {
-                    HandleCancelInternal(reason);
                 } catch(Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             }
         }
--- a/Implab/ActionTaskBase.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/ActionTaskBase.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -42,10 +42,10 @@
                     m_cancel(error);
                     SetResult();
                 } catch(Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             } else {
-                HandleErrorInternal(error ?? new OperationCanceledException());
+                SetCancelledInternal(error);
             }
         }
     }
--- a/Implab/ActionTaskT.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/ActionTaskT.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -12,10 +12,8 @@
                 try {
                     m_task(value);
                     SetResult();
-                } catch(OperationCanceledException reason) {
-                    HandleCancelInternal(reason);
                 } catch(Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             }
         }
--- a/Implab/Components/RunnableComponent.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/Components/RunnableComponent.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -120,6 +120,20 @@
             
                 prev = m_pending;
 
+                Action<Exception> errorOrCancel = e => {
+                    if (e == null)
+                        e = new OperationCanceledException();
+                    
+                    lock (m_stateMachine) {
+                        if (m_pending == promise) {
+                            Move(Commands.Fail);
+                            m_pending = null;
+                            m_lastError = e;
+                        }
+                    }
+                    throw new PromiseTransientException(e);
+                };
+
                 promise = task.Then(
                     () => {
                         lock(m_stateMachine) {
@@ -128,16 +142,9 @@
                                 m_pending = null;
                             }
                         }
-                    }, e => {
-                        lock(m_stateMachine) {
-                            if (m_pending == promise) {
-                                Move(Commands.Fail);
-                                m_pending = null;
-                                m_lastError = e;
-                            }
-                        }
-                        throw new PromiseTransientException(e);
-                    }
+                    },
+                    errorOrCancel,
+                    errorOrCancel 
                 );
 
                 m_pending = promise;
--- a/Implab/FuncChainTask.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/FuncChainTask.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -15,10 +15,8 @@
                     var operation = m_task();
                     operation.On(SetResult, HandleErrorInternal, HandleCancelInternal);
                     CancellationRequested(operation.Cancel);
-                } catch (OperationCanceledException reason) {
-                    HandleCancelInternal(reason);
                 } catch (Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             }
         }
--- a/Implab/FuncChainTaskBase.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/FuncChainTaskBase.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -43,10 +43,10 @@
                     p.On(SetResult, HandleErrorInternal, SetCancelledInternal);
                     CancellationRequested(p.Cancel);
                 } catch (Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             } else {
-                HandleErrorInternal(reason ?? new OperationCanceledException());
+                SetCancelledInternal(reason);
             }
         }
     }
--- a/Implab/FuncChainTaskT.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/FuncChainTaskT.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -14,10 +14,8 @@
                     var operation = m_task(value);
                     operation.On(SetResult, HandleErrorInternal, SetCancelled);
                     CancellationRequested(operation.Cancel);
-                } catch (OperationCanceledException reason) {
-                    HandleCancelInternal(reason);
                 } catch (Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             }
         }
--- a/Implab/FuncTask.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/FuncTask.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -13,10 +13,8 @@
             if (m_task != null && LockCancelation()) {
                 try {
                     SetResult(m_task());
-                } catch(OperationCanceledException reason)  {
-                    HandleCancelInternal(reason);
                 } catch(Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             }
         }
--- a/Implab/FuncTaskBase.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/FuncTaskBase.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -40,10 +40,10 @@
                 try {
                     SetResult(m_cancel(reason));
                 } catch (Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             } else {
-                HandleErrorInternal(reason ?? new OperationCanceledException());
+                SetCancelledInternal(reason);
             }
         }
 
--- a/Implab/FuncTaskT.cs	Fri Apr 22 13:13:08 2016 +0300
+++ b/Implab/FuncTaskT.cs	Mon Aug 29 23:15:51 2016 +0300
@@ -12,10 +12,8 @@
             if (m_task != null && LockCancelation()) {
                 try {
                     SetResult(m_task(value));
-                } catch(OperationCanceledException reason)  {
-                    HandleCancelInternal(reason);
                 } catch(Exception err) {
-                    HandleErrorInternal(err);
+                    SetErrorInternal(err);
                 }
             }
         }