Esempio n. 1
0
        public void CompletesFromTopReached()
        {
            var  sub      = new PushSubject <int>();
            bool complete = false;

            sub.Take(1).Subscribe(_ => { }, () => complete = true);
            sub.PushValue(1);
            Assert.IsTrue(complete, "stream must be completed from the moment the top is reached");
        }
Esempio n. 2
0
        public void GetSimpleFirst()
        {
            var  valueStack = new Stack <int>();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;
            var  obs1       = new PushSubject <int>();

            var output = obs1.First();

            output.Subscribe(valueStack.Push, () => isComplete = true, errorStack.Push);

            obs1.PushValue(1);
            Assert.AreEqual(1, valueStack.Peek(), "value should be submitted");
            Assert.IsTrue(isComplete, "the stream should be completed");

            obs1.PushValue(2);
            Assert.AreEqual(1, valueStack.Count, "no value should be submitted");
        }
        public void SwitchFlatMap()
        {
            var  valueStack = new Stack <int>();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;
            var  obs        = new PushSubject <int>();
            var  sobs       = new[] { new PushSubject <int>(), new PushSubject <int>() };

            var output = obs.SwitchMap(i => sobs[i]);

            output.Subscribe(valueStack.Push, () => isComplete = true, errorStack.Push);

            obs.PushValue(0);
            Assert.AreEqual(0, valueStack.Count, "no value should be in the output stream");

            sobs[0].PushValue(1);
            Assert.AreEqual(1, valueStack.Peek(), "values from the input streams should be in the output stream");

            sobs[1].PushValue(2);
            Assert.AreEqual(1, valueStack.Count, "values from the input streams that are not used should not be in the output stream");

            var ex = new Exception();

            sobs[0].PushException(ex);
            Assert.IsTrue(Object.ReferenceEquals(ex, errorStack.Peek()), "errors from the input streams should be in the output stream");

            obs.PushValue(1);
            Assert.AreEqual(1, valueStack.Count, "no more values from the input streams should be in the output stream");

            sobs[0].PushValue(3);
            Assert.AreNotEqual(3, valueStack.Peek(), "values from the first input streams should not be in the output stream");

            sobs[1].PushValue(4);
            Assert.AreEqual(4, valueStack.Peek(), "values from the input streams should be in the output stream");

            sobs[0].Complete();
            Assert.IsFalse(isComplete, "the output stream should not be completed");

            sobs[1].Complete();
            Assert.IsFalse(isComplete, "the output stream should not be completed");

            obs.Complete();
            Assert.IsTrue(isComplete, "the output stream should be completed");
        }
Esempio n. 4
0
        public void SteppedTest4()
        {
            var left         = new PushSubject <int>();
            var ______right  = new PushSubject <int>();
            var outputValues = new List <int>();

            left.Substract(______right, i => i, i => i).Subscribe(outputValues.Add);

            left.PushValue(2);
            CollectionAssert.AreEquivalent(new int[] { }.ToList(), outputValues);
            ______right.PushValue(0);
            CollectionAssert.AreEquivalent(new int[] { }.ToList(), outputValues);
            left.PushValue(2);
            CollectionAssert.AreEquivalent(new int[] { }.ToList(), outputValues);
            ______right.PushValue(2);
            CollectionAssert.AreEquivalent(new int[] { }.ToList(), outputValues);
            left.PushValue(4);
            CollectionAssert.AreEquivalent(new int[] { }.ToList(), outputValues);
            left.PushValue(4);
            CollectionAssert.AreEquivalent(new int[] { }.ToList(), outputValues);
            ______right.PushValue(3);
            CollectionAssert.AreEquivalent(new int[] { }.ToList(), outputValues);
            left.PushValue(5);
            CollectionAssert.AreEquivalent(new int[] { }.ToList(), outputValues);
            ______right.PushValue(6);
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5 }.ToList(), outputValues);
            left.PushValue(5);
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5, 5 }.ToList(), outputValues);
            ______right.PushValue(6);
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5, 5 }.ToList(), outputValues);
            ______right.PushValue(7);
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5, 5 }.ToList(), outputValues);
            left.PushValue(8);
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5, 5 }.ToList(), outputValues);
            ______right.PushValue(7);
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5, 5 }.ToList(), outputValues);
            ______right.PushValue(8);
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5, 5 }.ToList(), outputValues);
            left.PushValue(9);
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5, 5 }.ToList(), outputValues);
            left.PushValue(9);
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5, 5 }.ToList(), outputValues);
            ______right.PushValue(9);
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5, 5 }.ToList(), outputValues);
            left.Complete();
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5, 5 }.ToList(), outputValues);
            ______right.Complete();
            CollectionAssert.AreEquivalent(new int[] { 4, 4, 5, 5 }.ToList(), outputValues);
        }
