Exemple #1
0
        public void AssignDisposableWorks()
        {
            var i = 0;
            var a = DisposableEx.Assign(DisposableEx.Action(() => i++));

            Assert.Equal(0, i);
            var newItem = DisposableEx.Action(() => i += 10);

            a.Item = newItem;
            Assert.Equal(1, i);
            a.Item = newItem;
            Assert.Equal(1, i);
            a.Dispose();
            Assert.Equal(11, i);
            a.Dispose();
            Assert.Equal(11, i);
            a.Item = DisposableEx.Action(() => i = 0);
            Assert.Equal(11, i);
            a.Item = newItem;
            Assert.Equal(0, i);
            using (var d = DisposableEx.Assign <IDisposable>())
            {
                d.Item = null;
            }
        }
Exemple #2
0
        public void UseContextWorks()
        {
            var defaultTaskScheduler = TaskScheduler.Default;
            var v  = 0;
            var ts = new ThreadLocal <int>(() => v++);

            EventLoop.Run(() =>
            {
                var sc   = SynchronizationContext.Current;
                var done = false;

                var threadId     = -1;
                var realThreadId = -1;
                var disp         = DisposableEx.Action(() => threadId = ts.Value).UseContext(sc);
                Task.Factory.StartNew(() =>
                {
                    realThreadId = ts.Value;
                    disp.Dispose();
                    done = true;
                }, CancellationToken.None, TaskCreationOptions.None, defaultTaskScheduler);

                while (!done)
                {
                    EventLoop.DoEvents();
                }
                Assert.Equal(ts.Value, threadId);
                Assert.NotEqual(realThreadId, threadId);
            });
        }
Exemple #3
0
        public void DisposeOnFinalizeWorks()
        {
            var i = 0;

            DisposableEx.Action(() => i++).DisposeOnFinalize();
            GCEx.CollectAndWait();
            Assert.Equal(1, i);
        }
        public void UsingOk()
        {
            var lambda   = Lambda(p => Using(p(typeof(IDisposable), "a"), 2));
            var compiled = (Func <IDisposable, int>)lambda.Compile();
            var disposed = false;
            var dis      = DisposableEx.Action(() => disposed = true);

            Assert.Equal(2, compiled(dis));
            Assert.True(disposed);
        }
Exemple #5
0
        public void ConcatDisposableWorks()
        {
            var i = 0;
            var j = 0;

            using (DisposableEx.Concat(DisposableEx.Action(() => i = 1), DisposableEx.Action(() => j = 1)))
            {
            }
            Assert.Equal(1, i);
            Assert.Equal(1, j);
        }
Exemple #6
0
        public void ActionInvokesOnDispose()
        {
            var         a = 1;
            IDisposable disposable;

            using (disposable = DisposableEx.Action(() => a = 3))
            {
                a = 2;
            }
            Assert.Equal(3, a);
            a = 5;
            disposable.Dispose();
            Assert.Equal(5, a);
        }
        public void UsingOk()
        {
            var method = EmitEx.CreateMethod <Func <IDisposable, int> >();
            var il     = method.GetILGenerator();

            il.EmitLdarg(0);
            var disp = il.DeclareLocal(typeof(IDisposable));

            il.EmitStloc(disp);
            il.EmitUsing(disp, () =>
            {
            });
            il.EmitLdcI4(42);
            il.Emit(OpCodes.Ret);

            var compiled = method.CreateDelegate <Func <IDisposable, int> >();
            var disposed = false;
            var dis      = DisposableEx.Action(() => disposed = true);

            Assert.Equal(42, compiled(dis));
            Assert.True(disposed);
        }