Exemple #1
0
        public void TestClose()
        {
            var dispatcher = new ManyToOneConcurrentArrayQueueDispatcher(MailboxSize, 2, 4, 10);

            dispatcher.Start();
            var mailbox = dispatcher.Mailbox;
            var actor   = new CountTakerActor();

            actor.Until = Until(MailboxSize);
            for (var i = 1; i <= MailboxSize; ++i)
            {
                var countParam = i;
                Action <ICountTaker> consumer = consumerActor => consumerActor.Take(countParam);
                var message = new LocalMessage <ICountTaker>(actor, consumer, "Take(int)");
                mailbox.Send(message);
            }

            actor.Until.Completes();
            dispatcher.Close();

            const int neverReceived = MailboxSize * 2;

            for (var count = MailboxSize + 1; count <= neverReceived; ++count)
            {
                var countParam = count;
                Action <ICountTaker> consumer = consumerActor => consumerActor.Take(countParam);
                var message = new LocalMessage <ICountTaker>(actor, consumer, "Take(int)");
                mailbox.Send(message);
            }

            Assert.Equal(MailboxSize, actor.Highest.Get());
        }
Exemple #2
0
        public void TestNotifyOnSendDispatch()
        {
            var mailboxSize = 64;
            var testResults = new TestResults(mailboxSize);

            var dispatcher = new ManyToOneConcurrentArrayQueueDispatcher(mailboxSize, 1000, true, 4, 10);

            dispatcher.Start();

            var mailbox = dispatcher.Mailbox;

            var actor = new CountTakerActor(testResults);

            for (var count = 1; count <= mailboxSize; ++count)
            {
                var countParam = count;
                Action <ICountTaker> consumer = consumerActor => consumerActor.Take(countParam);
                var localMessage = new LocalMessage <ICountTaker>(actor, consumer, "take(int)");

                // notify if in back off
                mailbox.Send(localMessage);

                // every third message give time for dispatcher to back off
                if (count % 3 == 0)
                {
                    Thread.Sleep(50);
                }
            }

            Assert.Equal(mailboxSize, testResults.GetHighest());
        }
    public IMailbox ProvideMailboxFor(int?hashCode, IDispatcher?dispatcher)
    {
        ManyToOneConcurrentArrayQueueDispatcher?maybeDispatcher;

        if (!hashCode.HasValue)
        {
            throw new ArgumentNullException(nameof(hashCode), "Cannot provide mailbox because the hashCode is null.");
        }

        if (dispatcher != null)
        {
            maybeDispatcher = (ManyToOneConcurrentArrayQueueDispatcher)dispatcher;
        }
        else
        {
            _dispatchers.TryGetValue(hashCode.Value, out maybeDispatcher);
        }

        if (maybeDispatcher == null)
        {
            var newDispatcher = new ManyToOneConcurrentArrayQueueDispatcher(
                _configuration.RingSize,
                _configuration.FixedBackoff,
                _configuration.NotifyOnSend,
                _configuration.DispatcherThrottlingCount,
                _configuration.SendRetires);

            var otherDispatcher = _dispatchers.GetOrAdd(hashCode.Value, newDispatcher);

            otherDispatcher.Start();
            return(otherDispatcher.Mailbox);
        }
        return(maybeDispatcher.Mailbox);
    }
Exemple #4
0
        public void TestBasicDispatch()
        {
            var testResult = new TestResults(MailboxSize);
            var dispatcher = new ManyToOneConcurrentArrayQueueDispatcher(MailboxSize, 2, false, 4, 10);

            dispatcher.Start();
            var mailbox = dispatcher.Mailbox;
            var actor   = new CountTakerActor(testResult);

            for (var count = 1; count <= MailboxSize; ++count)
            {
                var countParam = count;
                Action <ICountTaker> consumer = consumerActor => consumerActor.Take(countParam);
                var message = new LocalMessage <ICountTaker>(actor, consumer, "Take(int)");
                mailbox.Send(message);
            }

            Assert.Equal(MailboxSize, testResult.GetHighest());
        }