public void NoWorkAction() { var callback = new Action <GenericParameterHelper>(p => { }); var progress = new ProgressWithCompletion <GenericParameterHelper>(callback); Assert.True(progress.WaitAsync().IsCompleted); }
public void NoWorkFuncOfTask() { var callback = new Func <GenericParameterHelper, Task>(p => { return(TplExtensions.CompletedTask); }); var progress = new ProgressWithCompletion <GenericParameterHelper>(callback); Assert.True(progress.WaitAsync().IsCompleted); }
public async Task WaitAsync_CancellationToken() { var handlerMayComplete = new AsyncManualResetEvent(); var callback = new Func <GenericParameterHelper, Task>( async p => { Assert.Equal(1, p.Data); await handlerMayComplete; }); var progress = new ProgressWithCompletion <GenericParameterHelper>(callback); IProgress <GenericParameterHelper> reporter = progress; reporter.Report(new GenericParameterHelper(1)); var cts = new CancellationTokenSource(); var progressAwaitable = progress.WaitAsync(cts.Token); await Task.Delay(AsyncDelay); Assert.False(progressAwaitable.GetAwaiter().IsCompleted); cts.Cancel(); await Assert.ThrowsAnyAsync <OperationCanceledException>(() => progressAwaitable).WithCancellation(this.TimeoutToken); // clean up handlerMayComplete.Set(); }
/// <summary> /// Retrieves all messages waiting for pickup at our endpoint. /// </summary> /// <param name="longPoll">if set to <c>true</c> [long poll].</param> /// <param name="progress">A callback to invoke for each downloaded message as it arrives.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns> /// A task whose result is the complete list of received messages. /// </returns> public virtual async Task <ReadOnlyListOfMessage> ReceiveAsync(bool longPoll = false, IProgress <Message> progress = null, CancellationToken cancellationToken = default(CancellationToken)) { var messages = new List <Message>(); ReadOnlyListOfPayload payloads = null; var payloadProgress = new ProgressWithCompletion <Payload>( async payload => { var message = FromPayload(payload); if (message != null) { // Sterilize the message of its claimed endpoint's claimed identifiers, // so that only verifiable identifiers are passed onto our application. var verifiedIdentifiers = await this.Channel.GetVerifiableIdentifiersAsync(message.Author, cancellationToken); message.Author.AuthorizedIdentifiers = verifiedIdentifiers.ToArray(); lock (messages) { messages.Add(message); } if (progress != null) { progress.Report(message); } } }); payloads = await this.Channel.ReceiveAsync(longPoll, payloadProgress, cancellationToken); // Ensure that we've receives the asynchronous progress notifications for all the payloads // so we don't return a partial result. await payloadProgress.WaitAsync(); return(messages); }
private async Task <ImmutableArray <SymbolInformation> > GetVsSearchResultsAsync(TestWorkspace workspace, string query) { var solution = workspace.CurrentSolution; using var client = await RemoteHostClient.TryGetClientAsync(workspace, CancellationToken.None).ConfigureAwait(false); Assert.NotNull(client); var document = solution.Projects.First().Documents.First(); await UpdatePrimaryWorkspace(client, solution.WithDocumentFilePath(document.Id, Path.Combine(TempRoot.Root, document.FilePath))); var workspaceSymbolParams = new WorkspaceSymbolParams { Query = query, }; var symbolResultsBuilder = ArrayBuilder <SymbolInformation> .GetInstance(); var threadingContext = workspace.ExportProvider.GetExportedValue <IThreadingContext>(); var awaitableProgress = new ProgressWithCompletion <SymbolInformation[]>( symbols => symbolResultsBuilder.AddRange(symbols), threadingContext.JoinableTaskFactory); workspaceSymbolParams.PartialResultToken = awaitableProgress; var result = await client.RunRemoteAsync <JObject>( WellKnownServiceHubService.RemoteLanguageServer, Methods.InitializeName, solution : null, new object[] { new InitializeParams() }, callbackTarget : null, CancellationToken.None).ConfigureAwait(false); Assert.True(result["capabilities"]["workspaceSymbolProvider"].ToObject <bool>()); _ = await client.RunRemoteAsync <SymbolInformation[]>( WellKnownServiceHubService.RemoteLanguageServer, Methods.WorkspaceSymbolName, solution : null, new object[] { workspaceSymbolParams }, callbackTarget : null, CancellationToken.None).ConfigureAwait(false); await awaitableProgress.WaitAsync(CancellationToken.None); return(symbolResultsBuilder.ToImmutableAndFree()); }
private async Task <ImmutableArray <SymbolInformation> > GetVsSearchResultsAsync(TestWorkspace workspace, string query) { var solution = workspace.CurrentSolution; var client = (InProcRemoteHostClient)await InProcRemoteHostClient.CreateAsync(workspace.Services, runCacheCleanup : false); var document = solution.Projects.First().Documents.First(); await UpdatePrimaryWorkspace(client, solution.WithDocumentFilePath(document.Id, Path.Combine(TempRoot.Root, document.FilePath))); var workspaceSymbolParams = new WorkspaceSymbolParams { Query = query, }; var symbolResultsBuilder = ArrayBuilder <SymbolInformation> .GetInstance(); var threadingContext = workspace.ExportProvider.GetExportedValue <IThreadingContext>(); var awaitableProgress = new ProgressWithCompletion <SymbolInformation[]>(symbols => { symbolResultsBuilder.AddRange(symbols); }, threadingContext.JoinableTaskFactory); workspaceSymbolParams.PartialResultToken = awaitableProgress; using (var jsonRpc = JsonRpc.Attach(await client.RequestServiceAsync(WellKnownServiceHubService.LanguageServer))) { var result = await jsonRpc.InvokeWithCancellationAsync <JObject>( Methods.InitializeName, new object[] { new InitializeParams() }, CancellationToken.None); Assert.True(result["capabilities"]["workspaceSymbolProvider"].ToObject <bool>()); var symbolResult = await jsonRpc.InvokeWithCancellationAsync <SymbolInformation[]>( Methods.WorkspaceSymbolName, new object[] { workspaceSymbolParams }, CancellationToken.None); await awaitableProgress.WaitAsync(CancellationToken.None); } return(symbolResultsBuilder.ToImmutableAndFree()); }
public async Task WaitAsync() { var handlerMayComplete = new AsyncManualResetEvent(); var callback = new Func <GenericParameterHelper, Task>( async p => { Assert.Equal(1, p.Data); await handlerMayComplete; }); var progress = new ProgressWithCompletion <GenericParameterHelper>(callback); IProgress <GenericParameterHelper> reporter = progress; reporter.Report(new GenericParameterHelper(1)); var progressAwaitable = progress.WaitAsync(); Assert.False(progressAwaitable.GetAwaiter().IsCompleted); await Task.Delay(AsyncDelay); Assert.False(progressAwaitable.GetAwaiter().IsCompleted); handlerMayComplete.Set(); await progressAwaitable; }
/// <summary> /// Retrieves all messages waiting for pickup at our endpoint. /// </summary> /// <param name="longPoll">if set to <c>true</c> [long poll].</param> /// <param name="progress">A callback to invoke for each downloaded message as it arrives.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns> /// A task whose result is the complete list of received messages. /// </returns> public virtual async Task<ReadOnlyListOfMessage> ReceiveAsync(bool longPoll = false, IProgress<Message> progress = null, CancellationToken cancellationToken = default(CancellationToken)) { var messages = new List<Message>(); ReadOnlyListOfPayload payloads = null; var payloadProgress = new ProgressWithCompletion<Payload>( async payload => { var message = FromPayload(payload); if (message != null) { // Sterilize the message of its claimed endpoint's claimed identifiers, // so that only verifiable identifiers are passed onto our application. var verifiedIdentifiers = await this.Channel.GetVerifiableIdentifiersAsync(message.Author, cancellationToken); message.Author.AuthorizedIdentifiers = verifiedIdentifiers.ToArray(); lock (messages) { messages.Add(message); } if (progress != null) { progress.Report(message); } } }); payloads = await this.Channel.ReceiveAsync(longPoll, payloadProgress, cancellationToken); // Ensure that we've receives the asynchronous progress notifications for all the payloads // so we don't return a partial result. await payloadProgress.WaitAsync(); return messages; }