示例#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));
        }
        public async Task UpdateCustomGetterGrainState()
        {
            var entity = new EntityWithGuidKey();

            Internal.Utils.StoreGrainState(_serviceProvider, entity);
            entity.Title += "UPDATED";
            var state = new GrainStateWrapper <EntityWithGuidKey>()
            {
                Value = entity
            };
            var grainState = new GrainState <GrainStateWrapper <EntityWithGuidKey> >()
            {
                State = state
            };

            TestGrainReference grainRef
                = TestGrainReference.Create(entity);

            await _storage.WriteStateAsync(typeof(GrainWithCustomStateGuidKey).FullName,
                                           grainRef,
                                           grainState
                                           );

            Internal.Utils.AssertEntityEqualityVsDb(
                _serviceProvider, grainState.State?.Value);
        }
示例#3
0
        public async Task ReadCustomGetterGrainStateNoPreCompile()
        {
            var entity = new EntityWithGuidKey();

            Internal.Utils.StoreGrainState(_serviceProvider, entity);

            var state = new GrainStateWrapper <EntityWithGuidKey>()
            {
                Value = entity
            };

            var grainState = new TestGrainState <GrainStateWrapper <EntityWithGuidKey> >()
            {
                State = state
            };

            TestGrainReference grainRef
                = TestGrainReference.Create(entity);

            grainState.State = null;

            await _storage.ReadStateAsync(typeof(GrainWithCustomStateGuidKeyNoPreCompile).FullName,
                                          grainRef,
                                          grainState
                                          );

            Internal.Utils.AssertEntityEqualityVsDb(
                _serviceProvider,
                grainState.State?.Value);
        }
示例#4
0
        public async Task ReadInvalidConfiguredCustomKeyStateShouldFail()
        {
            GrainState <InvalidConfiguredEntityWithCustomGuidKey> grainState =
                Internal.Utils.CreateAndStoreGrainState <InvalidConfiguredEntityWithCustomGuidKey>(_serviceProvider);

            TestGrainReference grainRef
                = TestGrainReference.Create <InvalidConfiguredGrainWithGuidKey>(0);

            await Assert.ThrowsAsync <GrainStorageConfigurationException>(() => _storage.ReadStateAsync(
                                                                              typeof(InvalidConfiguredGrainWithGuidKey).FullName,
                                                                              grainRef,
                                                                              grainState));
        }
示例#5
0
        public async Task ReadTaggedEntityShouldSuccessForNullState()
        {
            GrainState <EntityWithIntegerKeyWithEtag> grainState =
                new GrainState <EntityWithIntegerKeyWithEtag>();

            TestGrainReference grainRef
                = TestGrainReference.Create <GrainWithIntegerKeyWithEtag>(0);

            await _storage.ReadStateAsync(typeof(GrainWithIntegerKeyWithEtag).FullName,
                                          grainRef,
                                          grainState);

            Assert.Null(grainState.ETag);
        }
示例#6
0
        public async Task ReadConfiguredCustomKeyStateShouldPassForGrainsWithSameStateType()
        {
            GrainState <ConfiguredEntityWithCustomGuidKey> grainState =
                Internal.Utils.CreateAndStoreGrainState <ConfiguredEntityWithCustomGuidKey>(_serviceProvider);


            TestGrainReference grainRef
                = TestGrainReference.Create <ConfiguredGrainWithCustomGuidKey2>(
                      grainState.State.CustomKey, grainState.State.CustomKeyExt);


            await _storage.ReadStateAsync(typeof(ConfiguredGrainWithCustomGuidKey2).FullName,
                                          grainRef,
                                          grainState);
        }
示例#7
0
        private async Task TestWriteAsync <TGrain, TState, TKey>()
            where TState : Entity <TKey>, new()
            where TGrain : Grain <TState>
        {
            GrainState <TState> grainState = CreateGrainState <TState>();

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

            await _storage.WriteStateAsync(typeof(TGrain).FullName,
                                           grainRef,
                                           grainState
                                           );

            Internal.Utils.AssertEntityEqualityVsDb(_serviceProvider, grainState.State);
        }
示例#8
0
        public async Task StateShoudContainETag()
        {
            GrainState <EntityWithIntegerKeyWithEtag> grainState =
                Internal.Utils.CreateAndStoreGrainState <EntityWithIntegerKeyWithEtag>(_serviceProvider);

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

            await _storage.ReadStateAsync(typeof(GrainWithIntegerKeyWithEtag).FullName,
                                          grainRef,
                                          grainState);

            string expected = BitConverter.ToString(grainState.State.ETag)
                              .Replace("-", string.Empty);

            Assert.Equal(expected, grainState.ETag);
        }
示例#9
0
        public async Task ReadTaggedEntityShouldSuccessForNullEtag()
        {
            GrainState <EntityWithIntegerKeyWithEtag> grainState =
                Internal.Utils.StoreGrainState <EntityWithIntegerKeyWithEtag>(_serviceProvider,
                                                                              new EntityWithIntegerKeyWithEtag
            {
                ETag = null
            });

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

            await _storage.ReadStateAsync(typeof(GrainWithIntegerKeyWithEtag).FullName,
                                          grainRef,
                                          grainState);

            Assert.Null(grainState.ETag);
        }
        private async Task TestClearAsync <TGrain, TState, TKey>()
            where TState : Entity <TKey>, new()
            where TGrain : Grain <TState>
        {
            TestGrainState <TState> grainState = Internal.Utils.CreateAndStoreGrainState <TState>(_serviceProvider);

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

            await _storage.ClearStateAsync(typeof(TGrain).FullName,
                                           grainRef,
                                           grainState
                                           );

            var actual = Internal.Utils.FetchEntityFromDb(_serviceProvider, grainState.State);

            Assert.Null(actual);
        }
        public async Task SinglePropertyWrite()
        {
            GrainState <EntityWithIntegerKey> grainState =
                Internal.Utils.CreateAndStoreGrainState <EntityWithIntegerKey>(_serviceProvider);


            grainState.State.Title  = "Should get updated";
            grainState.State.KeyExt = "Should not get updated";


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

            GrainStorageContext <EntityWithIntegerKey> .ConfigureEntryState(
                entry => entry
                .Property(e => e.Title)
                .IsModified = true
                );

            await _storage.WriteStateAsync(typeof(GrainWithIntegerKey).FullName,
                                           grainRef,
                                           grainState);


            var stored = (EntityWithIntegerKey)
                         Internal.Utils.FetchEntityFromDb(_serviceProvider, grainState.State);

            Assert.Equal("Should get updated", stored?.Title);
            Assert.NotEqual("Should not get updated", stored?.KeyExt);


            GrainStorageContext <EntityWithIntegerKey> .Clear();

            // Future updates should update the whole object if not configured
            await _storage.WriteStateAsync(typeof(GrainWithIntegerKey).FullName,
                                           grainRef,
                                           grainState);

            stored = (EntityWithIntegerKey)
                     Internal.Utils.FetchEntityFromDb(_serviceProvider, grainState.State);

            Assert.Equal(stored, grainState.State);
        }