Ejemplo n.º 1
0
        public void ListPushTriggeredWithStart()
        {
            var             outputValue = 0;
            EventWaitHandle waitHandle  = new EventWaitHandle(false, EventResetMode.ManualReset);
            var             observable  = PushObservable.FromEnumerable(new[] { 1 });
            var             task        = observable.ToTaskAsync();

            observable.Subscribe(i => outputValue = i);
            observable.Start();
            var res = task.Result;

            Assert.AreEqual(1, outputValue);
            Assert.AreEqual(1, res);
        }
Ejemplo n.º 2
0
        public void ListToListPushTriggeredWithEventWait()
        {
            var             outputValue = 0;
            EventWaitHandle waitHandle  = new EventWaitHandle(false, EventResetMode.ManualReset);
            var             observable  = PushObservable.FromEnumerable(new[] { 1 }, waitHandle);
            var             task        = observable.ToListAsync();

            observable.Subscribe(i => outputValue = i);
            waitHandle.Set();
            var res = task.Result;

            Assert.AreEqual(1, outputValue);
            CollectionAssert.AreEquivalent(new[] { 1 }, res);
        }
Ejemplo n.º 3
0
        public void SinglePushTriggeredWithEventWait()
        {
            var             outputValue = 0;
            EventWaitHandle waitHandle  = new EventWaitHandle(false, EventResetMode.ManualReset);
            var             observable  = PushObservable.FromSingle(1, waitHandle);
            var             task        = observable.ToTaskAsync();

            observable.Subscribe(i => outputValue = i);
            waitHandle.Set();
            var res = task.Result;

            Assert.AreEqual(1, outputValue);
            Assert.AreEqual(1, res);
        }
Ejemplo n.º 4
0
        public void UndistinctSortedValuesForDistinct()
        {
            var inputValues  = new[] { -2, -1, -1, 0, 1, 2 };
            var outputValues = new List <int>();
            var obs          = PushObservable.FromEnumerable(inputValues);// new PushSubject<int>();
            var filtered     = obs.ExceptionOnUnsorted(i => i, SortOrder.Ascending, true);
            var output       = filtered.ToListAsync();
            var errorList    = filtered.ExceptionsToObservable().ToListAsync();

            obs.Start();
            output.Wait();
            errorList.Wait();
            CollectionAssert.AreEquivalent(new[] { -2, -1, 0, 1, 2 }, output.Result, "the output should contains only sorted values");
            Assert.AreEqual(1, errorList.Result.Count, "one exception should be issued");
        }
Ejemplo n.º 5
0
        public void SortedValues()
        {
            var inputValues  = new[] { -2, -1, 0, 1, 2 };
            var outputValues = new List <int>();
            var obs          = PushObservable.FromEnumerable(inputValues);// new PushSubject<int>();
            var filtered     = obs.ExceptionOnUnsorted(i => i);
            var output       = filtered.ToListAsync();
            var errorList    = filtered.ExceptionsToObservable().ToListAsync();

            obs.Start();
            output.Wait();
            errorList.Wait();
            CollectionAssert.AreEquivalent(inputValues, output.Result, "the output should be the same than the input");
            Assert.AreEqual(0, errorList.Result.Count, "no exception should be issued in a sorted stream");
        }
Ejemplo n.º 6
0
        public void BigLoad()
        {
            int    nb       = 5000;
            string example  = "abcdefghijklmnopqrstuvwxyz";
            var    examples = Enumerable.Range(0, nb).Select(i => example).ToList();
            var    initObs  = PushObservable.FromSingle(examples);
            var    task     = initObs
                              .FlatMap(i => new DeferredPushObservable <string>(push => i.ForEach(push)))
                              .FlatMap(i => new DeferredPushObservable <char>(push => i.ToList().ForEach(push)))
                              .ToListAsync();

            initObs.Start();
            task.Wait(5000);
            Assert.AreEqual(example.Count() * nb, task.Result.Count);
            Assert.IsTrue(task.IsCompletedSuccessfully);
        }
