0

I'm trying to understand the changing behavior when I uncomment the 2nd SubscribeOn.

In this case, I hit the line throwing exception.

Can someone provide me with an answer - if it is intended behavior and why?

Without 2nd SubscribeOn it takes 30+ seconds to finish (and that's good). With - it hits the line with an exception after 2 seconds of run.

    [TestMethod]
    public void AsyncObservable_NonReentrant()
    {
        int tester = 0;
        bool finished = false;
        Observable.Generate(
                0,
                i => i < 5,
                i => i+1,
                i => i,
                _ => TimeSpan.FromSeconds(1)
            )
            .SubscribeOn(ThreadPoolScheduler.Instance)
            .Finally(()=>finished = true)
            .Select(_ =>
            {
                Interlocked.Increment(ref tester);
                if (tester > 1)
                    throw new InvalidOperationException();
                return Observable
                       .Repeat(1, 5)
                       .Window(3)
                       .Finally(()=>Interlocked.Decrement(ref tester))
                       /*.SubscribeOn(ThreadPoolScheduler.Instance)*/;
            })
            .Concat()
            .Subscribe(__ =>
            {
                Thread.Sleep(3000);
            });
        while (!finished)
            Thread.Sleep(100);
    }
Oleg Dok
  • 21,109
  • 4
  • 45
  • 54
  • 1
    This is a thread-jumping nightmare code dump. What specific behavior are you trying to understand? What behavior do you believe violates the Reactive Contract? What are you expecting? – Shlomo Sep 05 '19 at 14:39
  • I'm trying to understand - why using second SubscribeOn (effectively changing the scheduler only - ie - no changes in logic) - changes the execution path – Oleg Dok Sep 06 '19 at 20:38
  • 1
    Your code is using threading to control the execution path. Scheduling changes the thread scheduling, affecting the execution path. – Shlomo Sep 10 '19 at 01:43
  • But that's what the Reactive extensions was born for - to abstract async operations out of workflow... – Oleg Dok Sep 11 '19 at 06:55
  • It can be used that way; you’re not doing that. Threading here isn’t abstracted at all, it’s all over the code. – Shlomo Sep 11 '19 at 11:11
  • 1
    @OlegDok - As a general rule, if you are subscribing inside another observable then you are doing something wrong. – Enigmativity Sep 25 '19 at 11:18
  • But I'm not, just using SubscribeOn! Is it counts also? – Oleg Dok Sep 26 '19 at 16:32

0 Answers0