public async Task when_subscribing_to_noninitialized_shell_then_can_wait_event_and_completion() { object zombie = true; uint cookie = 1; IVsShellPropertyEvents callback = null; var shell = new Mock <IVsShell>(); shell.Setup(x => x.GetProperty(ZombieProperty, out zombie)).Returns(VSConstants.S_OK); var capture = new CaptureMatch <IVsShellPropertyEvents>(s => callback = s); shell.Setup(x => x.AdviseShellPropertyChanges(Capture.With(capture), out cookie)) .Returns(VSConstants.S_OK); #pragma warning disable VSSDK005 // Avoid instantiating JoinableTaskContext var observable = new ShellInitializedObservable(new JoinableLazy <IVsShell>(() => shell.Object, taskFactory: new JoinableTaskContext().Factory)); #pragma warning restore VSSDK005 // Avoid instantiating JoinableTaskContext // Callback should have been provided at this point. Assert.NotNull(callback); var completed = false; ShellInitialized data = null; using (observable.Subscribe(e => data = e, () => completed = true)) { Assert.False(completed, "Observable shouldn't have completed yet."); Assert.Null(data); callback.OnShellPropertyChange(ZombieProperty, false); SpinWait.SpinUntil(() => completed, 5000); Assert.True(completed, "Observable should have completed already."); Assert.NotNull(data); shell.Verify(x => x.UnadviseShellPropertyChanges(cookie)); // Subsequent subscription should get one and complete right away. ShellInitialized ev = default; observable.Subscribe(e => ev = e); Assert.Same(data, ev); } }
public async Task when_subscribing_to_noninitialized_shell_then_can_wait_event_and_completion() { object zombie = true; uint cookie = 1; IVsShellPropertyEvents callback = null; var shell = new Mock <IVsShell>(); shell.Setup(x => x.GetProperty(ZombieProperty, out zombie)).Returns(VSConstants.S_OK); var capture = new CaptureMatch <IVsShellPropertyEvents>(s => callback = s); shell.Setup(x => x.AdviseShellPropertyChanges(Capture.With(capture), out cookie)) .Returns(VSConstants.S_OK); var observable = new ShellInitializedObservable(shell.Object); // Callback should have been provided at this point. Assert.NotNull(callback); var completed = false; ShellInitialized data = null; observable.Subscribe(e => data = e, () => completed = true); Assert.False(completed, "Observable shouldn't have completed yet."); Assert.Null(data); callback.OnShellPropertyChange(ZombieProperty, false); SpinWait.SpinUntil(() => completed, 5000); Assert.True(completed, "Observable should have completed already."); Assert.NotNull(data); shell.Verify(x => x.UnadviseShellPropertyChanges(cookie)); // Subsequent subscription should get one and complete right away. var ev = await observable; Assert.Same(data, ev); }