public void It_should_Be_Greater_Than_False_When_True()
            {
                var target = new InterlockedBoolean(true);

                target.GetValue().Should().BeTrue();
                target.Should().Be(new InterlockedBoolean(true));
                (target >= false).Should().BeTrue();
                (false <= target).Should().BeTrue();
                (target >= new InterlockedBoolean(false)).Should().BeTrue();
                (new InterlockedBoolean(false) <= target).Should().BeTrue();
                (target > false).Should().BeTrue();
                (false < target).Should().BeTrue();
                (target > new InterlockedBoolean(false)).Should().BeTrue();
                (new InterlockedBoolean(false) < target).Should().BeTrue();
                (target == true).Should().BeTrue();
                (true == target).Should().BeTrue();
                (target == new InterlockedBoolean(true)).Should().BeTrue();
                (new InterlockedBoolean(true) == target).Should().BeTrue();
                (target != false).Should().BeTrue();
                (false != target).Should().BeTrue();
                (target != new InterlockedBoolean(false)).Should().BeTrue();
                (new InterlockedBoolean(false) != target).Should().BeTrue();
                (target <= false).Should().BeFalse();
                (false >= target).Should().BeFalse();
                (target <= new InterlockedBoolean(false)).Should().BeFalse();
                (new InterlockedBoolean(false) >= target).Should().BeFalse();
                (target < false).Should().BeFalse();
                (false > target).Should().BeFalse();
                (target < new InterlockedBoolean(false)).Should().BeFalse();
                (new InterlockedBoolean(false) > target).Should().BeFalse();
                target.As <IComparable <bool> >().Should().BeGreaterThan(false);
                target.As <IComparable <InterlockedBoolean> >().Should().BeGreaterThan(new InterlockedBoolean(false));
                target.CompareTo(false).Should().BeGreaterThan(0);
            }
Esempio n. 2
0
 public void TestEquality(InterlockedBoolean first, InterlockedBoolean second, bool expected)
 {
     (first == second).ShouldBeEquivalentTo(expected);
     first.Equals(second).ShouldBeEquivalentTo(expected);
     (first.GetHashCode() == second.GetHashCode()).ShouldBeEquivalentTo(expected);
     (first != second).ShouldBeEquivalentTo(!expected);
 }
Esempio n. 3
0
        public void TestNotEquals()
        {
            var ibTrue  = new InterlockedBoolean(true);
            var ibFalse = new InterlockedBoolean(false);

            TestEquality(ibTrue, ibFalse, false);
        }
Esempio n. 4
0
 public SimpleQueue(Func <ResolvedEvent, CancellationToken, Task> onResolvedEvent, CancellationToken token)
 {
     _onResolvedEvent = onResolvedEvent;
     _token           = token;
     _events          = new ConcurrentQueue <ResolvedEvent>();
     _isPushing       = new InterlockedBoolean();
 }
            public void It_should_Implement_IComparable_of_T()
            {
                var target = new InterlockedBoolean(true);

                target.CompareTo(true).Should().Be(0);
                target.CompareTo(new InterlockedBoolean(true)).Should().Be(0);
            }
Esempio n. 6
0
        public void TestReferenceEquals()
        {
            var ibTrue     = new InterlockedBoolean(true);
            var ibAlsoTrue = ibTrue;

            TestEquality(ibTrue, ibAlsoTrue, true);
        }
        public void set_value_true_was_true()
        {
            var interlockedBoolean = new InterlockedBoolean(true);
            var oldValue = interlockedBoolean.Set(true);

            Assert.IsTrue(interlockedBoolean.Value);
            Assert.IsTrue(oldValue);
        }
Esempio n. 8
0
        public void set_value_true_was_true()
        {
            var interlockedBoolean = new InterlockedBoolean(true);
            var oldValue           = interlockedBoolean.Set(true);

            Assert.IsTrue(interlockedBoolean.Value);
            Assert.IsTrue(oldValue);
        }
            public void It_should_Be_False_Upon_Demand()
            {
                var target = new InterlockedBoolean(false);

                target.GetValue().Should().BeFalse();
                target.Equals(false).Should().BeTrue();
                target.Equals(new InterlockedBoolean(false)).Should().BeTrue();
            }
