Ejemplo n.º 1
0
        public async Task <UnitType> Execute(IOrchestrationContext context)
        {
            Console.WriteLine($"Starting worker for [{Start},{Start+Count})");

            var pos = Start;

            while (pos < Start + Count)
            {
                var nextportionsize = Math.Min(PortionSize, (Start + Count) - pos);
                var results         = await context.PerformActivity(new SearchPortion()
                {
                    Target = Target,
                    Start  = pos,
                    Count  = nextportionsize,
                });

                foreach (var c in results)
                {
                    context.ForkEvent(new CollisionFoundEvent()
                    {
                        Collision = c
                    });
                }
                pos += nextportionsize;
            }

            await context.Finish();

            Console.WriteLine($"Worker finished [{Start},{Start + Count})");

            return(UnitType.Value);
        }
Ejemplo n.º 2
0
        public async Task <UnitType> Execute(IOrchestrationContext context)
        {
            for (int i = 0; i < 100000; i++)
            {
                context.ForkOrchestration(new CopyBlob()
                {
                    From = $"A{i}", To = $"B{i}"
                });
            }

            await context.Finish();

            return(UnitType.Value);
        }
Ejemplo n.º 3
0
        protected override async Task Run(IOrchestrationContext context)
        {
            await context.Finish();

            context.ForkUpdate(new IncrementThenRead()
            {
                CounterId = 0
            });

            await context.Finish();

            context.ForkUpdate(new IncrementThenRead()
            {
                CounterId = 1
            });
            context.ForkUpdate(new IncrementThenRead()
            {
                CounterId = 2
            });

            await context.Finish();

            for (uint i = 0; i < 100; i++)
            {
                context.ForkUpdate(new IncrementThenRead()
                {
                    CounterId = 100 + i
                });
            }

            var f1 = context.Finish();

            for (uint i = 0; i < 100; i++)
            {
                context.ForkUpdate(new IncrementThenRead()
                {
                    CounterId = 200 + i
                });
            }

            var f2 = context.Finish();

            for (uint i = 0; i < 100; i++)
            {
                context.ForkUpdate(new IncrementThenRead()
                {
                    CounterId = 300 + i
                });
            }

            await Task.WhenAll(f1, f2);

            await context.Finish();
        }
Ejemplo n.º 4
0
        protected override async Task Run(IOrchestrationContext context)
        {
            // ------ global counter

            context.ForkEvent(new SomeEvent());
            context.ForkEvent(new SomeEvent());
            context.ForkEvent(new SomeEvent());
            context.ForkEvent(new SomeEvent());
            context.ForkEvent(new SomeEvent());
            await context.Finish();

            var count = await context.PerformRead(new ReadGlobalCounter());

            Assert.Equal(5, count);

            // ------- account

            // get a fresh guid
            var guid = await context.NewGuid();

            // reads and withdraws must fail
            try
            {
                await context.PerformRead(new ReadBalance()
                {
                    AccountId = guid
                });

                Assert.Fail("KeyNotFoundException expected");
            }
            catch (KeyNotFoundException) { }
            try
            {
                await context.PerformUpdate(new TryWithdraw()
                {
                    AccountId = guid, Amount = 2
                });

                Assert.Fail("KeyNotFoundException expected");
            }
            catch (KeyNotFoundException) { }

            // existence checks must fail because we don't hold the lock
            try
            {
                await context.StateExists <AccountState, IAccountAffinity, Guid>(guid);

                Assert.Fail("SynchronizationDisciplineException expected");
            }
            catch (SynchronizationDisciplineException) { }

            // run a locked creation
            await context.PerformOrchestration(new CreateAccount()
            {
                AccountId = guid
            });

            // existence checks must fail because we don't hold the lock
            try
            {
                await context.StateExists <AccountState, IAccountAffinity, Guid>(guid);

                Assert.Fail("SynchronizationDisciplineException expected");
            }
            catch (SynchronizationDisciplineException) { }


            // reads should succeed and return the correct balance
            var amount = await context.PerformRead(new ReadBalance()
            {
                AccountId = guid
            });

            Assert.Equal(11, amount);

            // deposit should succeed and create the account
            var tasks = new List <Task <bool> >
            {
                context.PerformUpdate(new TryWithdraw()
                {
                    AccountId = guid, Amount = 2
                }),
                context.PerformUpdate(new TryWithdraw()
                {
                    AccountId = guid, Amount = 2
                }),
                context.PerformUpdate(new TryWithdraw()
                {
                    AccountId = guid, Amount = 2
                }),
                context.PerformUpdate(new TryWithdraw()
                {
                    AccountId = guid, Amount = 2
                }),
                context.PerformUpdate(new TryWithdraw()
                {
                    AccountId = guid, Amount = 2
                }),
                context.PerformUpdate(new TryWithdraw()
                {
                    AccountId = guid, Amount = 2
                }),
                context.PerformUpdate(new TryWithdraw()
                {
                    AccountId = guid, Amount = 2
                }),
                context.PerformUpdate(new TryWithdraw()
                {
                    AccountId = guid, Amount = 2
                }),
                context.PerformUpdate(new TryWithdraw()
                {
                    AccountId = guid, Amount = 2
                }),
                context.PerformUpdate(new TryWithdraw()
                {
                    AccountId = guid, Amount = 2
                })
            };

            await Task.WhenAll(tasks);

            Assert.Equal(5, tasks.Count(t => t.Result));

            var bal = await context.PerformRead(new ReadBalance()
            {
                AccountId = guid
            });

            Assert.Equal(1, bal);
        }