Ejemplo n.º 1
0
		public void ValidityTest ()
		{
			var header1 = new DataflowMessageHeader ();
			var header2 = new DataflowMessageHeader (2);

			Assert.IsFalse (header1.IsValid);
			Assert.IsTrue (header2.IsValid);
		}
Ejemplo n.º 2
0
		public void EqualityTest ()
		{
			var header1 = new DataflowMessageHeader (2);
			var header2 = new DataflowMessageHeader (5);
			var header3 = new DataflowMessageHeader (2);

			Assert.AreEqual (header1, header1);
			Assert.AreEqual (header1.GetHashCode (), header1.GetHashCode ());
			Assert.AreEqual (header1, header3);
			Assert.AreEqual (header1.GetHashCode (), header3.GetHashCode ());
			Assert.AreNotEqual (header1, header2);
			Assert.AreNotEqual (header1.GetHashCode (), header2.GetHashCode ());
		}
 public void ReleaseReservation(DataflowMessageHeader messageHeader, ITargetBlock <L7PDU> target)
 {
     this._L7Pdus.ReleaseReservation(messageHeader, target);
 }
 public PmFrameBase ConsumeMessage(DataflowMessageHeader messageHeader, ITargetBlock <PmFrameBase> target, out bool messageConsumed)
 {
     return(this._Frames.ConsumeMessage(messageHeader, target, out messageConsumed));
 }
 public L7ConversationStatistics ConsumeMessage(DataflowMessageHeader messageHeader, ITargetBlock <L7ConversationStatistics> target, out bool messageConsumed)
 {
     return(this._L7ConversationStatistics.ConsumeMessage(messageHeader, target, out messageConsumed));
 }
 public bool ReserveMessage(DataflowMessageHeader messageHeader, ITargetBlock <L7ConversationStatistics> target)
 {
     return(this._L7ConversationStatistics.ReserveMessage(messageHeader, target));
 }
Ejemplo n.º 7
0
    public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, 
      Bucket messageValue, 
      ISourceBlock<Bucket> source, 
      bool consumeToAccept)
    {
      var lines = messageValue.ToLines();
      _bufferBlock.Post(lines);

      return DataflowMessageStatus.Accepted;
    }
 public void ReleaseReservation(DataflowMessageHeader messageHeader, ITargetBlock <L4Conversation> target)
 {
     this._L4Conversations.ReleaseReservation(messageHeader, target);
 }
Ejemplo n.º 9
0
 /// <include file='XmlDocs\CommonXmlDocComments.xml' path='CommonXmlDocComments/Targets/Member[@name="OfferMessage"]/*' />
 DataflowMessageStatus ITargetBlock <T> .OfferMessage(DataflowMessageHeader messageHeader, T messageValue, ISourceBlock <T> source, Boolean consumeToAccept)
 {
     Contract.Assert(source == _owningSource, "Only valid to be used with the source for which it was created.");
     return(_target.OfferMessage(messageHeader, messageValue, this, consumeToAccept));
 }
Ejemplo n.º 10
0
		public void MaxNumberOfGroupsWithConsumeToAcceptTest ()
		{
			ITargetBlock<int> block = new BatchBlock<int> (1,
				new GroupingDataflowBlockOptions { MaxNumberOfGroups = 1 });

			var evt = new ManualResetEventSlim ();

			Func<Task<Tuple<DataflowMessageStatus, bool>>> startTask =
				() => Task.Factory.StartNew (
					() =>
					{
						var sourceBlock = new TestSourceBlock<int> { ConsumeWaiter = evt.Wait };
						var header = new DataflowMessageHeader (1);
						sourceBlock.AddMessage (header, 1);
						var status = block.OfferMessage (header, 1, sourceBlock, true);

						return Tuple.Create (status, sourceBlock.WasConsumed (header));
					});

			var task1 = startTask ();
			var task2 = startTask ();

			Thread.Sleep (100);

			Assert.IsFalse (task1.IsCompleted);
			Assert.IsFalse (task2.IsCompleted);

			evt.Set ();

			Assert.IsTrue (Task.WaitAll (new Task[] { task1, task2 }, 1000));

			CollectionAssert.AreEquivalent (
				new[]
				{
					Tuple.Create (DataflowMessageStatus.Accepted, true),
					Tuple.Create (DataflowMessageStatus.DecliningPermanently, false)
				},
				new[] { task1.Result, task2.Result });
		}
