Esempio n. 1
0
        public void API_TryWrappedInvoke()
        {
            var atomicInt = new LockFreeInt32();

            var target = new AtomicDelegate <Func <int, int> >();

            Assert.AreEqual((false, default(int)), target.TryWrappedInvoke(f => f(10)));

            target.Set(i => atomicInt.Add(i).CurrentValue);
            Assert.AreEqual((true, 10), target.TryWrappedInvoke(f => f(10)));
            Assert.AreEqual(10, atomicInt.Value);

            Assert.AreEqual(true, target.TryWrappedInvoke((Action <Func <int, int> >)(f => f(10))));
            Assert.AreEqual(20, atomicInt.Value);
            target.Set(null);
            Assert.AreEqual(false, target.TryWrappedInvoke((Action <Func <int, int> >)(f => f(10))));
            Assert.AreEqual(20, atomicInt.Value);


            // Context-consuming variants
            atomicInt.Set(0);
            target.Set(null);
            Assert.AreEqual((false, default(int)), target.TryWrappedInvoke((f, ctx) => f(ctx), 10));

            target.Set(i => atomicInt.Add(i).CurrentValue);
            Assert.AreEqual((true, 10), target.TryWrappedInvoke((f, ctx) => f(ctx), 10));
            Assert.AreEqual(10, atomicInt.Value);

            Assert.AreEqual(true, target.TryWrappedInvoke((Action <Func <int, int>, int>)((f, ctx) => f(ctx)), 10));
            Assert.AreEqual(20, atomicInt.Value);
            target.Set(null);
            Assert.AreEqual(false, target.TryWrappedInvoke((Action <Func <int, int>, int>)((f, ctx) => f(ctx)), 10));
            Assert.AreEqual(20, atomicInt.Value);
        }
Esempio n. 2
0
        public void API_TryDynamicInvoke()
        {
            var atomicInt = new LockFreeInt32();

            var targetA = new AtomicDelegate <Action <int> >();

            Assert.AreEqual((false, (object)null), targetA.TryDynamicInvoke(10));

            targetA.Set(i => atomicInt.Add(i));
            Assert.AreEqual((true, (object)null), targetA.TryDynamicInvoke(300));
            Assert.Throws <TargetParameterCountException>(() => targetA.TryDynamicInvoke());
            Assert.Throws <ArgumentException>(() => targetA.TryDynamicInvoke(""));
            Assert.Throws <TargetParameterCountException>(() => targetA.TryDynamicInvoke(3, 3));

            var targetB = new AtomicDelegate <Func <int, int> >();

            Assert.AreEqual((false, (object)null), targetB.TryDynamicInvoke(10));

            targetB.Set(i => atomicInt.Add(i).CurrentValue);
            var invokeRes = targetB.TryDynamicInvoke(300);

            Assert.AreEqual(true, invokeRes.DelegateWasInvoked);
            Assert.AreEqual(600, (int)invokeRes.Result);

            Assert.Throws <TargetParameterCountException>(() => targetB.TryDynamicInvoke());
            Assert.Throws <ArgumentException>(() => targetB.TryDynamicInvoke(""));
            Assert.Throws <TargetParameterCountException>(() => targetB.TryDynamicInvoke(3, 3));
        }