Esempio n. 5
0
        public void PushValueAfterComplete()
        {
            var values = new List <int>();
            var tmp    = new PushSubject <int>();

            tmp.Subscribe(values.Add);
            tmp.Complete();
            tmp.PushValue(1);
            Assert.AreEqual(0, values.Count, "pushed value should not be streamed");
        }
Esempio n. 6
0
        public void SimpleMerge()
        {
            var  valueStack = new Stack <int>();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;
            var  obs1       = new PushSubject <int>();
            var  obs2       = new PushSubject <int>();

            var output = obs1.Merge(obs2);

            output.Subscribe(valueStack.Push, () => isComplete = true, errorStack.Push);

            obs1.PushValue(1);
            Assert.AreEqual(1, valueStack.Peek(), "input from one stream should be found on the merged stream");

            obs2.PushValue(2);
            Assert.AreEqual(2, valueStack.Peek(), "input from one stream should be found on the merged stream");

            obs2.PushValue(3);
            Assert.AreEqual(3, valueStack.Peek(), "input from one stream should be found on the merged stream");

            obs1.PushValue(4);
            Assert.AreEqual(4, valueStack.Peek(), "input from one stream should be found on the merged stream");

            var ex1 = new Exception();

            obs1.PushException(ex1);
            Assert.IsTrue(Object.ReferenceEquals(ex1, errorStack.Peek()), "input exception from one stream should be found on the merged stream");

            var ex2 = new Exception();

            obs2.PushException(ex2);
            Assert.IsTrue(Object.ReferenceEquals(ex2, errorStack.Peek()), "input exception from one stream should be found on the merged stream");

            Assert.IsFalse(isComplete, "the stream should not be finished if not every input stream is complete");

            obs1.Complete();
            Assert.IsFalse(isComplete, "the stream should not be finished if not every input stream is complete");

            obs2.Complete();
            Assert.IsTrue(isComplete, "the stream should be finished if every input stream is complete");
        }
Esempio n. 7
0
        public void PushSimpleValue()
        {
            var  values    = new List <int>();
            bool completed = false;
            var  tmp       = new PushSubject <int>();

            tmp.Subscribe(values.Add, () => completed = true);
            tmp.PushValue(1);
            Assert.AreEqual(1, values[0], "pushed value doesn't match");
            Assert.IsFalse(completed, "shouldn't be completed");
        }