Ejemplo n.º 11
0
 TSplit ISourceBlock <TSplit> .ConsumeMessage(DataflowMessageHeader messageHeader, ITargetBlock <TSplit> target, out bool messageConsumed)
 {
     return(orderCapturer.ConsumeMessage(messageHeader, target, out messageConsumed));
 }
Ejemplo n.º 12
0
		public void NonGreedyMaxNumberOfGroupsTest ()
		{
			var scheduler = new TestScheduler ();
			var block = new JoinBlock<int, int> (
				new GroupingDataflowBlockOptions
				{ MaxNumberOfGroups = 1, Greedy = false, TaskScheduler = scheduler });
			var source1 = new TestSourceBlock<int> ();
			var source2 = new TestSourceBlock<int> ();

			var header1 = new DataflowMessageHeader (1);
			source1.AddMessage (header1, 11);
			source2.AddMessage (header1, 21);

			Assert.AreEqual (DataflowMessageStatus.Postponed,
				block.Target1.OfferMessage (header1, 11, source1, false));
			Assert.AreEqual (DataflowMessageStatus.Postponed,
				block.Target2.OfferMessage (header1, 21, source2, false));

			scheduler.ExecuteAll ();

			Assert.IsTrue (source1.WasConsumed (header1));
			Assert.IsTrue (source2.WasConsumed (header1));

			var header2 = new DataflowMessageHeader (2);
			Assert.AreEqual (DataflowMessageStatus.DecliningPermanently,
				block.Target1.OfferMessage (header2, 21, source1, false));

			Tuple<int, int> tuple;
			Assert.IsTrue (block.TryReceive (out tuple));
			Assert.AreEqual (Tuple.Create (11, 21), tuple);

			Assert.IsTrue (block.Completion.Wait (1000));
		}
Ejemplo n.º 13
0
 bool ISourceBlock <TSplit> .ReserveMessage(DataflowMessageHeader messageHeader, ITargetBlock <TSplit> target)
 {
     return(orderCapturer.ReserveMessage(messageHeader, target));
 }
Ejemplo n.º 14
0
        public void TestSourceCoreSpecifics()
        {
            var messageHeader = new DataflowMessageHeader(1);
            bool consumed;
            var block = new BufferBlock<int>();
            ((ITargetBlock<int>)block).OfferMessage(messageHeader, 42, null, false);

            var target = new ActionBlock<int>(i => { });
            Assert.True(((ISourceBlock<int>)block).ReserveMessage(messageHeader, target));
            ((ISourceBlock<int>)block).ReleaseReservation(messageHeader, target);

            ((ISourceBlock<int>)block).ConsumeMessage(messageHeader, DataflowBlock.NullTarget<int>(), out consumed);
            
            Assert.True(consumed);
            Assert.Equal(expected: 0, actual: block.Count);
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Offers the message.
 /// </summary>
 /// <param name="messageHeader">The message header.</param>
 /// <param name="messageValue">The message value.</param>
 /// <param name="source">The source.</param>
 /// <param name="consumeToAccept">The consume to accept.</param>
 /// <returns>DataflowMessageStatus.</returns>
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, LogEntry messageValue, ISourceBlock<LogEntry> source, Boolean consumeToAccept)
 {
     return _BatchBlock.OfferMessage(messageHeader, messageValue, source, consumeToAccept);
 }
 public bool ReserveMessage(DataflowMessageHeader messageHeader, ITargetBlock <PmFrameBase> target)
 {
     return(this._Frames.ReserveMessage(messageHeader, target));
 }