Esempio n. 3
0
        public static bool TryInvoke <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(this AtomicDelegate <Action <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> > @this, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11)
        {
            var curValue = @this.Value;

            if (curValue is null)
            {
                return(false);
            }
            curValue(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
            return(true);
        }
Esempio n. 4
0
        public static bool TryInvoke <T1, T2, T3, T4, T5, T6>(this AtomicDelegate <Action <T1, T2, T3, T4, T5, T6> > @this, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
        {
            var curValue = @this.Value;

            if (curValue is null)
            {
                return(false);
            }
            curValue(arg1, arg2, arg3, arg4, arg5, arg6);
            return(true);
        }
Esempio n. 5
0
        public static bool TryInvoke <T1, T2, T3>(this AtomicDelegate <Action <T1, T2, T3> > @this, T1 arg1, T2 arg2, T3 arg3)
        {
            var curValue = @this.Value;

            if (curValue is null)
            {
                return(false);
            }
            curValue(arg1, arg2, arg3);
            return(true);
        }
Esempio n. 6
0
        public static bool TryInvoke(this AtomicDelegate <Action> @this)
        {
            var curValue = @this.Value;

            if (curValue is null)
            {
                return(false);
            }
            curValue();
            return(true);
        }
Esempio n. 7
0
 public static (bool DelegateWasInvoked, TOut Result) TryInvoke <TOut>(this AtomicDelegate <Func <TOut> > @this)
 {
Esempio n. 8
0
        public void API_CombineRemoveRemoveAll()
        {
            var lastRecordedInput = new LockFreeReference <string>();
            Func <string, string> initialValue = s => { lastRecordedInput.Set(s); return(s.ToUpper()); };

            var target = new AtomicDelegate <Func <string, string> >(initialValue);

            var combineRes = target.Combine(s => s.ToLower());

            Assert.AreEqual(initialValue, combineRes.PreviousValue);
            Assert.AreEqual("TEST", combineRes.PreviousValue("Test"));
            Assert.AreEqual("test", combineRes.CurrentValue("Test"));

            Assert.AreEqual("Test", lastRecordedInput.Value);

            target.TryInvoke("Input");
            Assert.AreEqual("Input", lastRecordedInput.Value);

            combineRes = target.Combine(s => s + s);
            Assert.AreEqual("abcabc", target.Value("abc"));
            Assert.AreEqual("abc", lastRecordedInput.Value);
            Assert.AreEqual(combineRes.CurrentValue, target.Value);

            var removeRes = target.Remove(initialValue);

            Assert.AreEqual("qwertyqwerty", removeRes.PreviousValue("qwerty"));
            Assert.AreEqual("qwerty", lastRecordedInput.Value);
            Assert.AreEqual("ijklijkl", removeRes.CurrentValue("ijkl"));
            Assert.AreEqual("qwerty", lastRecordedInput.Value);
            Assert.AreEqual(removeRes.CurrentValue, target.Value);

            removeRes = target.Remove(c => "this delegate was never added");
            Assert.AreEqual(removeRes.CurrentValue, removeRes.PreviousValue);
            Assert.AreEqual(target.Value, removeRes.PreviousValue);

            var invocationCounter = new LockFreeInt32();
            Func <string, string> CurrentValue = s => { invocationCounter.Increment(); return(s[0].ToString()); };

            target.Combine(CurrentValue);
            target.Combine(CurrentValue);
            target.Combine(CurrentValue);

            Assert.AreEqual((true, "r"), target.TryInvoke("rrrrr"));
            Assert.AreEqual(3, invocationCounter.Value);

            target.Remove(CurrentValue);
            Assert.AreEqual((true, "r"), target.TryInvoke("rrrrr"));
            Assert.AreEqual(5, invocationCounter.Value);

            removeRes = target.RemoveAll(CurrentValue);
            Assert.AreEqual((true, "rrrrrrrrrr"), target.TryInvoke("rrrrr"));
            Assert.AreEqual(5, invocationCounter.Value);
            Assert.AreEqual("rrrrrrrrrr", removeRes.CurrentValue("rrrrr"));
            Assert.AreEqual("r", removeRes.PreviousValue("rrrrr"));

            removeRes = target.RemoveAll(CurrentValue);
            Assert.AreEqual((true, "rrrrrrrrrr"), target.TryInvoke("rrrrr"));
            Assert.AreEqual(7, invocationCounter.Value);
            Assert.AreEqual("rrrrrrrrrr", removeRes.CurrentValue("rrrrr"));
            Assert.AreEqual(removeRes.CurrentValue, removeRes.PreviousValue);
        }