public void AddDeltaAndReturnPreviousValue()
 {
     AtomicInteger ai = new AtomicInteger(1);
     Assert.AreEqual(1, ai.AddDeltaAndReturnPreviousValue(2));
     Assert.AreEqual(3, ai.Value);
     Assert.AreEqual(3, ai.AddDeltaAndReturnPreviousValue(-4));
     Assert.AreEqual(-1, ai.Value);
 }
 public void testSingleSelectorAccepts()
 {
     AtomicInteger counter = new AtomicInteger();
     IMessageSelector selector = new TestMessageSelector(true, counter);
     MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector);
     QueueChannel channel = new QueueChannel();
     channel.AddInterceptor(interceptor);
     Assert.That(channel.Send(new StringMessage("test1")), Is.True);
 }
 public void CompareExpectedValueAndSetNewValueInMultipleThreads()
 {
     AtomicInteger ai = new AtomicInteger(1);
     Thread t = ThreadManager.StartAndAssertRegistered(
         "T1", () => { while (!ai.CompareAndSet(2, 3)) Thread.Sleep(100); });
     Assert.IsTrue(ai.CompareAndSet(1, 2));
     ThreadManager.JoinAndVerify(Delays.Long);
     Assert.IsFalse(t.IsAlive);
     Assert.AreEqual(ai.Value, 3);
 }
 public void testMultipleSelectorsAccept()
 {
     AtomicInteger counter = new AtomicInteger();
     IMessageSelector selector1 = new TestMessageSelector(true, counter);
     IMessageSelector selector2 = new TestMessageSelector(true, counter);
     MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector1, selector2);
     QueueChannel channel = new QueueChannel();
     channel.AddInterceptor(interceptor);
     Assert.That(channel.Send(new StringMessage("test1")), Is.True);
     Assert.That(counter.Value, Is.EqualTo(2));
 }
 public void CompareExpectedValueAndSetNewValue()
 {
     AtomicInteger ai = new AtomicInteger(1);
     Assert.IsTrue(ai.CompareAndSet(1, 2));
     Assert.IsTrue(ai.CompareAndSet(2, -4));
     Assert.AreEqual(-4, ai.Value);
     Assert.IsFalse(ai.CompareAndSet(-5, 7));
     Assert.IsFalse((7 == ai.Value));
     Assert.IsTrue(ai.CompareAndSet(-4, 7));
     Assert.AreEqual(7, ai.Value);
 }
        public void ForInt32LocalSum(int from, int to)
        {
            var total = new AtomicInteger(0);
            var result = _executor.For(
                from, to,
                ()=>0,
                (i, s, l) => l+i,
                l=>total.AddDeltaAndReturnNewValue(l));

            Assert.That(result.IsCompleted, Is.True);
            Assert.That(total.Value, Is.EqualTo((from+to-1)*(to-from)/2));
        }
        public void ForInt32LocalSumWithParallelism(int from, int to, int parallelism)
        {
            var options = new ParallelOptions { MaxDegreeOfParallelism = parallelism };
            var total = new AtomicInteger(0);
            var result = _executor.For(
                from, to, options,
                () => 0,
                (i, s, l) => l + i,
                l => total.AddDeltaAndReturnNewValue(l));

            Assert.That(result.IsCompleted, Is.True);
            Assert.That(total.Value, Is.EqualTo((from + to - 1) * (to - from) / 2));
        }
 public void TestPostReceiveInterceptor()
 {
     AtomicInteger invokedCount = new AtomicInteger();
     AtomicInteger messageCount = new AtomicInteger();
     QueueChannel channel = new QueueChannel();
     channel.AddInterceptor(new TestPostReceiveInterceptorInterceptor(invokedCount, messageCount, channel));
     channel.Receive(TimeSpan.Zero);
     Assert.That(invokedCount.Value, Is.EqualTo(1));
     Assert.That(messageCount.Value, Is.EqualTo(0));
     channel.Send(new StringMessage("test"));
     IMessage result = channel.Receive(TimeSpan.Zero);
     Assert.IsNotNull(result);
     Assert.That(invokedCount.Value, Is.EqualTo(2));
     Assert.That(messageCount.Value, Is.EqualTo(1));
 }
 public void TestPostSendInterceptorWithUnsentMessage()
 {
     AtomicInteger invokedCounter = new AtomicInteger(0);
     AtomicInteger sentCounter = new AtomicInteger(0);
     QueueChannel singleItemChannel = new QueueChannel(1);
     singleItemChannel.AddInterceptor(new TestPostSendInterceptorWithUnsentMessageInterceptor(invokedCounter,
                                                                                              sentCounter,
                                                                                              singleItemChannel));
     Assert.That(invokedCounter.Value, Is.EqualTo(0));
     Assert.That(sentCounter.Value, Is.EqualTo(0));
     singleItemChannel.Send(new StringMessage("test1"));
     Assert.That(invokedCounter.Value, Is.EqualTo(1));
     Assert.That(sentCounter.Value, Is.EqualTo(1));
     singleItemChannel.Send(new StringMessage("test2"), TimeSpan.Zero);
     Assert.That(invokedCounter.Value, Is.EqualTo(2));
     Assert.That(sentCounter.Value, Is.EqualTo(1));
 }
 public void testMultipleSelectorsReject()
 {
     bool exceptionThrown = false;
     AtomicInteger counter = new AtomicInteger();
     IMessageSelector selector1 = new TestMessageSelector(true, counter);
     IMessageSelector selector2 = new TestMessageSelector(false, counter);
     IMessageSelector selector3 = new TestMessageSelector(false, counter);
     IMessageSelector selector4 = new TestMessageSelector(true, counter);
     MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector1, selector2, selector3,
                                                                               selector4);
     QueueChannel channel = new QueueChannel();
     channel.AddInterceptor(interceptor);
     try
     {
         channel.Send(new StringMessage("test1"));
     }
     catch (MessageDeliveryException)
     {
         exceptionThrown = true;
     }
     Assert.That(exceptionThrown, Is.True);
     Assert.That(counter.Value, Is.EqualTo(2));
 }
 public void GetLastLazySetValue()
 {
     AtomicInteger ai = new AtomicInteger(1);
     Assert.AreEqual(1, ai.Value);
     ai.LazySet(2);
     Assert.AreEqual(2, ai.Value);
     ai.LazySet(-3);
     Assert.AreEqual(-3, ai.Value);
 }
 public void WeakCompareExpectedValueAndSetNewValue()
 {
     AtomicInteger ai = new AtomicInteger(1);
     while (!ai.WeakCompareAndSet(1, 2)) { }
     while (!ai.WeakCompareAndSet(2, -4)) { }
     Assert.AreEqual(-4, ai.Value);
     while (!ai.WeakCompareAndSet(-4, 7)) { }
     Assert.AreEqual(7, ai.Value);
     Assert.IsFalse(ai.WeakCompareAndSet(-4, 7));
 }
 public void Value()
 {
     AtomicInteger ai = new AtomicInteger(42);
     Assert.AreEqual(ai.Value, 42);
 }