Ejemplo n.º 17
0
		public void BoundedPostponedTest ()
		{
			var scheduler = new TestScheduler ();
			var broadcast = new BroadcastBlock<int> (
				null,
				new DataflowBlockOptions { TaskScheduler = scheduler, BoundedCapacity = 1 });
			ITargetBlock<int> target = broadcast;
			var source = new TestSourceBlock<int> ();

			Assert.IsTrue (broadcast.Post (1));
			var header = new DataflowMessageHeader (1);
			source.AddMessage (header, 2);
			Assert.AreEqual (DataflowMessageStatus.Postponed,
				target.OfferMessage (header, 2, source, false));
			Assert.IsFalse (source.WasConsumed (header));

			scheduler.ExecuteAll ();

			Assert.IsTrue (source.WasConsumed (header));
		}
Ejemplo n.º 18
0
 /// <summary>
 /// Called by a linked ITargetBlock<TInput> to
 /// accept and consume a DataflowMessageHeader
 /// previously offered by this ISourceBlock<TOutput>.
 /// </summary>
 /// <param name="messageHeader">
 /// The DataflowMessageHeader of the message being consumed.
 /// </param>
 /// <param name="target">
 /// The ITargetBlock<TInput> consuming the message
 /// </param>
 /// <param name="messageConsumed">
 /// True if the message was successfully consumed. False otherwise.
 /// </param>
 /// <returns>
 /// The value of the consumed message. This may correspond to a different DataflowMessageHeader
 /// instance than was previously reserved and passed as the messageHeader to
 /// ConsumeMessage. The consuming ITargetBlock<TInput>
 /// must use the returned value instead of the value passed as messageValue through
 /// OfferMessage.
 /// If the message requested is not available, the return value will be null.
 /// </returns>
 /// <exception>
 /// System.ArgumentException: The messageHeader is not valid.
 /// </exception>
 /// <exception>
 /// System.ArgumentNullException: The target is null.
 /// </exception>
 /// <remarks>
 /// Only ITargetBlock<TInput> instances linked
 /// to this ISourceBlock<TOutput> instance may
 /// use ConsumeMessage, and it must only be used to reserve DataflowMessageHeader
 /// instances previously offered by this source to the target.
 /// </remarks>
 #endregion // Documentation
 public long ConsumeMessage(DataflowMessageHeader messageHeader, ITargetBlock <long> target, out bool messageConsumed)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 19
0
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, T messageValue, ISourceBlock <T> source, bool consumeToAccept)
 {
     _buffer.Push(messageValue);
     return(DataflowMessageStatus.Accepted);
 }
Ejemplo n.º 20
0
 /// <include file='XmlDocs\CommonXmlDocComments.xml' path='CommonXmlDocComments/Sources/Member[@name="ConsumeMessage"]/*' />
 T ISourceBlock <T> .ConsumeMessage(DataflowMessageHeader messageHeader, ITargetBlock <T> target, out Boolean messageConsumed)
 {
     return(_owningSource.ConsumeMessage(messageHeader, this, out messageConsumed));
 }
Ejemplo n.º 21
0
		public void ConsumeToAcceptTest ()
		{
			var source = new TestSourceBlock<int> ();
			var buffer = new BufferBlock<int> ();
			var target = (ITargetBlock<int>)buffer;

			var header = new DataflowMessageHeader (1);
			source.AddMessage (header, 1);

			Assert.AreEqual (DataflowMessageStatus.Accepted,
				target.OfferMessage (header, 1, source, true));

			Assert.IsTrue (source.WasConsumed (header));
			Assert.IsFalse (source.WasReserved (header));
		}
