Ejemplo n.º 1
0
        public async Task WriteWithETagViolation()
        {
            GrainState <EntityWithIntegerKeyWithEtag> grainState =
                Internal.Utils.CreateAndStoreGrainState <EntityWithIntegerKeyWithEtag>(_serviceProvider);

            TestGrainReference grainRef
                = TestGrainReference.Create(grainState.State);

            // update the database
            EntityWithIntegerKeyWithEtag clone = grainState.State.Clone();

            clone.Title = "Updated";
            using (var context = _serviceProvider.GetRequiredService <TestDbContext>())
            {
                context.Entry(clone).State = EntityState.Modified;

                context.SaveChanges();
            }

            // This should fail
            grainState.State.Title = "Failing Update";
            await Assert.ThrowsAsync <InconsistentStateException>(() =>
                                                                  _storage.WriteStateAsync(typeof(GrainWithIntegerKeyWithEtag).FullName,
                                                                                           grainRef,
                                                                                           grainState));
        }
        private void MockConcurrencyChecks()
        {
            // Check etags as the in-memory storage doesn't
            foreach (EntityWithIntegerKeyWithEtag entitiy in this.ETagEntities.Local)
            {
                var entry = this.Entry(entitiy);

                if (entry.State != EntityState.Modified)
                {
                    continue;
                }

                EntityWithIntegerKeyWithEtag storedEntity = this.ETagEntities.AsNoTracking()
                                                            .Single(e => e.Id == entitiy.Id);

                if (!storedEntity.ETag.SequenceEqual(entitiy.ETag))
                {
                    throw new DbUpdateConcurrencyException("ETag violation",
                                                           entry.GetInfrastructure().StateManager.Entries.Select(
                                                               e => (IUpdateEntry)e).ToList());
                }

                entitiy.ETag = BitConverter.GetBytes(Random.Next());
            }
        }