Example #1
0
        public void Caching() => TestZone.Run(tick =>
        {
            var random = new Random();
            var atom   = Atom.Computed(() => random.Next(), keepAlive: true);

            Assert.AreEqual(atom.Value, atom.Value);
        });
Example #2
0
        public void Deactivation() => TestZone.Run(tick =>
        {
            var activation = "";

            var source = Atom.Value(
                0,
                onActive: () => activation   += "S",
                onInactive: () => activation += "s");

            var middle = Atom.Computed(
                () => source.Value + 1,
                onActive: () => activation   += "M",
                onInactive: () => activation += "m");

            var target = Atom.Computed(
                () => middle.Value + 1,
                onActive: () => activation   += "T",
                onInactive: () => activation += "t");

            target.Get();
            Assert.AreEqual("", activation);

            var autoRun = Atom.AutoRun(() => target.Get());
            Assert.AreEqual("TMS", activation);

            autoRun.Dispose();
            Assert.AreEqual("TMS", activation);

            tick(0);
            Assert.AreEqual("TMStms", activation);
        });
Example #3
0
        public void ObsoleteAtomsActualizedInInitialOrder() => TestZone.Run(tick =>
        {
            var actualization = "";

            var source = Atom.Value(1);
            var middle = Atom.Computed(() =>
            {
                actualization += "M";
                return(source.Value);
            });
            var target = Atom.Computed(() =>
            {
                actualization += "T";
                source.Get();
                return(middle.Value);
            });

            var autoRun = Atom.AutoRun(() => target.Get());
            Assert.AreEqual("TM", actualization);

            source.Value = 2;

            tick(0);
            Assert.AreEqual("TMTM", actualization);

            autoRun.Dispose();
        });
Example #4
0
        public void NoReactivationDuringPulling() => TestZone.Run(tick =>
        {
            var activation = "";

            var activationSource = Atom.Computed(
                pull: () => 1,
                onActive: () => activation   += "A",
                onInactive: () => activation += "D");

            var modifiedSource = Atom.Value(1);
            var listener       = Atom.Computed(() => activationSource.Value + modifiedSource.Value);

            Assert.AreEqual("", activation);

            var autoRun = Atom.AutoRun(() => listener.Get());
            Assert.AreEqual("A", activation);

            modifiedSource.Value = 2;
            Assert.AreEqual("A", activation);

            tick(0);
            Assert.AreEqual("A", activation);

            autoRun.Dispose();
        });
Example #5
0
        public void ThrowException() => TestZone.Run(tick =>
        {
            var source    = Atom.Value(0);
            var exception = new Exception();

            var middle = Atom.Computed(() =>
            {
                if (source.Value == 0)
                {
                    throw exception;
                }

                return(source.Value + 1);
            });

            var stack = new Stack <Exception>();

            var reaction = new ReactionAtom(
                reaction: () => middle.Get(),
                exceptionHandler: ex => stack.Push(ex));

            reaction.Get();

            Assert.AreEqual(1, stack.Count);
            Assert.AreEqual(exception, stack.Peek());
            Assert.Throws <Exception>(() => middle.Get());

            source.Value = 1;
            tick(0);

            Assert.AreEqual(2, middle.Value);
        });
Example #6
0
        public static async Task RunMe()
        {
            // get the devices on my network
            var devices = await new LutronDiscovery().DiscoverAllLutronDevices();
            // find the first one (there is only one at my house)
            var firstDevice = devices.FirstOrDefault();

            if (firstDevice == null)
            {
                throw new Exception("Where is my bridge pro?");
            }

            IPAddress bridgeAddress = firstDevice.IPAddress;
            var       cancelToken   = new CancellationToken();

            var extraction = new CertificateExtraction();

            var options = new BridgeSslStreamOptions(bridgeAddress)
            {
                Logging = new ConsoleLogging(),
                LocalCertificateSelectionPolicy = (a1, a2, a3, a4) => extraction.KeyedCertificate,
            };

            using (var connector = new BridgeSslStreamConnector(options, cancelToken))
            {
                var  zone1       = new TestZone();
                bool isConnected = await connector.Connect();

                connector.Responses.Subscribe((response) =>
                {
                    var r = response;
                });
                var result = await connector.Ping();

                await Task.Delay(2000);

                var result2 = await connector.GetDevices();

                await Task.Delay(2000);

                connector.SendGetScenes();
                await Task.Delay(2000);

                connector.SendGetZoneStatus(zone1);
                await Task.Delay(2000);

                connector.SetZoneLevel(zone1, 50);
                await Task.Delay(2000);

                connector.TurnOffZone(zone1);
                await Task.Delay(2000);

                connector.TurnOnZone(zone1);
            }
        }
