Пример #1
0
        public void TestAccumulatorByTopicTimeElapsed()
        {
            using (var accumulator = new AccumulatorByTopic <Tuple <string, int> >(t => t.Item1, 5,
                                                                                   TimeSpan.FromMilliseconds(15)))
            {
                var latch = new ManualResetEventSlim();
                IBatchByTopic <Tuple <string, int> > batch = null;
                accumulator.NewBatch += b =>
                {
                    latch.Set();
                    batch = b;
                };
                Assert.IsTrue(accumulator.Add(Tuple.Create("a", 1)));
                Assert.IsTrue(accumulator.Add(Tuple.Create("b", 2)));
                Assert.IsTrue(accumulator.Add(Tuple.Create("c", 3)));

                latch.Wait();

                Assert.That(batch.Count, Is.EqualTo(3));
                Assert.That(batch.Count(g => g.Key == "a"), Is.EqualTo(1));
                Assert.That(batch.Count(g => g.Key == "b"), Is.EqualTo(1));
                Assert.That(batch.Count(g => g.Key == "c"), Is.EqualTo(1));
                CollectionAssert.AreEquivalent(new[] { 1 }, batch.First(g => g.Key == "a").Select(t => t.Item2));
                CollectionAssert.AreEquivalent(new[] { 2 }, batch.First(g => g.Key == "b").Select(t => t.Item2));
                CollectionAssert.AreEquivalent(new[] { 3 }, batch.First(g => g.Key == "c").Select(t => t.Item2));
                batch.Dispose();
            }
        }
Пример #2
0
        public void TestAccumulatorByTopicCountReached()
        {
            using (var accumulator = new AccumulatorByTopic <Tuple <string, int> >(t => t.Item1, 5,
                                                                                   TimeSpan.FromMilliseconds(1000000)))
            {
                IBatchByTopic <Tuple <string, int> > batch = null;
                accumulator.NewBatch += b => batch = b;
                accumulator.Add(Tuple.Create("a", 1));
                accumulator.Add(Tuple.Create("a", 2));
                accumulator.Add(Tuple.Create("b", 8));
                accumulator.Add(Tuple.Create("a", 3));
                accumulator.Add(Tuple.Create("c", 1));
                accumulator.Add(Tuple.Create("a", 1));

                Assert.That(batch.Count, Is.EqualTo(5));
                Assert.That(batch.Count(g => g.Key == "a"), Is.EqualTo(1));
                Assert.That(batch.Count(g => g.Key == "b"), Is.EqualTo(1));
                Assert.That(batch.Count(g => g.Key == "c"), Is.EqualTo(1));
                CollectionAssert.AreEquivalent(new[] { 1, 2, 3 }, batch.First(g => g.Key == "a").Select(t => t.Item2));
                CollectionAssert.AreEquivalent(new[] { 8 }, batch.First(g => g.Key == "b").Select(t => t.Item2));
                CollectionAssert.AreEquivalent(new[] { 1 }, batch.First(g => g.Key == "c").Select(t => t.Item2));

                accumulator.Add(Tuple.Create("a", 1));
                accumulator.Add(Tuple.Create("a", 1));
                accumulator.Add(Tuple.Create("a", 1));
                accumulator.Add(Tuple.Create("a", 1));
                Assert.That(batch.Count, Is.EqualTo(5));
                Assert.That(batch.Count(g => g.Key == "a"), Is.EqualTo(1));
                Assert.That(batch.Count(), Is.EqualTo(1));
                Assert.That(batch.First().Count(), Is.EqualTo(5));
                Assert.That(batch.First().Count(t => t.Item2 == 1), Is.EqualTo(5));
                batch.Dispose();
            }
        }