Ejemplo n.º 7
0
        public void SimpleConcatenationWithEmptyBottomTopStartingFirst()
        {
            var obs1 = PushObservable.FromEnumerable(new[] { 1, 2 });
            var obs2 = PushObservable.FromEnumerable(new int[] { });

            var outputObs  = obs1.Concatenate(obs2);
            var outputTask = outputObs.ToListAsync();

            obs1.Start();
            obs2.Start();

            outputTask.Wait();
            var output = outputTask.Result;

            CollectionAssert.AreEquivalent(new[] { 1, 2 }, output);
        }
Ejemplo n.º 8
0
        protected override IStream <TOut> CreateOutputStream(EfCoreSaveArgs <TInEf, TIn, TOut> args)
        {
            var ret = args.SourceStream.Observable
                      .Chunk(args.BatchSize)
                      .Map(i => i.Select(j => (Input: j, Entity: args.GetEntity(j))).ToList())
                      .Do(i =>
            {
                var dbContext = args.KeyedConnection == null
                        ? this.ExecutionContext.DependencyResolver.Resolve <DbContext>()
                        : this.ExecutionContext.DependencyResolver.Resolve <DbContext>(args.KeyedConnection);
                this.ExecutionContext.InvokeInDedicatedThreadAsync(dbContext, () => ProcessBatch(i, dbContext, args.BulkLoadMode)).Wait();
            })
                      .FlatMap((i, ct) => PushObservable.FromEnumerable(i, ct))
                      .Map(i => args.GetOutput(i.Input, i.Entity));

            return(base.CreateUnsortedStream(ret));
        }
Ejemplo n.º 9
0
        public Stream(ITraceMapper traceMapper, IExecutionContext executionContext, string sourceNodeName, IPushObservable <T> observable)
        {
            this.TraceMapper      = traceMapper;
            this.SourceNodeName   = sourceNodeName;
            this.ExecutionContext = executionContext;

            this.Observable = observable.TakeUntil(executionContext.StopProcessEvent);

            if (traceMapper != null)
            {
                this.TraceObservable =
                    PushObservable.Merge <ITraceContent>(
                        this.Observable.ExceptionsToObservable().Map(e => new UnhandledExceptionStreamTraceContent(e)),
                        this.Observable.Count().Map(count => new CounterSummaryStreamTraceContent(count)),
                        this.Observable.Map((e, i) => new RowProcessStreamTraceContent(i + 1, e))
                        ).Map(i => traceMapper.MapToTrace(i, executionContext.NextTraceSequence()));
            }
        }
Ejemplo n.º 10
0
        public Stream(ITracer tracer, IExecutionContext executionContext, string sourceNodeName, IPushObservable <T> observable)
        {
            this.Tracer           = tracer;
            this.SourceNodeName   = sourceNodeName;
            this.ExecutionContext = executionContext;

            this.Observable = observable
                              .CompletesOnException(e => tracer.Trace(new UnhandledExceptionStreamTraceContent(e)))
                              .TakeUntil(executionContext.StopProcessEvents);

            if (tracer != null)
            {
                PushObservable.Merge <ITraceContent>(
                    this.Observable.Count().Map(count => new CounterSummaryStreamTraceContent(count)),
                    this.Observable.Map((e, i) => new RowProcessStreamTraceContent(i + 1, e))
                    ).Subscribe(tracer.Trace);
            }
        }
Ejemplo n.º 11
0
        public void SimpleConcatenationWithEmptyBottomBottomStartingFirst()
        {
            EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);

            var obs1 = PushObservable.FromEnumerable(new[] { 1, 2 }, waitHandle);
            var obs2 = PushObservable.FromEnumerable(new int[] { }, waitHandle);

            var outputObs  = obs1.Concatenate(obs2);
            var outputTask = outputObs.ToListAsync();

            waitHandle.Set();
            // obs2.Start();
            // obs1.Start();

            outputTask.Wait();
            var output = outputTask.Result;

            CollectionAssert.AreEquivalent(new[] { 1, 2 }, output);
        }