Ejemplo n.º 22
0
        public void TestSourceCoreSpecificsThroughBufferBlock()
        {
            var messageHeader = new DataflowMessageHeader(1);
            bool consumed;
            var block = new BufferBlock<int>();
            ((ITargetBlock<int>)block).OfferMessage(messageHeader, 42, null, false);

            var nonlinkedTarget = new ActionBlock<int>(i => { });
            bool reserved = ((ISourceBlock<int>)block).ReserveMessage(messageHeader, nonlinkedTarget);
            Assert.False(!reserved, "Failure: SourceCore did not allow a non-linked target to reserve");

            ((ISourceBlock<int>)block).ReleaseReservation(messageHeader, nonlinkedTarget);
            ((ISourceBlock<int>)block).ConsumeMessage(messageHeader, new ActionBlock<int>(i => { }), out consumed);
            Assert.False(!consumed || block.Count != 0, "Failure: SourceCore did not allow a non-linked target to consume");
        }
Ejemplo n.º 23
0
 /// <include file='XmlDocs\CommonXmlDocComments.xml' path='CommonXmlDocComments/Sources/Member[@name="ReserveMessage"]/*' />
 bool ISourceBlock <T> .ReserveMessage(DataflowMessageHeader messageHeader, ITargetBlock <T> target)
 {
     return(_owningSource.ReserveMessage(messageHeader, this));
 }
 public L4Conversation ConsumeMessage(DataflowMessageHeader messageHeader, ITargetBlock <L4Conversation> target, out bool messageConsumed)
 {
     return(this._L4Conversations.ConsumeMessage(messageHeader, target, out messageConsumed));
 }
Ejemplo n.º 25
0
 /// <include file='XmlDocs\CommonXmlDocComments.xml' path='CommonXmlDocComments/Sources/Member[@name="ReleaseReservation"]/*' />
 void ISourceBlock <T> .ReleaseReservation(DataflowMessageHeader messageHeader, ITargetBlock <T> target)
 {
     _owningSource.ReleaseReservation(messageHeader, this);
 }
 public bool ReserveMessage(DataflowMessageHeader messageHeader, ITargetBlock <L4Conversation> target)
 {
     return(this._L4Conversations.ReserveMessage(messageHeader, target));
 }
Ejemplo n.º 27
0
		public void NullTargetTest ()
		{
			var target = DataflowBlock.NullTarget<int> ();
			Assert.IsTrue (target.Post (1));

			var source = new TestSourceBlock<int> ();
			var header = new DataflowMessageHeader (1);
			source.AddMessage (header, 2);

			Assert.IsFalse (source.WasConsumed (header));

			Assert.AreEqual (DataflowMessageStatus.Accepted,
				target.OfferMessage (header, 2, source, true));
			Assert.IsTrue (source.WasConsumed (header));

			Assert.IsFalse (target.Completion.Wait (100));

			target.Complete ();

			Assert.IsFalse (target.Completion.Wait (100));

			target.Fault (new Exception ());

			Assert.IsFalse (target.Completion.Wait (100));
		}
 public void ReleaseReservation(DataflowMessageHeader messageHeader, ITargetBlock <L7ConversationStatistics> target)
 {
     this._L7ConversationStatistics.ReleaseReservation(messageHeader, target);
 }
Ejemplo n.º 29
0
 void ISourceBlock <Tuple <T1, T2> > .ReleaseReservation(
     DataflowMessageHeader messageHeader, ITargetBlock <Tuple <T1, T2> > target)
 {
     ((ISourceBlock <Tuple <T1, T2> >)_transformManyBlock)
     .ReleaseReservation(messageHeader, target);
 }
 public L7PDU ConsumeMessage(DataflowMessageHeader messageHeader, ITargetBlock <L7PDU> target, out bool messageConsumed)
 {
     return(this._L7Pdus.ConsumeMessage(messageHeader, target, out messageConsumed));
 }
