public void CorrectlyShutsDown() { var fixture = new KeyedOperationQueue(); var op1 = new Subject<int>(); var op2 = new Subject<int>(); var op3 = new Subject<int>(); bool isCompleted = false; int op1Result = 0, op2Result = 0, op3Result = 0; fixture.EnqueueObservableOperation("foo", () => op1).Subscribe(x => op1Result = x); fixture.EnqueueObservableOperation("bar", () => op2).Subscribe(x => op2Result = x); // Shut down the queue, shouldn't be completed until op1 and op2 complete fixture.ShutdownQueue().Subscribe(_ => isCompleted = true); Assert.False(isCompleted); op1.OnNext(1); op1.OnCompleted(); Assert.False(isCompleted); Assert.Equal(1, op1Result); op2.OnNext(2); op2.OnCompleted(); Assert.True(isCompleted); Assert.Equal(2, op2Result); // We've already shut down, new ops should be ignored fixture.EnqueueObservableOperation("foo", () => op3).Subscribe(x => op3Result = x); op3.OnNext(3); op3.OnCompleted(); Assert.Equal(0, op3Result); }
public void KeyedOperationQueueCorrectlyShutsDown() { var fixture = new KeyedOperationQueue(); var op1 = new Subject <int>(); var op2 = new Subject <int>(); var op3 = new Subject <int>(); bool isCompleted = false; int op1Result = 0, op2Result = 0, op3Result = 0; fixture.EnqueueObservableOperation("foo", () => op1).Subscribe(x => op1Result = x); fixture.EnqueueObservableOperation("bar", () => op2).Subscribe(x => op2Result = x); // Shut down the queue, shouldn't be completed until op1 and op2 complete fixture.ShutdownQueue().Subscribe(_ => isCompleted = true); Assert.False(isCompleted); op1.OnNext(1); op1.OnCompleted(); Assert.False(isCompleted); Assert.Equal(1, op1Result); op2.OnNext(2); op2.OnCompleted(); Assert.True(isCompleted); Assert.Equal(2, op2Result); // We've already shut down, new ops should be ignored fixture.EnqueueObservableOperation("foo", () => op3).Subscribe(x => op3Result = x); op3.OnNext(3); op3.OnCompleted(); Assert.Equal(0, op3Result); }
/// <summary> /// Closes all connections managed by this pool. /// </summary> public IObservable <Unit> Reset(bool shouldReopen = true) { var shutdownQueue = Observable.Return(Unit.Default); if (opQueue != null) { shutdownQueue = opQueue.ShutdownQueue(); } return(shutdownQueue.Finally(() => { if (connections != null) { foreach (var v in connections.Where(x => x != null && x.Connection != null)) { v.OnApplicationSuspended(); } connections = null; } if (shouldReopen) { connections = Enumerable.Range(0, connectionCount) .Select(_ => new Entry(connInfo.Item1, connInfo.Item2)) .ToList(); opQueue = new KeyedOperationQueue(); } })); }