Ejemplo n.º 14
0
 internal AtomicLoop(long iters, AtomicInteger obj, CyclicBarrier b)
 {
     this.iters = iters;
     this.obj = obj;
     this.barrier = b;
     obj.Value = (CASLoops.rng.next());
 }
        public void DefaultConstructor()
        {
            AtomicInteger ai = new AtomicInteger();

            Assert.AreEqual(0, ai.Value);
        }
        public void Value()
        {
            AtomicInteger ai = new AtomicInteger(42);

            Assert.AreEqual(ai.Value, 42);
        }
 public void DecrementValueAndReturn()
 {
     AtomicInteger ai = new AtomicInteger(1);
     Assert.AreEqual(0, ai.DecrementValueAndReturn());
     Assert.AreEqual(-1, ai.DecrementValueAndReturn());
     Assert.AreEqual(-2, ai.DecrementValueAndReturn());
     Assert.AreEqual(-2, ai.Value);
 }
 public TestMessageSelector(bool shouldAccept, AtomicInteger counter)
 {
     _shouldAccept = shouldAccept;
     _counter = counter;
 }
 public void Exchange()
 {
     AtomicInteger ai = new AtomicInteger(1);
     Assert.AreEqual(1, ai.Exchange(0));
     Assert.AreEqual(0, ai.Exchange(-10));
     Assert.AreEqual(-10, ai.Exchange(1));
 }
 public void DefaultConstructor()
 {
     AtomicInteger ai = new AtomicInteger();
     Assert.AreEqual(0, ai.Value);
 }
 public void testSingleSelectorRejects()
 {
     AtomicInteger counter = new AtomicInteger();
     IMessageSelector selector = new TestMessageSelector(false, counter);
     MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector);
     QueueChannel channel = new QueueChannel();
     channel.AddInterceptor(interceptor);
     channel.Send(new StringMessage("test1"));
 }
 public void Constructor()
 {
     AtomicInteger ai = new AtomicInteger(1);
     Assert.AreEqual(1, ai.Value);
 }
 public void GetLastSetValue()
 {
     AtomicInteger ai = new AtomicInteger(1);
     Assert.AreEqual(1, ai.Value);
     ai.Value = 2;
     Assert.AreEqual(2, ai.Value);
     ai.Value = -3;
     Assert.AreEqual(-3, ai.Value);
 }
        public void ShutdownNowReturnsTasksThatWereNotRun()
        {
            var nRun = new AtomicInteger(0);
            var action = ThreadManager.GetManagedAction(
                () =>
                {
                    nRun.IncrementValueAndReturn();
                    try { Thread.Sleep(Delays.Medium); }
                    catch (ThreadInterruptedException) { }
                });
            var es = ExecutorService;
            int submitCount = 0;

            try
            {
                for (int i = 0; i < 100; i++)
                {
                    es.Execute(action);
                    submitCount++;
                }
            }
            catch (RejectedExecutionException) { }

            IList<IRunnable> l = es.ShutdownNow();
            Assert.IsTrue(es.IsShutdown);
            Assert.IsNotNull(l);
            Assert.That(l.Count, Is.LessThanOrEqualTo(submitCount-nRun.Value));
            ThreadManager.JoinAndVerify(Delays.Long);
        }
 public void ImplicitConverter()
 {
     AtomicInteger ai = new AtomicInteger(1);
     int result = ai;
     Assert.AreEqual(1, result);
     ai.Value = 3;
     result = ai;
     Assert.AreEqual(3, result);
 }
        public void Constructor()
        {
            AtomicInteger ai = new AtomicInteger(1);

            Assert.AreEqual(1, ai.Value);
        }
 public void ReturnValueAndDecrement()
 {
     AtomicInteger ai = new AtomicInteger(1);
     Assert.AreEqual(1, ai.ReturnValueAndDecrement());
     Assert.AreEqual(0, ai.ReturnValueAndDecrement());
     Assert.AreEqual(-1, ai.ReturnValueAndDecrement());
 }