Ejemplo n.º 12
0
        public void RangeOfValuesNotAutomaticallyStartsOnHandle(int start, int nb)
        {
            var expectedValues = Enumerable.Range(start, nb).ToList();
            var outputValues   = new List <int>();
            var obs            = PushObservable.Range(-5, nb);

            obs.Subscribe(outputValues.Add);

            var task = obs.ToTaskAsync();

            obs.Start();
            Assert.IsTrue(task.Wait(5000), "the stream should complete");

            for (int i = 0; i < nb; i++)
            {
                Assert.AreEqual(start + i, outputValues[i], "all values should be the same");
            }
            Assert.AreEqual(nb, outputValues.Count, "nb items from the input source must be the same that in the output");
        }
Ejemplo n.º 13
0
        public void SeveralUnSortedElements()
        {
            var inputValues  = new int[] { 4, 1, 2, 5, 0, 3 };
            var outputValues = new List <int>();
            var obs          = PushObservable.FromEnumerable(inputValues);
            var task         = obs.Aggregate((i) => new List <int>(), i => i % 3, (a, i) => a.Union(new[] { i }).ToList(), (i, k, a) => new { Key = k, Value = a }).ToListAsync();

            obs.Start();
            task.Wait();
            Assert.AreEqual(3, task.Result.Count, "the output stream should have one element");

            CollectionAssert.AreEquivalent(new[] { 4, 1 }, task.Result[0].Value);
            Assert.AreEqual(1, task.Result[0].Key);

            CollectionAssert.AreEquivalent(new[] { 2, 5 }, task.Result[1].Value);
            Assert.AreEqual(2, task.Result[1].Key);

            CollectionAssert.AreEquivalent(new[] { 0, 3 }, task.Result[2].Value);
            Assert.AreEqual(0, task.Result[2].Key);
        }
Ejemplo n.º 14
0
        public void RangeOfValues(int start, int nb, int top)
        {
            var             outputValues = new List <int>();
            EventWaitHandle waitHandle   = new EventWaitHandle(false, EventResetMode.ManualReset);
            var             obs          = PushObservable.Range(start, nb, waitHandle).Take(top);

            obs.Subscribe(outputValues.Add);

            var task = obs.ToTaskAsync();

            waitHandle.Set();

            task.Wait(5000);

            for (int i = 0; i < top; i++)
            {
                Assert.AreEqual(start + i, outputValues[i], "all values should be the same");
            }
            Assert.AreEqual(top, outputValues.Count, $"nb items from the output must be {top}");
        }
Ejemplo n.º 15
0
        public void RangeOfValues(int start, int nb, int skip)
        {
            var             outputValues = new List <int>();
            EventWaitHandle waitHandle   = new EventWaitHandle(false, EventResetMode.ManualReset);

            var obs = PushObservable.Range(start, nb, waitHandle).Skip(skip);

            obs.Subscribe(outputValues.Add);

            var task = obs.ToTaskAsync();

            waitHandle.Set();
            task.Wait(5000);

            for (int i = 0; i < (nb - skip); i++)
            {
                Assert.AreEqual(start + i + skip, outputValues[i], "all values should be the same");
            }
            Assert.AreEqual(nb - skip, outputValues.Count, $"nb items from the output must be the same that in the input except the {skip}th ones");
        }