Esempio n. 8
0
        public void SimplePushValues()
        {
            var  valueStack = new Stack <Tuple <int, int> >();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;
            var  obs1       = new PushSubject <int>();
            var  obs2       = new PushSubject <int>();

            var output = obs1.CombineWithLatest <int, int, Tuple <int, int> >(obs2, (v1, v2) => new Tuple <int, int>(v1, v2));

            output.Subscribe(valueStack.Push, () => isComplete = true, errorStack.Push);

            obs1.PushValue(1);
            Assert.AreEqual(0, valueStack.Count, "no value should be in the output stream if not both value are sent into input streams");

            obs1.PushValue(2);
            Assert.AreEqual(0, valueStack.Count, "no value should be in the output stream if not both value are sent into input streams");

            obs2.PushValue(3);
            var outValue = valueStack.Peek();

            Assert.IsTrue(outValue.Item1 == 2 && outValue.Item2 == 3, "the ouput value should contains the 2 last submitted values in both streams");

            obs2.PushValue(4);
            outValue = valueStack.Peek();
            Assert.IsTrue(outValue.Item1 == 2 && outValue.Item2 == 4, "the ouput value should contains the 2 last submitted values in both streams");

            obs1.PushValue(5);
            outValue = valueStack.Peek();
            Assert.IsTrue(outValue.Item1 == 5 && outValue.Item2 == 4, "the ouput value should contains the 2 last submitted values in both streams");

            obs2.Complete();
            Assert.IsFalse(isComplete, "the output stream shouldn't complete as long as both input streams are not complete");

            obs1.PushValue(6);
            outValue = valueStack.Peek();
            Assert.IsTrue(outValue.Item1 == 6 && outValue.Item2 == 4, "the ouput value should contains the 2 last submitted values in both streams");

            obs1.Complete();
            Assert.IsTrue(isComplete, "the output stream should be completed");
        }
Esempio n. 9
0
        public void PushCompleted()
        {
            bool valueSubmitted = false;
            bool completed      = false;
            var  tmp            = new PushSubject <int>();

            tmp.Subscribe(i => valueSubmitted = true, () => completed = true);
            tmp.Complete();
            Assert.IsTrue(completed, "stream should be completed");
            tmp.PushValue(1);
            Assert.IsFalse(valueSubmitted, "no more value should be submitted to the output stream");
        }
Esempio n. 10
0
        public void ComplexDistinct1()
        {
            var  valueStack = new Stack <Tuple <int> >();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;

            var obs = new PushSubject <Tuple <int> >();

            var output = obs.Distinct(l => l.Item1);

            output.Subscribe(valueStack.Push, () => isComplete = true, errorStack.Push);

            obs.PushValue(new Tuple <int>(1));
            Assert.AreEqual(1, valueStack.Peek().Item1, "the input value should be issued");

            obs.PushValue(new Tuple <int>(2));
            Assert.AreEqual(2, valueStack.Peek().Item1, "the input value should be issued");

            obs.PushValue(new Tuple <int>(1));
            Assert.AreEqual(2, valueStack.Count, "the input value should not be issued as it has been submitted first");

            obs.PushValue(new Tuple <int>(3));
            Assert.AreEqual(3, valueStack.Peek().Item1, "the input value should be issued");

            obs.PushValue(new Tuple <int>(1));
            Assert.AreEqual(3, valueStack.Count, "the input value should not be issued as it has been submitted first");

            obs.PushValue(new Tuple <int>(2));
            Assert.AreEqual(3, valueStack.Count, "the input value should not be issued as it has been submitted first");

            obs.Complete();
            Assert.IsTrue(isComplete, "the stream should be completed");
        }
Esempio n. 11
0
        public void SteppedTest1()
        {
            var left         = new PushSubject <int>();
            var right        = new PushSubject <int>();
            var outputValues = new List <int>();

            left.Substract(right, i => i, i => i).Subscribe(outputValues.Add);

            left.PushValue(1);
            CollectionAssert.AreEquivalent(new int[] { }.ToList(), outputValues);
            right.PushValue(2);
            CollectionAssert.AreEquivalent(new int[] { 1 }.ToList(), outputValues);
        }
