public void SimpleErrors()
        {
            var  valueStack = new Stack <Exception>();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;
            var  obs1       = new PushSubject <int>();

            var output = obs1.ExceptionsToObservable();

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

            var ex = new Exception();

            obs1.PushException(ex);
            Assert.IsTrue(Object.ReferenceEquals(ex, valueStack.Peek()), "exception message should be in the output stream");
            Assert.AreEqual(0, errorStack.Count, "no exception should be in the error output stream");

            ex = new Exception();
            obs1.PushException(ex);
            Assert.IsTrue(Object.ReferenceEquals(ex, valueStack.Peek()), "exception message should be in the output stream");
            Assert.AreEqual(0, errorStack.Count, "no exception should be in the error output stream");

            obs1.Complete();
            Assert.AreEqual(2, valueStack.Count, "no more exception should be in the error output stream");
            Assert.IsTrue(isComplete, "the output stream should be complete");
        }
        public void SimplePushBeforeTriggerIncluded()
        {
            var  valueStack = new Stack <int>();
            var  errorStack = new Stack <Exception>();
            bool isComplete = false;
            var  obs1       = new PushSubject <int>();

            var output = obs1.TakeUntil(i => i == 0, true);

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

            obs1.PushValue(1);
            Assert.AreEqual(1, valueStack.Peek(), "the input value should be issued");

            var ex = new Exception();

            obs1.PushException(ex);
            Assert.IsTrue(Object.ReferenceEquals(ex, errorStack.Peek()), "the exception should be issued");

            obs1.PushValue(0);
            Assert.AreEqual(0, valueStack.Peek(), "the input value should be issued");

            obs1.PushValue(3);
            Assert.AreEqual(2, valueStack.Count, "the ouput stream should not issue any other value");

            ex = new Exception();
            obs1.PushException(ex);
            Assert.AreEqual(1, errorStack.Count, "the exception should not be issued");

            obs1.Complete();
            Assert.IsTrue(isComplete, "the output stream should be competed");
        }
Exemplo n.º 3
0
        public void SimplePushBeforeTriggerExclude()
        {
            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, false);

            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");

            var ex = new Exception();

            obs1.PushException(ex);
            Assert.AreEqual(0, errorStack.Count, "as the stream is not triggered, no error should be issued");

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

            obs1.PushValue(3);
            Assert.AreEqual(3, valueStack.Peek(), "the ouput stream should issue values as long as the trigger stream issues a value");

            ex = new Exception();
            obs1.PushException(ex);
            Assert.AreEqual(1, errorStack.Count, "as the stream is not triggered, no error should be issued");
            Assert.IsTrue(Object.ReferenceEquals(ex, errorStack.Peek()), "as the stream is triggered, the error should be issued");

            obs1.Complete();
            Assert.IsTrue(isComplete, "the output stream should be competed");
        }
Exemplo n.º 4
0
        public void ExactChunkAmountOfElements()
        {
            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");

            var ex = new Exception();

            obs1.PushException(ex);
            Assert.IsTrue(Object.ReferenceEquals(ex, errorStack.Peek()), "input errors should go in the output stream");

            obs1.Complete();

            Assert.AreEqual(1, valueStack.Count(), "the output stream should have returned one chunk");
            Assert.IsTrue(isComplete, "the stream should be finished if every input stream is complete");
        }
Exemplo n.º 5
0
        public void TriggerAfterMainStreamComplete()
        {
            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");

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

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

            var ex = new Exception();

            obs1.PushException(ex);
            Assert.AreEqual(0, errorStack.Count, "as the stream is completed, no error should be issued");
        }
Exemplo n.º 6
0
        public void PushErrorAfterComplete()
        {
            var errors = new List <Exception>();
            var tmp    = new PushSubject <int>();

            tmp.Subscribe(_ => { }, () => { }, errors.Add);
            tmp.Complete();
            tmp.PushException(new Exception());
            Assert.AreEqual(0, errors.Count, "pushed errors should not be streamed");
        }
Exemplo n.º 7
0
        public void PushTwoErrors()
        {
            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");

            exception = new Exception();
            tmp.PushException(exception);
            Assert.AreSame(exception, errors[1], "multiple exceptions can be sent in the stream");

            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");
        }
Exemplo n.º 8
0
        public void PushSimpleError()
        {
            var errors = new List <Exception>();
            var values = new List <int>();
            var tmp    = new PushSubject <int>();

            tmp.Subscribe(values.Add, () => { }, errors.Add);
            var task      = tmp.CompletesOnException(errors.Add).ToTaskAsync();
            var exception = new Exception();

            tmp.PushException(exception);
            Assert.AreSame(exception, errors[0], "the exception should be retrieved");
            Assert.IsTrue(task.Wait(5000), "the stream should be completed");
        }
Exemplo n.º 9
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");
        }
Exemplo n.º 10
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");
        }