Ejemplo n.º 31
0
 bool ISourceBlock <Tuple <T1, T2> > .ReserveMessage(
     DataflowMessageHeader messageHeader, ITargetBlock <Tuple <T1, T2> > target)
 {
     return(((ISourceBlock <Tuple <T1, T2> >)_transformManyBlock)
            .ReserveMessage(messageHeader, target));
 }
 public bool ReserveMessage(DataflowMessageHeader messageHeader, ITargetBlock <L7PDU> target)
 {
     return(this._L7Pdus.ReserveMessage(messageHeader, target));
 }
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, T messageValue, ISourceBlock <T> source, bool consumeToAccept)
 => _target.OfferMessage(messageHeader, messageValue, source, consumeToAccept);
 public void ReleaseReservation(DataflowMessageHeader messageHeader, ITargetBlock <PmFrameBase> target)
 {
     this._Frames.ReleaseReservation(messageHeader, target);
 }
Ejemplo n.º 35
0
        /// <include file='XmlDocs\CommonXmlDocComments.xml' path='CommonXmlDocComments/Targets/Member[@name="OfferMessage"]/*' />
        internal DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, TInput messageValue, ISourceBlock <TInput> source, Boolean consumeToAccept)
        {
            // Validate arguments
            if (!messageHeader.IsValid)
            {
                throw new ArgumentException(SR.Argument_InvalidMessageHeader, "messageHeader");
            }
            if (source == null && consumeToAccept)
            {
                throw new ArgumentException(SR.Argument_CantConsumeFromANullSource, "consumeToAccept");
            }
            Contract.EndContractBlock();

            lock (IncomingLock)
            {
                // If we shouldn't be accepting more messages, don't.
                if (_decliningPermanently)
                {
                    CompleteBlockIfPossible();
                    return(DataflowMessageStatus.DecliningPermanently);
                }

                // We can directly accept the message if:
                //      1) we are not bounding, OR
                //      2) we are bounding AND there is room available AND there are no postponed messages AND no messages are currently being transfered to the input queue.
                // (If there were any postponed messages, we would need to postpone so that ordering would be maintained.)
                // (Unlike all other blocks, TargetCore can accept messages while processing, because
                // input message IDs are properly assigned and the correct order is preserved.)
                if (_boundingState == null ||
                    (_boundingState.OutstandingTransfers == 0 && _boundingState.CountIsLessThanBound && _boundingState.PostponedMessages.Count == 0))
                {
                    // Consume the message from the source if necessary
                    if (consumeToAccept)
                    {
                        Debug.Assert(source != null, "We must have thrown if source == null && consumeToAccept == true.");

                        bool consumed;
                        messageValue = source.ConsumeMessage(messageHeader, _owningTarget, out consumed);
                        if (!consumed)
                        {
                            return(DataflowMessageStatus.NotAvailable);
                        }
                    }

                    // Assign a message ID - strictly sequential, no gaps.
                    // Once consumed, enqueue the message with its ID and kick off asynchronous processing.
                    long messageId = _nextAvailableInputMessageId.Value++;
                    Debug.Assert(messageId != Common.INVALID_REORDERING_ID, "The assigned message ID is invalid.");
                    if (_boundingState != null)
                    {
                        _boundingState.CurrentCount += 1;                         // track this new item against our bound
                    }
                    _messages.Enqueue(new KeyValuePair <TInput, long>(messageValue, messageId));
                    ProcessAsyncIfNecessary();
                    return(DataflowMessageStatus.Accepted);
                }
                // Otherwise, we try to postpone if a source was provided
                else if (source != null)
                {
                    Debug.Assert(_boundingState != null && _boundingState.PostponedMessages != null,
                                 "PostponedMessages must have been initialized during construction in non-greedy mode.");

                    // Store the message's info and kick off asynchronous processing
                    _boundingState.PostponedMessages.Push(source, messageHeader);
                    ProcessAsyncIfNecessary();
                    return(DataflowMessageStatus.Postponed);
                }
                // We can't do anything else about this message
                return(DataflowMessageStatus.Declined);
            }
        }
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, PmFrameBase messageValue, ISourceBlock <PmFrameBase> source, bool consumeToAccept)
 {
     return(this._ProcessFrame.OfferMessage(messageHeader, messageValue, source, consumeToAccept));
 }
