public async Task MockActorProxy_should_should_persist_state_across_multiple_proxies() { var fabricRuntime = new MockFabricRuntime("Overlord"); var stateActions = new List <string>(); var mockActorStateProvider = new MockActorStateProvider(fabricRuntime, stateActions); fabricRuntime.SetupActor( (service, actorId) => new ActorDemo(service, actorId), createStateProvider: () => mockActorStateProvider ); await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { var actor = fabricRuntime.ActorProxyFactory.CreateActorProxy <IActorDemo>(new ActorId("testivus")); return(actor.SetCountAsync(5)); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); var count = await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { var sameActor = fabricRuntime.ActorProxyFactory.CreateActorProxy <IActorDemo>(new ActorId("testivus")); return(sameActor.GetCountAsync()); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); count.Should().Be(5); }
public async Task CreateActorsWithActorService() { _fabricRuntime = new MockFabricRuntime("Overlord"); _fabricRuntime.SetupActor <ActorDemo, ActorDemoActorService>( (service, actorId) => new ActorDemo(service, actorId), (context, actorTypeInformation, stateProvider, stateManagerFactory) => new ActorDemoActorService(context, actorTypeInformation, stateProvider: stateProvider)); await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { var actor = _fabricRuntime.ActorProxyFactory.CreateActorProxy <IActorDemo>(new ActorId("first")); return(actor.SetCountAsync(1)); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { var actor = _fabricRuntime.ActorProxyFactory.CreateActorProxy <IActorDemo>(new ActorId("second")); return(actor.SetCountAsync(2)); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { var actor = _fabricRuntime.ActorProxyFactory.CreateActorProxy <IActorDemo>(new ActorId("third")); return(actor.SetCountAsync(3)); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); _proxy = _fabricRuntime.ActorProxyFactory.CreateActorServiceProxy <IActorDemoActorService>( _fabricRuntime.ApplicationUriBuilder.Build("ActorDemoActorService").ToUri(), new ActorId("testivus")); }
public static async Task <T> ExecuteCommandAsync <T> (Func <CancellationToken, Task <T> > func, ICommand command, IActorStateManager stateManager, CancellationToken cancellationToken) where T : struct { T?returnValue = null; await ExecutionHelper.ExecuteWithRetriesAsync( async ct => { var conditionalValue = await stateManager.TryGetStateAsync <object>(GetStateKey(command), ct); if (conditionalValue.HasValue) { returnValue = (T)conditionalValue.Value; } }, maxRetries : 3, retryDelay : 4.Seconds(), userCancellationToken : CancellationToken.None); if (returnValue != null) { return(await Task.FromResult(returnValue.Value)); } returnValue = await func(cancellationToken); await StoreCommand(command, stateManager, returnValue); return(returnValue ?? default(T)); }
private static async Task StoreCommand(ICommand command, IActorStateManager stateManager, object returnValue) { await ExecutionHelper.ExecuteWithRetriesAsync( async ct => { await stateManager.AddStateAsync(GetStateKey(command), returnValue, ct); }, maxRetries : 3, retryDelay : 4.Seconds(), userCancellationToken : CancellationToken.None); }
public async Task Should_be_able_to_get_all_actor_states_from_ActorService() { var result = await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { return(_proxy.GetCountsAsync(CancellationToken.None)); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); result.Should().BeEquivalentTo(new[] { 1, 2, 3 }); }
public async Task Should_be_able_to_get_actor_state_from_ActorService() { var result = await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { return(_proxy.GetCountAsync(new ActorId("second"), CancellationToken.None)); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); result.Should().Be(2); }
public Task StoreDomainEventAsync(IDomainEvent domainEvent) { return(ExecutionHelper.ExecuteWithRetriesAsync(async ct => { var eventStream = await this.StateManager.GetOrAddStateAsync <TEventStream>(EventStreamStateKey, new TEventStream(), ct); eventStream.Append(domainEvent); await this.StateManager.SetStateAsync(EventStreamStateKey, eventStream, ct); return Task.FromResult(true); }, 3, TimeSpan.FromSeconds(1), CancellationToken.None)); }
public async Task Should_be_able_to_get_actor_state_from_Actor() { var result = await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { var actor = _fabricRuntime.ActorProxyFactory.CreateActorProxy <IActorDemo>(new ActorId("first")); return(actor.GetCountAsync()); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); result.Should().Be(1); }
private static async Task <bool> HasPreviousExecution(ICommand command, IActorStateManager stateManager) { var hasPreviousExecution = false; await ExecutionHelper.ExecuteWithRetriesAsync( async ct => { hasPreviousExecution = await stateManager.ContainsStateAsync(GetStateKey(command), ct); }, maxRetries : 3, retryDelay : 4.Seconds(), userCancellationToken : CancellationToken.None); return(hasPreviousExecution); }
protected Task <TAggregateRoot> GetAndSetDomainAsync() { if (DomainState != null) { return(Task.FromResult(DomainState)); } return(ExecutionHelper.ExecuteWithRetriesAsync(async ct => { var eventStream = await this.StateManager.GetOrAddStateAsync(EventStreamStateKey, new TEventStream(), ct); DomainState = new TAggregateRoot(); DomainState.Initialize(this, eventStream.DomainEvents, TimeProvider); return DomainState; }, 3, TimeSpan.FromSeconds(1), CancellationToken.None)); }
public async Task Should_be_able_to_remove_actor_from_ActorService() { await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { _proxy.RemoveAsync(new ActorId("first"), CancellationToken.None); return(Task.FromResult(true)); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); var result = await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { return(_proxy.GetCountsAsync(CancellationToken.None)); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); result.Should().BeEquivalentTo(new[] { 2, 3 }); }
public async Task <TAggregateRoot> GetAsync <TAggregateRoot>() where TAggregateRoot : class, IEventStored, new() { if (_trackedAggregate == null) { _trackedAggregate = await ExecutionHelper.ExecuteWithRetriesAsync(async ct => { var eventStream = await _stateManager.GetOrAddStateAsync("state", new TEventStream(), ct); var aggregate = new TAggregateRoot(); aggregate.Initialize(_eventController, eventStream.DomainEvents, UtcNowTimeProvider.Instance); return(aggregate); }, 3, TimeSpan.FromSeconds(1), CancellationToken.None); } return((TAggregateRoot)_trackedAggregate); }
public async Task MockActorProxy_should_SaveState_after_Actor_method() { var fabricRuntime = new MockFabricRuntime("Overlord"); var stateActions = new List <string>(); var mockActorStateProvider = new MockActorStateProvider(fabricRuntime, stateActions); fabricRuntime.SetupActor((service, actorId) => new ActorDemo(service, actorId), createStateProvider: () => mockActorStateProvider); // Only to get around the kinda stupid introduced 1/20 msec 'bug' await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { var actor = fabricRuntime.ActorProxyFactory.CreateActorProxy <IActorDemo>(new ActorId("testivus")); return(actor.SetCountAsync(5)); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); stateActions.Should().BeEquivalentTo(new[] { "ContainsStateAsync", "ActorActivatedAsync", "SaveStateAsync" }); }
public async Task MockActorProxy_should_should_be_able_to_create_proxy_for_specific_ActorService() { var fabricRuntime = new MockFabricRuntime("Overlord"); fabricRuntime.SetupActor <ActorDemo, ActorDemoActorService>( (service, actorId) => new ActorDemo(service, actorId), (context, actorTypeInformation, stateProvider, stateManagerFactory) => new ActorDemoActorService(context, actorTypeInformation)); IActorDemoActorService proxy = null; await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { proxy = fabricRuntime.ActorProxyFactory.CreateActorServiceProxy <IActorDemoActorService>( fabricRuntime.ApplicationUriBuilder.Build("ActorDemoActorService").ToUri(), new ActorId("testivus")); return(Task.FromResult(true)); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); proxy.Should().NotBeNull(); }
public async Task MockActorProxy_should_should_be_able_to_create_proxy_for_Actor_with_specific_ActorService() { var fabricRuntime = new MockFabricRuntime("Overlord"); fabricRuntime.SetupActor <ActorDemo, ActorDemoActorService>( (service, actorId) => new ActorDemo(service, actorId), (context, actorTypeInformation, stateProvider, stateManagerFactory) => new ActorDemoActorService(context, actorTypeInformation, stateProvider: stateProvider, stateManagerFactory: stateManagerFactory)); IActorDemo proxy = null; // Only to get around the kinda stupid introduced 1/20 msec 'bug' await ExecutionHelper.ExecuteWithRetriesAsync((ct) => { proxy = fabricRuntime.ActorProxyFactory.CreateActorProxy <IActorDemo>(new ActorId("testivus")); return(Task.FromResult(true)); }, 3, TimeSpan.FromMilliseconds(3), CancellationToken.None); proxy.Should().NotBeNull(); }
public async Task SaveChanges() { // TODO: Idempotency check, check for prior execution of this command id. If prior execution has happened, goto c. //Save event stream await ExecutionHelper.ExecuteWithRetriesAsync(async ct => { var eventStream = await _stateManager.GetOrAddStateAsync("state", new TEventStream(), ct); eventStream.Append(_trackedAggregate.GetChanges().ToArray()); await _stateManager.SetStateAsync("state", eventStream, ct); return(Task.FromResult(true)); }, 3, TimeSpan.FromSeconds(1), CancellationToken.None); // Save the event ids together with the command id for idempotency checking var commandId = ServiceRequestContext.Current?["CommandId"] ?? Guid.NewGuid().ToString(); var eventIds = _trackedAggregate.GetChanges().Select(e => e.EventId); await ExecutionHelper.ExecuteWithRetriesAsync(async ct => { var commandExecution = new CommandExecution() { RaisedEventIds = eventIds.ToArray() }; await _stateManager.AddOrUpdateStateAsync(commandId, commandExecution, (s, execution) => commandExecution, ct); }, 3, TimeSpan.FromSeconds(1), CancellationToken.None); // Force save changes. await ExecutionHelper.ExecuteWithRetriesAsync(async ct => { await _stateManager.SaveStateAsync(ct); }, 3, TimeSpan.FromSeconds(1), CancellationToken.None); _trackedAggregate.ClearChanges(); // TODO: (c) Load the commandId from state manager and raise all domain events with ids in that command id record (b). //foreach (var domainEvent in _trackedAggregate.GetChanges()) //{ // // TODO: Use logger here. // await _eventController.RaiseDomainEvent(domainEvent); //} }