Esempio n. 10
0
        public void CompareExchange_false_was_true_compare_with_true()
        {
            var interlockedBoolean = new InterlockedBoolean(true);
            var oldValue           = interlockedBoolean.CompareExchange(false, true);

            Assert.IsFalse(interlockedBoolean.Value);
            Assert.IsTrue(oldValue);
        }
        public void CompareExchange_true_was_true_compare_with_false()
        {
            var interlockedBoolean = new InterlockedBoolean(true);
            var oldValue = interlockedBoolean.CompareExchange(true, false);

            Assert.IsTrue(interlockedBoolean.Value);
            Assert.IsTrue(oldValue);
        }
        public void CompareExchange_false_was_false_compare_with_true()
        {
            var interlockedBoolean = new InterlockedBoolean();
            var oldValue = interlockedBoolean.CompareExchange(false, true);

            Assert.IsFalse(interlockedBoolean.Value);
            Assert.IsFalse(oldValue);
        }
            public void It_should_Accept_The_Value_Set()
            {
                var target = new InterlockedBoolean(true);

                target.SetValue(true);
                target.GetValue().Should().BeTrue();
                target.SetValue(false);
                target.GetValue().Should().BeFalse();
            }
            public void It_should_Be_True_Upon_Demand()
            {
                var target = new InterlockedBoolean(true);

                target.GetValue().Should().BeTrue();
                target.Equals(true).Should().BeTrue();
                target.Equals(new InterlockedBoolean(true)).Should().BeTrue();
                target.GetHashCode().Should().Be(true.GetHashCode());
            }
            public void It_should_Be_False_By_Default()
            {
                var target = new InterlockedBoolean();

                target.GetValue().Should().BeFalse();
                target.Equals(false).Should().BeTrue();
                target.Equals(new InterlockedBoolean(false)).Should().BeTrue();
                target.GetHashCode().Should().Be(false.GetHashCode());
            }
Esempio n. 16
0
        public void TestInitialize()
        {
            var ib = new InterlockedBoolean();

            ib.Value.Should().Be(false);
            ib = new InterlockedBoolean(false);
            ib.Value.Should().Be(false);
            ib = new InterlockedBoolean(true);
            ib.Value.Should().Be(true);
        }
            public void It_should_Be_Less_Than_True_When_False()
            {
                var target = new InterlockedBoolean(false);

                target.GetValue().Should().BeFalse();
                target.Should().Be(new InterlockedBoolean(false));
                target.As <IComparable <bool> >().Should().BeLessThan(true);
                target.As <IComparable <InterlockedBoolean> >().Should().BeLessThan(new InterlockedBoolean(true));
                target.As <IComparable>().CompareTo(true).Should().BeLessThan(0);
            }
            public void It_should_Evaluate_Equality_With_Value_Semantics()
            {
                var target = new InterlockedBoolean(true);

                target.Equals(null).Should().BeFalse();
                target.Equals((object)new InterlockedBoolean(true)).Should().BeTrue();

                // ReSharper disable once SuspiciousTypeConversion.Global
                target.Equals((object)true).Should().BeTrue();
                target.Equals(new object()).Should().BeFalse();
            }
Esempio n. 19
0
        public void TestSet()
        {
            var ib = new InterlockedBoolean(true);

            ib.Value.ShouldBeEquivalentTo(true);

            ib.Value = false;
            ib.Value.ShouldBeEquivalentTo(false);

            ib.Value = true;
            ib.Value.ShouldBeEquivalentTo(true);
        }
Esempio n. 20
0
        public void TestEquals()
        {
            var ibTrue     = new InterlockedBoolean(true);
            var ibAlsoTrue = new InterlockedBoolean(true);
            var ibFalse    = new InterlockedBoolean(false);

            TestEquality(ibTrue, ibAlsoTrue, true);
            ibTrue.Equals(true).ShouldBeEquivalentTo(true);
            ibTrue.Equals(false).ShouldBeEquivalentTo(false);
            ibFalse.Equals(false).ShouldBeEquivalentTo(true);
            ibFalse.Equals(true).ShouldBeEquivalentTo(false);
        }