Esempio n. 12
0
        public void NotExactChunkAmountOfElements()
        {
            var  valueStack = new Stack <IEnumerable <int> >();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;
            var  obs1       = new PushSubject <int>();

            var output = obs1.Chunk(3);

            output.Subscribe(valueStack.Push, () => isComplete = true, errorStack.Push);

            obs1.PushValue(1);
            Assert.AreEqual(0, valueStack.Count(), "the output stream should be empty");
            obs1.PushValue(2);
            Assert.AreEqual(0, valueStack.Count(), "the output stream should be empty");
            obs1.PushValue(3);
            Assert.AreEqual(1, valueStack.Count(), "the output stream should return a chunk of values");
            var value = valueStack.Peek().ToList();

            Assert.AreEqual(3, value.Count, "the chunk of values should be the size of the chunk");

            Assert.AreEqual(1, value[0], "All values must match");
            Assert.AreEqual(2, value[1], "All values must match");
            Assert.AreEqual(3, value[2], "All values must match");

            obs1.PushValue(4);
            Assert.AreEqual(1, valueStack.Count(), "the output stream should not have more chunk of values");

            obs1.Complete();

            Assert.AreEqual(2, valueStack.Count(), "the output stream should have returned a second chunk");

            value = valueStack.Peek().ToList();
            Assert.AreEqual(1, value.Count, "the chunk of values should be the size of the remaining items in the stream");

            Assert.AreEqual(4, value[0], "All values must match");

            Assert.IsTrue(isComplete, "the stream should be finished if every input stream is complete");
        }
Esempio n. 13
0
        public void FlatMapWithDeferable()
        {
            var  valueStack = new Stack <int>();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;
            var  obs        = new PushSubject <int>();
            var  sobs       = new[] { PushObservable.FromEnumerable(new[] { 1, 2 }), PushObservable.FromEnumerable(new[] { 3, 4 }) };

            var output = obs.FlatMap(i => sobs[i]);

            output.Subscribe(valueStack.Push, () => isComplete = true, errorStack.Push);

            obs.PushValue(0);
            System.Threading.Thread.Sleep(50); //the time for the 2 values to be issued
            CollectionAssert.AreEquivalent(new[] { 1, 2 }, valueStack, "output values should be automatically issued");

            obs.PushValue(1);
            System.Threading.Thread.Sleep(50); //the time for the 2 values to be issued
            CollectionAssert.AreEquivalent(new[] { 1, 2, 3, 4 }, valueStack, "output values should be automatically issued");

            obs.Complete();
            Assert.IsTrue(isComplete, "the output stream should be completed");
        }
Esempio n. 14
0
        public void TriggerStreamCompletesAfterTriggering()
        {
            var  valueStack = new Stack <int>();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;
            var  obs1       = new PushSubject <int>();
            var  obs2       = new PushSubject <int>();

            var output = obs1.SkipUntil(obs2);

            output.Subscribe(valueStack.Push, () => isComplete = true, errorStack.Push);

            obs1.PushValue(1);
            Assert.AreEqual(0, valueStack.Count, "the output stream should not issue any value as long as it's not triggered");

            obs2.PushValue(2);

            obs2.Complete();
            Assert.IsFalse(isComplete, "the output stream should not be completed");

            obs1.PushValue(3);
            Assert.AreEqual(3, valueStack.Peek(), "the output stream should issue a value even if the trigger is completed");
        }
Esempio n. 15
0
        public void FilterWithErrors()
        {
            var outputValues = new Stack <int>();
            var errors       = new Stack <Exception>();
            var completed    = false;
            var obs          = new PushSubject <int>();

            var mapped = obs.Filter(i => 10 / i > 0);

            mapped.Subscribe(outputValues.Push, () => completed = true, errors.Push);

            obs.PushValue(-10);
            Assert.AreEqual(0, outputValues.Count, "no output should be issued");
            Assert.IsFalse(completed, "the stream should not be completed");

            obs.PushValue(0);
            Assert.AreEqual(0, outputValues.Count, "no output should be issued");
            Assert.IsFalse(completed, "the stream should not be completed");
            Assert.IsInstanceOfType(errors.Peek(), typeof(DivideByZeroException), "a division by zero should be received in the stream");

            obs.PushValue(10);
            Assert.AreEqual(1, outputValues.Count, "one output should be issued");
            Assert.AreEqual(10, outputValues.Peek(), "issued value should match output");
            Assert.IsFalse(completed, "the stream should not be completed");

            obs.PushValue(9);
            Assert.AreEqual(2, outputValues.Count, "two output should be issued");
            Assert.AreEqual(9, outputValues.Peek(), "issued value should match output");
            Assert.IsFalse(completed, "the stream should not be completed");

            obs.PushValue(11);
            Assert.AreEqual(2, outputValues.Count, "two output should be issued");
            Assert.IsFalse(completed, "the stream should not be completed");

            obs.Complete();
            Assert.IsTrue(completed, "the stream should be completed");
        }
