public UserClubAssignmentService( AggregateManagerRef aggregateManagerRef, ActorSystem actorSystem) { AggregateManager = aggregateManagerRef.Ref; ActorSystem = actorSystem; Queue = Source.Queue <AssignUserToClubFlow>(5, OverflowStrategy.Backpressure); AssignmentFlow = Flow.Create <AssignUserToClubFlow>() .Ask <GetUserState>() .SelectAsync(4, async x => { var getUserState = new GetUserState(x.UserId); var userState = await x.AggregateManager.Ask <UserState>(getUserState, TimeSpan.FromMilliseconds(500)); x.UserState = userState; return(x); }) .SelectAsync(4, async x => { var getBankAccountState = new GetBankAccountState(x.UserId); var bankAccountState = await x.AggregateManager.Ask <BankAccountState>(getBankAccountState, TimeSpan.FromMilliseconds(500)); x.BankAccountState = bankAccountState; return(x); }) .Select(x => { var networth = x.BankAccountState.Savings - x.BankAccountState.Credit; AddMember addMemberCommand = null; if (networth < 4) { addMemberCommand = new AddGoldMember(x.UserId); } else if (networth < 6) { addMemberCommand = new AddPlatinumMember(x.UserId); } else { addMemberCommand = new AddBlackMember(x.UserId); } AggregateManager.Tell(addMemberCommand); return(new AssignmentSummary { UserId = x.UserId, Name = x.UserState.Name, Email = x.UserState.Email, NetWorth = networth }); }); }
public async Task When_flow_is_opened_user_is_assigned_to_a_club() { var aggregateManager = Sys.ActorOf(Props.Create(() => new AggregateManager())); var aggregateManagerRef = new AggregateManagerRef(aggregateManager); var userClubAssignmentService = new UserClubAssignmentService(aggregateManagerRef, Sys); var flowUnderTest = userClubAssignmentService.AssignmentFlow; var queue = userClubAssignmentService.Queue; var materializer = Sys.Materializer(); var probe = this.SinkProbe <AssignmentSummary>(); var id = new Guid("1c041a27-0c61-414e-a002-30706b3411fd"); var name = $"testUser"; var email = $"*****@*****.**"; var registerUser = new RegisterUser(id, name, email, "SE", "1234567890"); var savings = 20; var credit = 14; var openAccount = new OpenBankAccount(id, savings, credit); aggregateManager.Tell(registerUser, ActorRefs.NoSender); aggregateManager.Tell(openAccount, ActorRefs.NoSender); var assignmentCommand = new AssignUserToClubFlow { UserId = id, AggregateManager = aggregateManager }; using (materializer) { var flow = queue .Via(flowUnderTest) .ToMaterialized(probe, Keep.Both) .Run(materializer); var q = flow.Item1; var sink = flow.Item2; var result = await q.OfferAsync(assignmentCommand); sink.Request(1); //sink.RequestNext(); sink.ExpectNext <AssignmentSummary>(x => x.Email == email && x.NetWorth == savings - credit); } }