Esempio n. 21
0
        public void benchmark_set_value()
        {
            var interlockedBoolean = new InterlockedBoolean();

            const int iterations = 100000000;
            var       sw         = Stopwatch.StartNew();

            for (var i = 0; i < iterations; i++)
            {
                interlockedBoolean.Set(true);
            }
            sw.Stop();

            Assert.Inconclusive("{0} ({1:N0} ops/sec)", sw.Elapsed, iterations / sw.Elapsed.TotalMilliseconds * 1000);
        }
Esempio n. 22
0
        public void Dispose()
        {
            if (_isDisposed)
            {
                return;
            }

            if (_isDisposing.ExchangeValue(true))
            {
                return;
            }

            Dispose(true);
            _isDisposing = false;
            _isDisposed  = true;

            // Use SuppressFinalize in case a subclass implements a finalizer.
            GC.SuppressFinalize(this);
        }
Esempio n. 23
0
        public ResolvedEventDispatcher(
            IEventStoreConnection eventStore,
            ISerializer serializer,
            ICheckpointRepository checkpoints,
            Func <ISerializer, ResolvedEvent, bool, CancellationToken, Task> dispatchResolvedEvent,
            Action onCaughtUp = null,
            string streamId   = null,
            UserCredentials userCredentials = null)
        {
            _isStarted  = new InterlockedBoolean();
            _isDisposed = new InterlockedBoolean();

            _eventStore            = eventStore;
            _serializer            = serializer;
            _checkpoints           = checkpoints;
            _dispatchResolvedEvent = dispatchResolvedEvent;
            _onCaughtUp            = onCaughtUp ?? (() => { });
            _streamId        = streamId;
            _userCredentials = userCredentials;
            _projectedEvents = new Subject <ResolvedEvent>();

            _queue = new SimpleQueue(async(resolvedEvent, token) =>
            {
                try
                {
                    await _dispatchResolvedEvent(_serializer, resolvedEvent, _subscription.IsSubscribedToAll, _disposed.Token);
                }
                catch (Exception ex)
                {
                    _projectedEvents.OnError(ex);
                    throw;
                }

                if (_isDisposed.Value)
                {
                    return;
                }

                _projectedEvents.OnNext(resolvedEvent);
            }, _disposed.Token);
        }