Esempio n. 16
0
        public void TriggerAfterMainStreamComplete()
        {
            var  valueStack = new Stack <int>();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;
            var  obs1       = new PushSubject <int>();

            var output = obs1.SkipUntil(i => i == 0);

            output.Subscribe(valueStack.Push, () => isComplete = true, errorStack.Push);

            obs1.PushValue(1);
            Assert.AreEqual(0, valueStack.Count, "the output stream should not issue any value as long as it's not triggered");

            obs1.Complete();
            Assert.IsTrue(isComplete, "the output stream should be competed");

            obs1.PushValue(0);

            var ex = new Exception();

            obs1.PushException(ex);
            Assert.AreEqual(0, errorStack.Count, "as the stream is completed, no error should be issued");
        }
Esempio n. 17
0
        public void SplitObservables2()
        {
            var  lastValues  = new List <int>();
            bool isCompleted = false;
            // var lastValues=new List<KeyValuePair<int, int>>();
            IPushSubject <KeyValuePair <int, int> > src = new PushSubject <KeyValuePair <int, int> >();
            var resS = src.Group(i => i.Key, iS => iS.Scan((acc, val) => acc + val.Value, 0));

            resS.Subscribe(i => lastValues.Add(i), () => isCompleted = true);

            src.PushValue(new KeyValuePair <int, int>(1, 1));
            src.Complete();
            CollectionAssert.AreEquivalent(new[] { 1 }, lastValues);
            Assert.IsTrue(isCompleted);
        }
Esempio n. 18
0
        public void PushSimpleError()
        {
            var  errors    = new List <Exception>();
            var  values    = new List <int>();
            bool completed = false;
            var  tmp       = new PushSubject <int>();

            tmp.Subscribe(values.Add, () => completed = true, errors.Add);
            var exception = new Exception();

            tmp.PushException(exception);
            Assert.AreSame(exception, errors[0], "the exception should be retrieved");
            Assert.IsFalse(completed, "shouldn't be completed");
            tmp.PushValue(1);
            Assert.AreEqual(1, values[0], "pushed value should still go in the stream after exception");
        }
Esempio n. 19
0
        public void TriggerStreamCompletesBeforeMainStreamIsTriggered()
        {
            var  valueStack = new Stack <int>();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;
            var  obs1       = new PushSubject <int>();
            var  obs2       = new PushSubject <int>();

            var output = obs1.SkipUntil(obs2);

            output.Subscribe(valueStack.Push, () => isComplete = true, errorStack.Push);

            obs1.PushValue(1);
            Assert.AreEqual(0, valueStack.Count, "the output stream should not issue any value as long as it's not triggered");

            obs2.Complete();
            Assert.IsTrue(isComplete, "the output stream should be completed as it cannot be triggered anymore");
        }
Esempio n. 20
0
        public void PushMulipleValues()
        {
            int  nb           = 10;
            var  inputValues  = Enumerable.Range(0, nb).ToList();
            var  outputValues = new List <int>();
            bool completed    = false;
            var  tmp          = new PushSubject <int>();

            tmp.Subscribe(outputValues.Add, () => completed = true);
            foreach (var item in inputValues)
            {
                tmp.PushValue(item);
            }
            for (int i = 0; i < nb; i++)
            {
                Assert.AreEqual(inputValues[i], outputValues[i], "all values should be the same");
            }
            Assert.AreEqual(inputValues.Count, outputValues.Count, "nb items from the input source must be the same that in the output");
            Assert.IsFalse(completed, "shouldn't be completed");
        }
