public void ValidityTest ()
		{
			var header1 = new DataflowMessageHeader ();
			var header2 = new DataflowMessageHeader (2);

			Assert.IsFalse (header1.IsValid);
			Assert.IsTrue (header2.IsValid);
		}
		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 ());
		}
Example #3
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));
		}
Example #4
0
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader,
   Bucket messageValue,
   ISourceBlock<Bucket> source,
   bool consumeToAccept)
 {
   _preprocessorBlock.Post(messageValue);
   return DataflowMessageStatus.Accepted;
 }
Example #5
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 });
		}
Example #6
0
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, Bucket bucket, ISourceBlock<Bucket> source, bool consumeToAccept)
 {
   Console.WriteLine(bucket.ToString());
   return DataflowMessageStatus.Accepted;
 }
Example #7
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));
		}
Example #8
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));
		}
Example #9
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);
        }
Example #10
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));
		}
Example #11
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);
 }
Example #12
0
    public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, 
      Bucket messageValue, 
      ISourceBlock<Bucket> source, 
      bool consumeToAccept)
    {
      var lines = messageValue.ToLines();
      _bufferBlock.Post(lines);

      return DataflowMessageStatus.Accepted;
    }
Example #13
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));
		}
        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");
        }
Example #15
0
 public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader, Bucket messageValue, ISourceBlock<Bucket> source, bool consumeToAccept)
 {
   messageValue.FeedTarget(_batchBlock);
   return DataflowMessageStatus.Accepted;
 }