public void ErrorInMap()
        {
            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 = -2; value < 0; value++)
            {
                obs.PushValue(value);
                Assert.AreEqual(10 / value, outputValues.Peek(), "the output value should match the map");
                Assert.AreEqual(0, errors.Count, "no error should be submitted");
            }
            obs.PushValue(0);
            Assert.AreEqual(2, outputValues.Count, "no value should be added if error");
            Assert.IsInstanceOfType(errors.Peek(), typeof(DivideByZeroException), "a division by zero should be received in the stream");

            obs.PushValue(1);
            Assert.AreEqual(10 / 1, outputValues.Peek(), "the output value should match the map");
            Assert.AreEqual(1, errors.Count, "no nore error should be submitted");

            obs.Complete();
            Assert.IsTrue(completed, "the stream should be completed");
        }
        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");
        }
        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");
        }
Exemple #4
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();
        }