Ejemplo n.º 37
0
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, Bucket messageValue,
                                           ISourceBlock <Bucket> source, bool consumeToAccept)
 {
     messageValue.FeedTarget(_batchBlock);
     return(DataflowMessageStatus.Accepted);
 }
Ejemplo n.º 38
0
		public void NonGreedyMaxNumberOfGroupsTest ()
		{
			var scheduler = new TestScheduler ();
			var block = new BatchBlock<int> (2,
				new GroupingDataflowBlockOptions
				{ MaxNumberOfGroups = 1, Greedy = false, TaskScheduler = scheduler });
			ITargetBlock<int> target = block;
			var source1 = new TestSourceBlock<int> ();
			var source2 = new TestSourceBlock<int> ();

			var header1 = new DataflowMessageHeader (1);
			source1.AddMessage (header1, 11);
			source2.AddMessage (header1, 21);

			Assert.AreEqual (DataflowMessageStatus.Postponed,
				target.OfferMessage (header1, 11, source1, false));
			Assert.AreEqual (DataflowMessageStatus.Postponed,
				target.OfferMessage (header1, 21, source2, false));

			scheduler.ExecuteAll ();

			Assert.IsTrue (source1.WasConsumed (header1));
			Assert.IsTrue (source2.WasConsumed (header1));

			var header2 = new DataflowMessageHeader (2);
			Assert.AreEqual (DataflowMessageStatus.DecliningPermanently,
				target.OfferMessage (header2, 21, source1, false));

			int[] batch;
			Assert.IsTrue (block.TryReceive (out batch));
			CollectionAssert.AreEquivalent (new[] { 11, 21 }, batch);

			Assert.IsTrue (block.Completion.Wait (1000));
		}
Ejemplo n.º 39
0
 /// <inheritdoc />
 public DataflowMessageStatus OfferMessage(
     DataflowMessageHeader messageHeader, T messageValue, ISourceBlock <T> source, bool consumeToAccept)
 {
     ResetTimer();
     return(((IPropagatorBlock <T, T[]>)_batchBlock).OfferMessage(messageHeader, messageValue, source, consumeToAccept));
 }
Ejemplo n.º 40
0
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, Bucket bucket, ISourceBlock<Bucket> source, bool consumeToAccept)
 {
   Console.WriteLine(bucket.ToString());
   return DataflowMessageStatus.Accepted;
 }
Ejemplo n.º 41
0
 /// <inheritdoc />
 public T[] ConsumeMessage(DataflowMessageHeader messageHeader, ITargetBlock <T[]> target, out bool messageConsumed)
 {
     return(((IPropagatorBlock <T, T[]>)_batchBlock).ConsumeMessage(messageHeader, target, out messageConsumed));
 }
Ejemplo n.º 42
0
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader,
   Bucket messageValue,
   ISourceBlock<Bucket> source,
   bool consumeToAccept)
 {
   _preprocessorBlock.Post(messageValue);
   return DataflowMessageStatus.Accepted;
 }
Ejemplo n.º 43
0
 /// <inheritdoc />
 public bool ReserveMessage(DataflowMessageHeader messageHeader, ITargetBlock <T[]> target)
 {
     return(((IPropagatorBlock <T, T[]>)_batchBlock).ReserveMessage(messageHeader, target));
 }
Ejemplo n.º 44
0
 /// <inheritdoc />
 public void ReleaseReservation(DataflowMessageHeader messageHeader, ITargetBlock <T[]> target)
 {
     ((IPropagatorBlock <T, T[]>)_batchBlock).ReleaseReservation(messageHeader, target);
 }
Ejemplo n.º 45
0
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, Bucket messageValue, ISourceBlock<Bucket> source, bool consumeToAccept)
 {
   messageValue.FeedTarget(_batchBlock);
   return DataflowMessageStatus.Accepted;
 }