Esempio n. 24
0
        public void benchmark_get_value()
        {
            var interlockedBoolean = new InterlockedBoolean();

            const int iterations = 100000000;
            var       value      = false;

            var sw = Stopwatch.StartNew();

            for (var i = 0; i < iterations; i++)
            {
                value |= interlockedBoolean.Value;
            }
            sw.Stop();

            if (value)
            {
                Console.WriteLine();        // prevent too aggressive optimization
            }
            Assert.Inconclusive("{0} ({1:N0} ops/sec)", sw.Elapsed, iterations / sw.Elapsed.TotalMilliseconds * 1000);
        }
        public KeyValueWatchSubscription(KeyValue kv, string keyPattern,
                                         IKeyValueWatcher watcher, params KeyValueWatchOption[] watchOptions)
        {
            string subject = kv.RawKeySubject(keyPattern);

            // figure out the result options
            bool          headersOnly    = false;
            bool          includeDeletes = true;
            DeliverPolicy deliverPolicy  = DeliverPolicy.LastPerSubject;

            foreach (KeyValueWatchOption wo in watchOptions)
            {
                switch (wo)
                {
                case KeyValueWatchOption.MetaOnly: headersOnly = true; break;

                case KeyValueWatchOption.IgnoreDelete: includeDeletes = false; break;

                case KeyValueWatchOption.UpdatesOnly: deliverPolicy = DeliverPolicy.New; break;

                case KeyValueWatchOption.IncludeHistory: deliverPolicy = DeliverPolicy.All; break;
                }
            }

            if (deliverPolicy == DeliverPolicy.New)
            {
                endOfDataSent = new InterlockedBoolean(true);
                watcher.EndOfData();
            }
            else
            {
                KeyValueEntry kveCheckPending = kv._kvGetLastMessage(keyPattern);
                if (kveCheckPending == null)
                {
                    endOfDataSent = new InterlockedBoolean(true);
                    watcher.EndOfData();
                }
                else
                {
                    endOfDataSent = new InterlockedBoolean(false);
                }
            }

            PushSubscribeOptions pso = PushSubscribeOptions.Builder()
                                       .WithStream(kv.StreamName)
                                       .WithOrdered(true)
                                       .WithConfiguration(
                ConsumerConfiguration.Builder()
                .WithAckPolicy(AckPolicy.None)
                .WithDeliverPolicy(deliverPolicy)
                .WithHeadersOnly(headersOnly)
                .WithFilterSubject(subject)
                .Build())
                                       .Build();

            EventHandler <MsgHandlerEventArgs> handler = (sender, args) =>
            {
                KeyValueEntry kve = new KeyValueEntry(args.msg);
                if (includeDeletes || kve.Operation.Equals(KeyValueOperation.Put))
                {
                    watcher.Watch(kve);
                }

                if (endOfDataSent.IsFalse() && kve.Delta == 0)
                {
                    endOfDataSent.Set(true);
                    watcher.EndOfData();
                }
            };

            sub = kv.js.PushSubscribeAsync(subject, handler, false, pso);
            if (endOfDataSent.IsFalse())
            {
                ulong pending = sub.GetConsumerInformation().CalculatedPending;
                if (pending == 0)
                {
                    endOfDataSent.Set(true);
                    watcher.EndOfData();
                }
            }
        }
Esempio n. 26
0
        public void initial_value_false()
        {
            var interlockedBoolean = new InterlockedBoolean();

            Assert.IsFalse(interlockedBoolean.Value);
        }
        public void benchmark_get_value()
        {
            var interlockedBoolean = new InterlockedBoolean();

            const int iterations = 10000000;
            var value = false;

            var sw = Stopwatch.StartNew();
            for (var i = 0; i < iterations; i++)
            {
                value |= interlockedBoolean.Value;
            }
            sw.Stop();

            if (value) Console.WriteLine(); // prevent too aggressive optimization

            Assert.Inconclusive("{0} ({1:N0} ops/sec)", sw.Elapsed, (decimal)iterations / sw.ElapsedMilliseconds * 1000m);
        }
        public void benchmark_set_value()
        {
            var interlockedBoolean = new InterlockedBoolean();

            const int iterations = 10000000;
            var sw = Stopwatch.StartNew();
            for (var i = 0; i < iterations; i++)
            {
                interlockedBoolean.Set(true);
            }
            sw.Stop();

            Assert.Inconclusive("{0} ({1:N0} ops/sec)", sw.Elapsed, (decimal)iterations / sw.ElapsedMilliseconds * 1000m);
        }
Esempio n. 29
0
 public bool Equals(InterlockedBoolean other)
 {
     return(this.Equals(other.GetValue()));
 }
Esempio n. 30
0
        public void initial_value_true()
        {
            var interlockedBoolean = new InterlockedBoolean(true);

            Assert.IsTrue(interlockedBoolean.Value);
        }
 public void initial_value_true()
 {
     var interlockedBoolean = new InterlockedBoolean(true);
     Assert.IsTrue(interlockedBoolean.Value);
 }
 public void initial_value_false()
 {
     var interlockedBoolean = new InterlockedBoolean();
     Assert.IsFalse(interlockedBoolean.Value);
 }
Esempio n. 33
0
 public static bool EnsureCalledOnce(this InterlockedBoolean interlockedBoolean)
 {
     return(interlockedBoolean.CompareExchange(true, false));
 }
Esempio n. 34
0
 /// <inheritdoc />
 public int CompareTo(InterlockedBoolean other)
 {
     return(this.GetValue().CompareTo(other.GetValue()));
 }