Пример #1
0
        public static void test_cancel_after_resolve()
        {
            bool callbackCalled = false;

            eina.Error received_error = eina.Error.NO_ERROR;

            efl.ILoop    loop    = efl.App.GetLoopMain();
            eina.Promise promise = new eina.Promise();
            eina.Future  future  = new eina.Future(promise);

            future = future.Then((eina.Value value) => {
                callbackCalled = true;
                value.Get(out received_error);
                return(value);
            });

            promise.Reject(eina.Error.EPERM);
            future.Cancel();

            loop.Iterate();

            Test.Assert(callbackCalled, "Future callback should have been called.");
            Test.AssertEquals(received_error, eina.Error.ECANCELED);

            Test.AssertRaises <ObjectDisposedException>(() => { promise.Resolve(null); });
            Test.AssertRaises <ObjectDisposedException>(future.Cancel);
        }
Пример #2
0
        public static void test_simple_cancel()
        {
            bool cleanCalled = false;

            eina.Promise promise = new eina.Promise(() => { cleanCalled = true; });
            eina.Future  future  = new eina.Future(promise);
            future.Cancel();
            Test.Assert(cleanCalled, "Promise clean callback should have been called.");
            Test.AssertRaises <ObjectDisposedException>(() => { promise.Resolve(null); });
            Test.AssertRaises <ObjectDisposedException>(future.Cancel);
        }
Пример #3
0
        public static System.Threading.Tasks.Task <eina.Value> WrapAsync(eina.Future future, CancellationToken token)
        {
            // Creates a task that will wait for SetResult for completion.
            // TaskCompletionSource is used to create tasks for 'external' Task sources.
            var tcs = new System.Threading.Tasks.TaskCompletionSource <eina.Value>();

            // Flag to be passed to the cancell callback
            bool fulfilled = false;

            future.Then((eina.Value received) => {
                    lock (future)
                    {
                        // Convert an failed Future to a failed Task.
                        if (received.GetValueType() == eina.ValueType.Error)
                        {
                            eina.Error err;
                            received.Get(out err);
                            if (err == eina.Error.ECANCELED)
                            {
                                tcs.SetCanceled();
                            }
                            else
                            {
                                tcs.TrySetException(new efl.FutureException(received));
                            }
                        }
                        else
                        {
                            // Will mark the returned task below as completed.
                            tcs.SetResult(received);
                        }
                        fulfilled = true;
                        return(received);
                    }
                });
            // Callback to be called when the token is cancelled.
            token.Register(() => {
                    lock (future)
                    {
                        // Will trigger the Then callback above with an eina.Error
                        if (!fulfilled)
                        {
                            future.Cancel();
                        }
                    }
                });

            return(tcs.Task);
        }
Пример #4
0
        public static void test_simple_future_cancel()
        {
            bool callbackCalled        = false;
            bool promiseCallbackCalled = false;

            eina.Error received_error = eina.Error.NO_ERROR;

            eina.Promise promise = new eina.Promise(() => { promiseCallbackCalled = true; });
            eina.Future  future  = new eina.Future(promise);

            future = future.Then((eina.Value value) => {
                callbackCalled = true;
                value.Get(out received_error);
                return(value);
            });

            future.Cancel();

            Test.Assert(promiseCallbackCalled, "Promise cancel callback should have been called.");
            Test.Assert(callbackCalled, "Future callback should have been called.");
            Test.AssertEquals(received_error, eina.Error.ECANCELED);
        }