Ejemplo n.º 16
0
        public void DistinctSortedValuesForDistinct()
        {
            var inputValues  = new[] { -2, -1, 0, 1, 2 };
            var outputValues = new List <int>();
            var obs          = PushObservable.FromEnumerable(inputValues);// new PushSubject<int>();
            var filtered     = obs.ExceptionOnUnsorted(i => i, SortOrder.Ascending, true);
            var output       = filtered.ToListAsync();
            var errorList    = filtered.ExceptionsToObservable().ToListAsync();

            obs.Start();
            output.Wait();
            errorList.Wait();

            // Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
            // Trace.WriteLine(string.Join(',', output.Result.Select(i => i.ToString())));
            // Trace.WriteLine(errorList.Result.Count);


            Assert.AreEqual(0, errorList.Result.Count, "no exception should be issued in a sorted stream");
            Assert.AreEqual(inputValues.Length, output.Result.Count, "the output should be the same size than the input");
            CollectionAssert.AreEquivalent(inputValues, output.Result, "the output should be the same than the input");
        }
Ejemplo n.º 17
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");
        }
Ejemplo n.º 18
0
        protected override IStream <TOut> CreateOutputStream(ToSubProcessesArgs <TIn, TOut> args)
        {
            // Semaphore semaphore = args.NoParallelisation ? new Semaphore(1, 1) : new Semaphore(10, 10);
            Synchronizer synchronizer = new Synchronizer(args.NoParallelisation);

            if (this.ExecutionContext is GetDefinitionExecutionContext)
            {
                var inputStream = new SingleStream <TIn>(this.Tracer.GetSubTraceMapper(this), this.ExecutionContext, this.NodeName, PushObservable.FromSingle(default(TIn)));
                foreach (var subProcess in args.SubProcesses)
                {
                    var outputStream = subProcess(inputStream);
                    this.ExecutionContext.AddNode(this, outputStream.Observable, outputStream.TraceObservable);
                }
            }

            var outputObservable = args.Stream.Observable
                                   .FlatMap(i => PushObservable.FromEnumerable(args.SubProcesses.Select(sp => new
            {
                config  = i,
                subProc = sp
            })))
                                   .FlatMap(i =>
            {
                EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
                var inputStream            = new SingleStream <TIn>(this.Tracer.GetSubTraceMapper(this), this.ExecutionContext, this.NodeName, PushObservable.FromSingle(i.config, waitHandle));
                var outputStream           = i.subProc(inputStream);
                IDisposable awaiter        = null;
                outputStream.Observable.Subscribe(j => { }, () => awaiter?.Dispose());
                return(new DeferredWrapperPushObservable <TOut>(outputStream.Observable, () =>
                {
                    awaiter = synchronizer.WaitBeforeProcess();
                    waitHandle.Set();
                }));
            });

            return(base.CreateUnsortedStream(outputObservable));
        }
Ejemplo n.º 19
0
        protected override IStream <TOut> CreateOutputStream(SubProcessArgs <TIn, TOut> args)
        {
            if (this.ExecutionContext is GetDefinitionExecutionContext)
            {
                var inputStream  = new SingleStream <TIn>(new SubNodeWrapper(this), PushObservable.FromSingle(default(TIn), args.Stream.Observable.CancellationToken));
                var outputStream = args.SubProcess(inputStream);
                // this.ExecutionContext.AddNode(this, outputStream.Observable);
            }
            Synchronizer synchronizer     = new Synchronizer(args.NoParallelisation);
            var          outputObservable = args.Stream.Observable
                                            .FlatMap((i, ct) =>
            {
                // TODO: Solve bug here (this operator makes the process freezing))

                EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
                var inputStream            = new SingleStream <TIn>(new SubNodeWrapper(this), PushObservable.FromSingle(i, waitHandle, ct));
                var outputStream           = args.SubProcess(inputStream);
                // this.ExecutionContext.AddNode(this, outputStream.Observable);
                IDisposable awaiter = null;
                outputStream.Observable.Subscribe(j => { }, () => awaiter?.Dispose());
                return(new DeferredWrapperPushObservable <TOut>(outputStream.Observable, () =>
                {
                    awaiter = synchronizer.WaitBeforeProcess();
                    waitHandle.Set();
                }, null, ct));
            });

            return(base.CreateUnsortedStream(outputObservable));
        }