Esempio n. 21
0
        public void Synchronisation()
        {
            var outputValues = new Stack <int>();
            var errors       = new Stack <Exception>();
            var completed    = false;
            var obs          = new PushSubject <int>();

            var mapped = obs.Map(i => 10 / i);

            mapped.Subscribe(outputValues.Push, () => completed = true, errors.Push);

            for (int value = 1; value < 5; value++)
            {
                obs.PushValue(value);
                Assert.AreEqual(10 / value, outputValues.Peek(), "the output value should match the map");
                Assert.IsFalse(completed, "the stream should not be completed");
            }

            obs.Complete();
            Assert.IsTrue(completed, "the stream should be completed");
        }
Esempio n. 22
0
        public void RightSubmitsNextBeforeLeftGetsMatch()
        {
            var valueStack = new Stack <Tuple <int, int> >();
            var errorStack = new Stack <Exception>();
            //bool isComplete = false;
            var leftS  = new PushSubject <int>();
            var rightS = new PushSubject <int>();

            var output = leftS.LeftJoin(rightS, i => i, i => i, null, (l, r) => new Tuple <int, int>(l, r));

            output.Subscribe(valueStack.Push, () => { }, errorStack.Push);

            leftS.PushValue(1);

            Assert.AreEqual(0, valueStack.Count, "no value should be submitted to the ouput stream");

            rightS.PushValue(2);

            var outValue = valueStack.Peek();

            Assert.AreEqual(1, valueStack.Count, "a value should be submitted to the ouput stream");
            Assert.AreEqual(1, outValue.Item1, "the out value should match the unmatched input");
            Assert.AreEqual(0, outValue.Item2, "the unmatched output should have no linked references");
        }
Esempio n. 23
0
        public void MapValues()
        {
            var inputValues  = new[] { true, false, true, false, false };
            var outputValues = new List <bool>();
            var obs          = new PushSubject <bool>();
            var mapped       = obs.Map(i => !i);

            mapped.Subscribe(outputValues.Add);

            foreach (var item in inputValues)
            {
                obs.PushValue(item);
            }

            obs.Complete();

            Assert.IsTrue(mapped.ToTaskAsync().Wait(5000), "The mapping should complete");

            for (int i = 0; i < outputValues.Count; i++)
            {
                Assert.AreEqual(!inputValues[i], outputValues[i], "all values should match the result of the map definition");
            }
            Assert.AreEqual(inputValues.Length, outputValues.Count, $"nb items from the output must match the input one");
        }
Esempio n. 24
0
        public void ScanValues()
        {
            var  valueStack = new Stack <List <int> >();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;

            var obs = new PushSubject <int>();

            var output = obs.Scan((acc, val) => { acc = acc.ToList(); acc.Add(val); return(acc); }, new List <int>());

            output.Subscribe(valueStack.Push, () => isComplete = true, errorStack.Push);

            for (int size = 0; size < 4; size++)
            {
                obs.PushValue(size);
                Assert.IsTrue(AreListEquals(valueStack.Peek(), size), "the accumulation should be done");

                var ex = new Exception();
                obs.PushException(ex);
                Assert.IsTrue(Object.ReferenceEquals(ex, errorStack.Peek()), "input errors should go in the output stream");
            }
            obs.Complete();
            Assert.IsTrue(isComplete, "the stream must be completed");
        }