Ejemplo n.º 28
0
 //    static long runUpdaterAtomic(int n, long iters) throws Exception {
 //        LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
 //        CyclicBarrier b = new CyclicBarrier(n+1, timer);
 //        UpdaterAtomicInteger a = new UpdaterAtomicInteger();
 //        for (int j = 0; j < n; ++j)
 //            new Thread(new UpdaterAtomicLoop(iters, a, b)).start();
 //        b.await();
 //        b.await();
 //        if (sum.get() == 0) System.out.print(" ");
 //        return timer.getTime();
 //    }
 //
 internal static long runAtomic(int n, long iters)
 {
     LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
     CyclicBarrier b = new CyclicBarrier(n + 1, timer);
     AtomicInteger a = new AtomicInteger();
     for (int j = 0; j < n; ++j)
         new SupportClass.ThreadClass(new System.Threading.ThreadStart(new AtomicLoop(iters, a, b).Run)).Start();
     b.Await();
     b.Await();
     if (sum.Value == 0)
         System.Console.Out.Write(" ");
     return timer.Time;
 }
 public void ReturnValueAndIncrement()
 {
     AtomicInteger ai = new AtomicInteger(1);
     Assert.AreEqual(1, ai.ReturnValueAndIncrement());
     Assert.AreEqual(2, ai.Value);
     ai.Value = -2;
     Assert.AreEqual(-2, ai.ReturnValueAndIncrement());
     Assert.AreEqual(-1, ai.ReturnValueAndIncrement());
     Assert.AreEqual(0, ai.ReturnValueAndIncrement());
     Assert.AreEqual(1, ai.Value);
 }
        public void SerializationAndDeserialization()
        {
            AtomicInteger l = new AtomicInteger();

            l.Value = 22;
            MemoryStream bout = new MemoryStream(10000);

            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(bout, l);

            MemoryStream bin = new MemoryStream(bout.ToArray());
            BinaryFormatter formatter2 = new BinaryFormatter();
            AtomicInteger r = (AtomicInteger)formatter2.Deserialize(bin);
            Assert.AreEqual(l.Value, r.Value);
        }
 public void ToStringTest()
 {
     AtomicInteger ai = new AtomicInteger();
     for (int i = -12; i < 6; ++i)
     {
         ai.Value = i;
         Assert.AreEqual(ai.ToString(), Convert.ToString(i));
     }
 }
        public void TestWithListener()
        {
            var mocker = new AutoMoqer();

            var mockConnectionFactory = mocker.GetMock<RabbitMQ.Client.ConnectionFactory>();
            var mockConnection = mocker.GetMock<RabbitMQ.Client.IConnection>();

            mockConnectionFactory.Setup(c => c.CreateConnection()).Returns(mockConnection.Object);

            var called = new AtomicInteger(0);
            var connectionFactory = new CachingConnectionFactory(mockConnectionFactory.Object);

            var mockConnectionListener = new Mock<IConnectionListener>();
            mockConnectionListener.Setup(m => m.OnCreate(It.IsAny<IConnection>())).Callback((IConnection conn) => called.IncrementValueAndReturn());
            mockConnectionListener.Setup(m => m.OnClose(It.IsAny<IConnection>())).Callback((IConnection conn) => called.DecrementValueAndReturn());

            connectionFactory.ConnectionListeners = new List<IConnectionListener>() { mockConnectionListener.Object };

            var con = connectionFactory.CreateConnection();
            Assert.AreEqual(1, called.Value);

            con.Close();
            Assert.AreEqual(1, called.Value);
            mockConnection.Verify(c => c.Close(), Times.Never());

            connectionFactory.CreateConnection();
            Assert.AreEqual(1, called.Value);

            connectionFactory.Dispose();
            Assert.AreEqual(0, called.Value);
            mockConnection.Verify(c => c.Close(), Times.AtLeastOnce());

            mockConnectionFactory.Verify(c => c.CreateConnection(), Times.Exactly(1));
        }
 public TestPostSendInterceptorWithUnsentMessageInterceptor(AtomicInteger invokedCounter,
                                                            AtomicInteger sentCounter,
                                                            IMessageChannel channel)
 {
     _invokedCounter = invokedCounter;
     _sentCounter = sentCounter;
     _channel = channel;
 }