Example #7
0
        public void Lazy() => TestZone.Run(tick =>
        {
            var value = 0;
            var atom  = Atom.Computed(() => value = 1);

            tick(0f);
            Assert.AreEqual(0, value);

            atom.Get();
            Assert.AreEqual(1, value);
        });
Example #8
0
        public void NoSubscribed() => TestZone.Run(tick =>
        {
            var source = Atom.Value(1);
            var middle = Atom.Computed(() => source.Value + 1);
            var target = Atom.Computed(() => middle.Value + source.Value);

            Assert.AreEqual(3, target.Value);

            Assert.AreEqual(0, source.SubscribersCount());
            Assert.AreEqual(0, middle.SubscribersCount());
            Assert.AreEqual(0, target.SubscribersCount());
        });
Example #9
0
        public void InstantActualization() => TestZone.Run(tick =>
        {
            var source = Atom.Value(1);
            var middle = Atom.Computed(() => source.Value + 1);
            var target = Atom.Computed(() => middle.Value + 1);

            Assert.AreEqual(3, target.Value);

            source.Value = 2;

            Assert.AreEqual(4, target.Value);
        });
Example #10
0
        public void SettingEqualStateAreIgnored() => TestZone.Run(tick =>
        {
            var atom = Atom.Value(new[] { 1, 2, 3 },
                                  comparer: new TestComparer <int[]>((a, b) => a.SequenceEqual(b)));

            var v1     = atom.Value;
            var v2     = new[] { 1, 2, 3 };
            atom.Value = v2;
            var v3     = atom.Value;

            Assert.IsTrue(ReferenceEquals(v1, v3));
            Assert.IsFalse(ReferenceEquals(v2, v3));
        });
Example #11
0
        public void AtomicDeferredRestart() => TestZone.Run(tick =>
        {
            int targetValue = 0;

            var source = Atom.Value(1);
            var middle = Atom.Computed(() => source.Value + 1);
            var target = Atom.Computed(() => targetValue = middle.Value + 1, keepAlive: true);

            target.Get();
            Assert.AreEqual(3, targetValue);

            source.Value = 2;
            Assert.AreEqual(3, targetValue);

            tick(0);
            Assert.AreEqual(4, targetValue);
        });
Example #12
0
        public void ManualActivation() => TestZone.Run(tick =>
        {
            var activation = "";

            var atom = Atom.Computed(
                pull: () => 1,
                keepAlive: true,
                onActive: () => activation   += "A",
                onInactive: () => activation += "D");

            Assert.AreEqual("", activation);

            atom.Get();
            Assert.AreEqual("A", activation);

            atom.Deactivate();
            Assert.AreEqual("AD", activation);
        });
Example #13
0
        public void AutoUnsubscribed_Reaction() => TestZone.Run(tick =>
        {
            var source = Atom.Value(1);
            var middle = Atom.Computed(() => source.Value + 1);
            var target = Atom.Computed(() => middle.Value + source.Value);

            var run = Atom.AutoRun(() => target.Get());

            Assert.AreEqual(2, source.SubscribersCount());
            Assert.AreEqual(1, middle.SubscribersCount());
            Assert.AreEqual(1, target.SubscribersCount());

            run.Dispose();
            tick(0);

            Assert.AreEqual(0, source.SubscribersCount());
            Assert.AreEqual(0, middle.SubscribersCount());
            Assert.AreEqual(0, target.SubscribersCount());
        });
Example #14
0
        public void NoActivationWithoutSubscribers() => TestZone.Run(tick =>
        {
            var runs       = "";
            var activation = "";

            var atom = Atom.Computed(
                pull: () =>
            {
                runs += "R";
                return(1);
            },
                onActive: () => activation   += "A",
                onInactive: () => activation += "D");

            atom.Get();
            atom.Get();
            atom.Get();
            Assert.AreEqual("RRR", runs);
            Assert.AreEqual("", activation);
        });
Example #15
0
        public void DoNotActualizeWhenMastersNotChanged() => TestZone.Run(tick =>
        {
            var targetUpdates = 0;

            var source = Atom.Value(1);
            var middle = Atom.Computed(() => Math.Abs(source.Value));
            var target = Atom.Computed(() =>
            {
                ++targetUpdates;
                return(middle.Value);
            }, keepAlive: true);

            target.Get();
            Assert.AreEqual(1, target.Value);

            source.Set(-1);
            target.Get();

            Assert.AreEqual(1, targetUpdates);
        });