Esempio n. 25
0
        public void SplitObservables()
        {
            var  lastValues  = new List <int>();
            bool isCompleted = false;
            // var lastValues=new List<KeyValuePair<int, int>>();
            IPushSubject <KeyValuePair <int, int> > src = new PushSubject <KeyValuePair <int, int> >();
            var resS = src.Group(i => i.Key, iS => iS.Last().Map(i => i.Value));

            resS.Subscribe(i => lastValues.Add(i), () => isCompleted = true);

            src.PushValue(new KeyValuePair <int, int>(1, 1));
            src.PushValue(new KeyValuePair <int, int>(1, 2));
            src.PushValue(new KeyValuePair <int, int>(2, 3));
            src.PushValue(new KeyValuePair <int, int>(2, 4));
            src.PushValue(new KeyValuePair <int, int>(1, 5));
            src.PushValue(new KeyValuePair <int, int>(1, 6));
            src.Complete();
            CollectionAssert.AreEquivalent(new[] { 6, 4 }, lastValues);
            Assert.IsTrue(isCompleted);
        }
Esempio n. 26
0
        public void SteppedTest5()
        {
            var left         = new PushSubject <int>();
            var ______right  = new PushSubject <int>();
            var outputValues = new List <int>();

            left.Substract(______right, i => i, i => i).Subscribe(outputValues.Add);

            left.PushValue(1);
            ______right.PushValue(2);
            left.PushValue(2);
            ______right.PushValue(5);
            left.PushValue(2);
            left.PushValue(3);
            left.PushValue(4);
            ______right.PushValue(5);
            left.PushValue(4);
            ______right.PushValue(6);
            left.PushValue(5);
            ______right.Complete();
            left.PushValue(6);
            left.PushValue(7);
            left.PushValue(7);
            left.PushValue(8);
            left.Complete();
            CollectionAssert.AreEquivalent(new[] { 1, 3, 4, 4, 7, 7, 8 }, outputValues.ToArray());
        }
Esempio n. 27
0
        public void PushValues(TIn input, Action <TOut> push)
        {
            var       src       = new PushSubject <string>();
            Exception exception = null;

            if (_args.Mapping.HasColumnHeader)
            {
                var numberedSrc = src
                                  .Map((txt, idx) => new { txt, idx });
                var lineParserS = numberedSrc
                                  .Skip(_args.Mapping.FirstLinesToIgnore)
                                  .Take(1)
                                  .Map(i => _args.Mapping.GetSerializer(i.txt))
                                  .CompletesOnException(i => exception = i);
                numberedSrc
                .Skip(1 + _args.Mapping.FirstLinesToIgnore)
                .Filter(i => !string.IsNullOrWhiteSpace(i.txt))
                .CombineWithLatest(lineParserS, (line, parser) =>
                {
                    try
                    {
                        return(parser.Deserialize(line.txt));
                    }
                    catch (Exception ex)
                    {
                        throw new FlatFileLineDeserializeException(line.idx, ex);
                    }
                })
                .CompletesOnException(i => exception = i)
                .Map(i => _args.ResultSelector(input, i))
                .Do(push);
            }
            else
            {
                var serializer  = _args.Mapping.GetSerializer();
                var numberedSrc = src
                                  .Map((txt, idx) => new { txt, idx });
                numberedSrc
                .Skip(_args.Mapping.FirstLinesToIgnore)
                .Filter(i => !string.IsNullOrWhiteSpace(i.txt))
                .Map(i =>
                {
                    try
                    {
                        return(serializer.Deserialize(i.txt));
                    }
                    catch (Exception ex)
                    {
                        throw new FlatFileLineDeserializeException(i.idx, ex);
                    }
                })
                .Map(i => _args.ResultSelector(input, i))
                .Do(push)
                .CompletesOnException(i => exception = i);
            }

            using (var sr = new StreamReader(_args.DataStreamSelector(input)))
                while (!sr.EndOfStream)
                {
                    src.PushValue(sr.ReadLine());
                }

            if (exception != null)
            {
                throw exception;
            }
            //src.PushException(exception);
            src.Complete();
        }