コード例 #1
0
ファイル: EventPollerTest.cs プロジェクト: zzms/Disruptor-net
        public void ShouldPollForEvents()
        {
            var pollSequence   = new Sequence();
            var bufferSequence = new Sequence();
            var gatingSequence = new Sequence();
            var sequencerMock  = new Mock <ISequencer>();
            var sequencer      = sequencerMock.Object;
            var handled        = false;
            Func <object, long, bool, bool> handler = (ev, seq, end) =>
            {
                handled = true;
                return(false);
            };
            var providerMock = new Mock <IDataProvider <object> >();
            var provider     = providerMock.Object;
            var poller       = EventPoller <object> .NewInstance(provider, sequencer, pollSequence, bufferSequence, gatingSequence);

            var @event = new object();

            object states = PollState.Idle;

            sequencerMock.SetupGet(x => x.Cursor)
            .Returns(() =>
            {
                switch ((PollState)states)
                {
                case PollState.Processing:
                    return(0L);

                case PollState.Gating:
                    return(0L);

                case PollState.Idle:
                    return(-1L);

                default:
                    throw new ArgumentOutOfRangeException();
                }
            });

            sequencerMock.Setup(x => x.GetHighestPublishedSequence(0L, -1L)).Returns(-1L);
            sequencerMock.Setup(x => x.GetHighestPublishedSequence(0L, 0L)).Returns(0L);

            providerMock.Setup(x => x[0]).Returns(() => (PollState)states == PollState.Processing ? @event : null);

            // Initial State - nothing published.
            states = PollState.Idle;
            Assert.That(poller.Poll(handler), Is.EqualTo(PollState.Idle));

            // Publish Event.
            states = PollState.Gating;
            bufferSequence.IncrementAndGet();
            Assert.That(poller.Poll(handler), Is.EqualTo(PollState.Gating));

            states = PollState.Processing;
            gatingSequence.IncrementAndGet();
            Assert.That(poller.Poll(handler), Is.EqualTo(PollState.Processing));

            Assert.That(handled, Is.True);
        }
コード例 #2
0
 /// <summary>
 /// Creates an event poller for this sequence that will use the supplied data provider and
 /// gating sequences.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="provider">The data source for users of this event poller</param>
 /// <param name="gatingSequences">Sequence to be gated on.</param>
 /// <returns>A poller that will gate on this ring buffer and the supplied sequences.</returns>
 public EventPoller <T> NewPoller <T>(IDataProvider <T> provider, params ISequence[] gatingSequences)
 {
     return(EventPoller <T> .NewInstance(provider, this, new Sequence(), _cursor, gatingSequences));
 }