Example #16
0
        public void NoReactivationDuringModification() => TestZone.Run(tick =>
        {
            var activation = "";

            var atom = Atom.Value(1,
                                  onActive: () => activation   += "A",
                                  onInactive: () => activation += "D");

            Assert.AreEqual("", activation);

            var autoRun = Atom.AutoRun(() => atom.Get());
            Assert.AreEqual("A", activation);

            atom.Value = 2;
            Assert.AreEqual("A", activation);

            tick(0);
            Assert.AreEqual("A", activation);

            autoRun.Dispose();
        });
Example #17
0
        public void AutoUnsubscribed_MultiReaction() => TestZone.Run(tick =>
        {
            var source  = Atom.Value(1);
            var middle  = Atom.Computed(() => source.Value + 1);
            var target1 = Atom.Computed(() => middle.Value + 1);
            var target2 = Atom.Computed(() => middle.Value + 1);

            var run1 = Atom.AutoRun(() => target1.Get());
            var run2 = Atom.AutoRun(() => target2.Get());

            Assert.AreEqual(2, middle.SubscribersCount());

            run1.Dispose();
            tick(0);

            Assert.AreEqual(1, middle.SubscribersCount());

            run2.Dispose();
            tick(0);

            Assert.AreEqual(0, middle.SubscribersCount());
        });
Example #18
0
        public void AutoActivation() => TestZone.Run(tick =>
        {
            var activation = "";

            var atom = Atom.Computed(
                pull: () => 1,
                onActive: () => activation   += "A",
                onInactive: () => activation += "D");

            var listener = Atom.Computed(() => atom.Value + 1, keepAlive: true);

            Assert.AreEqual("", activation);

            listener.Get();
            Assert.AreEqual("A", activation);

            listener.Deactivate();
            Assert.AreEqual("A", activation);

            tick(0);
            Assert.AreEqual("AD", activation);
        });
Example #19
0
        public void AutoRun() => TestZone.Run(tick =>
        {
            var source = Atom.Value(0);

            int runs     = 0;
            var disposer = Atom.AutoRun(() =>
            {
                source.Get();
                ++runs;
            });
            Assert.AreEqual(1, runs);

            source.Value++;
            tick(0);

            Assert.AreEqual(2, runs);

            disposer.Dispose();
            source.Value++;
            tick(0);
            Assert.AreEqual(2, runs);
        });
Example #20
0
        public void WhenAtom() => TestZone.Run(tick =>
        {
            var source = Atom.Value(0);

            string watch = "";

            Atom.When(() => source.Value > 1, () => watch += "B");

            tick(0);
            Assert.AreEqual("", watch);

            source.Value = 1;
            tick(0);
            Assert.AreEqual("", watch);

            source.Value = 2;
            tick(0);
            Assert.AreEqual("B", watch);

            source.Value = 3;
            tick(0);
            Assert.AreEqual("B", watch);
        });
Example #21
0
        public void Invalidate() => TestZone.Run(tick =>
        {
            //
            var source = Atom.Value(0);

            string actualization = "";

            var dispose = Atom.AutoRun(() =>
            {
                source.Get();
                actualization += "T";
            });

            tick(0);
            Assert.AreEqual("T", actualization);

            source.Invalidate();

            tick(0);
            Assert.AreEqual("TT", actualization);

            dispose.Dispose();
        });
Example #22
0
        public void KeepAliveComputed() => TestZone.Run(tick =>
        {
            var source = Atom.Value(1);
            var middle = Atom.Computed(() => source.Value + 1, keepAlive: true);
            var target = Atom.Computed(() => middle.Value + source.Value);

            var run = Atom.AutoRun(() => target.Get());

            Assert.AreEqual(2, source.SubscribersCount());
            Assert.AreEqual(1, middle.SubscribersCount());
            Assert.AreEqual(1, target.SubscribersCount());

            run.Dispose();
            tick(0);

            Assert.AreEqual(1, source.SubscribersCount());
            Assert.AreEqual(0, middle.SubscribersCount());
            Assert.AreEqual(0, target.SubscribersCount());

            middle.Deactivate();
            tick(0);

            Assert.AreEqual(0, source.SubscribersCount());
        });