Ejemplo n.º 20
0
        protected override IStream <TOut> CreateOutputStream(SubProcessArgs <TIn, TOut> args)
        {
            if (this.ExecutionContext is GetDefinitionExecutionContext)
            {
                var inputStream  = new SingleStream <TIn>(this.Tracer.GetSubTraceMapper(this), this.ExecutionContext, this.NodeName, PushObservable.FromSingle(default(TIn)));
                var outputStream = args.SubProcess(inputStream);
                this.ExecutionContext.AddNode(this, outputStream.Observable, outputStream.TraceObservable);
            }
            Synchronizer synchronizer     = new Synchronizer(args.NoParallelisation);
            var          outputObservable = args.Stream.Observable
                                            .FlatMap(i =>
            {
                EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
                var inputStream            = new SingleStream <TIn>(this.Tracer.GetSubTraceMapper(this), this.ExecutionContext, this.NodeName, PushObservable.FromSingle(i, waitHandle));
                var outputStream           = args.SubProcess(inputStream);
                this.ExecutionContext.AddNode(this, outputStream.Observable, outputStream.TraceObservable);
                IDisposable awaiter = null;
                outputStream.Observable.Subscribe(j => { }, () => awaiter?.Dispose());
                return(new DeferredWrapperPushObservable <TOut>(outputStream.Observable, () =>
                {
                    awaiter = synchronizer.WaitBeforeProcess();
                    waitHandle.Set();
                }));
            });

            return(base.CreateUnsortedStream(outputObservable));
        }
Ejemplo n.º 21
0
 protected override IStream <TOut> CreateOutputStream(UnpivotArgs <TIn, TUnpivoted, TOut> args)
 {
     return(base.CreateUnsortedStream(args.InputStream.Observable.FlatMap(i => PushObservable.FromEnumerable(args.FieldsToUnpivot.Select(inp => args.ResultSelector(i, inp(i)))))));
 }
Ejemplo n.º 22
0
        protected override IStream <Correlated <TOut> > CreateOutputStream(GroupByCorrelatedArgs <TIn, TKey, TOut> args)
        {
            if (this.ExecutionContext is GetDefinitionExecutionContext)
            {
                var inputStream  = new SingleStream <Correlated <TIn> >(new SubNodeWrapper(this), PushObservable.FromSingle(default(Correlated <TIn>), null, args.Stream.Observable.CancellationToken));
                var outputStream = args.SubProcess(inputStream, default);
                this.ExecutionContext.AddNode(this, outputStream.Observable);
            }
            var outputObservable = args.Stream.Observable.Group(i => args.GetKey(i.Row), (iS, firstElement) => args.SubProcess(new Stream <Correlated <TIn> >(new SubNodeWrapper(this), iS), firstElement.Row).Observable);

            return(base.CreateUnsortedStream(outputObservable));
        }
Ejemplo n.º 23
0
        protected override IStream <TOut> CreateOutputStream(GroupArgs <TIn, TKey, TOut> args)
        {
            if (this.ExecutionContext is GetDefinitionExecutionContext)
            {
                var inputStream  = new SingleStream <TIn>(this.Tracer.GetSubTraceMapper(this), this.ExecutionContext, this.NodeName, PushObservable.FromSingle(default(TIn)));
                var outputStream = args.SubProcess(inputStream);
                this.ExecutionContext.AddNode(this, outputStream.Observable, outputStream.TraceObservable);
            }
            var outputObservable = args.Stream.Observable.Group(args.GetKey, iS => args.SubProcess(new Stream <TIn>(this.Tracer.GetSubTraceMapper(this), this.ExecutionContext, this.NodeName, iS)).Observable);

            return(base.CreateUnsortedStream(outputObservable));
        }