public void IterationSetup() { var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var numEventsProcessed = 0; _dispatcher = new Mock <ReverseCallDispatcher>(); _dispatcher .Setup(_ => _.Reject(It.IsAny <EventHandlerRegistrationResponse>(), It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); _dispatcher .Setup(_ => _.Accept(It.IsAny <EventHandlerRegistrationResponse>(), It.IsAny <CancellationToken>())) .Returns(tcs.Task); _dispatcher .Setup(_ => _.Call(It.IsAny <HandleEventRequest>(), It.IsAny <Dolittle.Runtime.Execution.ExecutionContext>(), It.IsAny <CancellationToken>())) .Returns <HandleEventRequest, Dolittle.Runtime.Execution.ExecutionContext, CancellationToken>((request, _, __) => { Interlocked.Add(ref numEventsProcessed, 1); var response = new EventHandlerResponse(); if (numEventsProcessed == NumberEventsToProcess) { tcs.SetResult(); } return(Task.FromResult(response)); }); var eventHandlers = new List <IEventHandler>(); eventHandlers.AddRange(Enumerable.Range(0, EventHandlers).Select(_ => _eventHandlerFactory.CreateFast( new EventHandlerRegistrationArguments(Runtime.CreateExecutionContextFor("d9fd643f-ce74-4ae5-b706-b76859fd8827"), Guid.NewGuid(), _eventTypes, Partitioned, ScopeId.Default), true, _dispatcher.Object, CancellationToken.None))); _eventHandlersToRun = eventHandlers; }
/// <inheritdoc/> public override async Task Connect( IAsyncStreamReader <EventHandlerClientToRuntimeMessage> runtimeStream, IServerStreamWriter <EventHandlerRuntimeToClientMessage> clientStream, ServerCallContext context) { _logger.ConnectingEventHandler(); using var cts = CancellationTokenSource.CreateLinkedTokenSource(_hostApplicationLifetime.ApplicationStopping, context.CancellationToken); try { var connectResult = await _reverseCallServices.Connect(runtimeStream, clientStream, context, _eventHandlersProtocol, cts.Token).ConfigureAwait(false); if (!connectResult.Success) { return; } using var dispatcher = connectResult.Result.dispatcher; var arguments = connectResult.Result.arguments; using var eventHandler = _configuration.Value.Fast ? _eventHandlerFactory.CreateFast(arguments, _configuration.Value.ImplicitFilter, dispatcher, context.CancellationToken) : _eventHandlerFactory.Create(arguments, dispatcher, context.CancellationToken); await _eventHandlers.RegisterAndStart( eventHandler, (failure, cancellation) => dispatcher.Reject(new EventHandlerRegistrationResponse { Failure = failure.ToProtobuf() }, cancellation), cts.Token).ConfigureAwait(false); } finally { cts.Cancel(); } }