private global::StrawberryShake.EntityId? UpdateIGetHero_HeroEntity( global::System.Text.Json.JsonElement? obj, global::System.Collections.Generic.ISet <global::StrawberryShake.EntityId> entityIds) { if (!obj.HasValue) { return(null); } global::StrawberryShake.EntityId entityId = _extractId(obj.Value); entityIds.Add(entityId); if (entityId.Name.Equals("GetHero_Hero_Droid", global::System.StringComparison.Ordinal)) { DroidEntity entity = _entityStore.GetOrCreate <DroidEntity>(entityId); entity.Name = DeserializeNonNullableString(global::StrawberryShake.Transport.Http.JsonElementExtensions.GetPropertyOrNull(obj.Value, "name")); entity.AppearsIn = DeserializeEpisodeArray(global::StrawberryShake.Transport.Http.JsonElementExtensions.GetPropertyOrNull(obj.Value, "appearsIn")); return(entityId); } if (entityId.Name.Equals("GetHero_Hero_Human", global::System.StringComparison.Ordinal)) { HumanEntity entity = _entityStore.GetOrCreate <HumanEntity>(entityId); entity.Name = DeserializeNonNullableString(global::StrawberryShake.Transport.Http.JsonElementExtensions.GetPropertyOrNull(obj.Value, "name")); entity.AppearsIn = DeserializeEpisodeArray(global::StrawberryShake.Transport.Http.JsonElementExtensions.GetPropertyOrNull(obj.Value, "appearsIn")); return(entityId); } throw new global::System.NotSupportedException(); }
public async Task Watch_Interact_With_Persistence() { string fileName = Path.GetTempFileName(); string connectionString = "Data Source=" + fileName; File.Delete(fileName); try { { // arrange using var cts = new CancellationTokenSource(100_20_000); using IWebHost host = TestServerHelper.CreateServer( _ => { }, out var port); var serviceCollection = new ServiceCollection(); serviceCollection .AddStarWarsGetHeroClient() .ConfigureHttpClient( c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql")) .ConfigureWebSocketClient( c => c.Uri = new Uri("ws://localhost:" + port + "/graphql")) .AddSQLitePersistence(connectionString); await using var services = serviceCollection.BuildServiceProvider(); await services.GetRequiredService <SQLitePersistence>().InitializeAsync(); var client = services.GetRequiredService <StarWarsGetHeroClient>(); var storeAccessor = services.GetRequiredService <StarWarsGetHeroClientStoreAccessor>(); var entityId = new EntityId("Droid", "2001"); await Task.Delay(250, cts.Token); // act await client.GetHero.ExecuteAsync(cts.Token); string? name = null; IDisposable session = client.GetHero .Watch(ExecutionStrategy.CacheFirst) .Subscribe(result => name = result.Data?.Hero?.Name); while (name is null && !cts.Token.IsCancellationRequested) { await Task.Delay(10, cts.Token); } var name1 = name; name = null; storeAccessor.EntityStore.Update(s => { if (s.CurrentSnapshot.TryGetEntity(entityId, out DroidEntity? entity)) { entity = new DroidEntity("NewName"); s.SetEntity(entityId, entity); } }); while (name is null && !cts.Token.IsCancellationRequested) { await Task.Delay(10, cts.Token); } var name2 = name; name = null; session.Dispose(); // assert Assert.Equal("R2-D2", name1); Assert.Equal("NewName", name2); await Task.Delay(500, cts.Token); } { // arrange using var cts = new CancellationTokenSource(100_20_000); using IWebHost host = TestServerHelper.CreateServer( _ => { }, out var port); var serviceCollection = new ServiceCollection(); serviceCollection .AddStarWarsGetHeroClient() .ConfigureHttpClient( c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql")) .ConfigureWebSocketClient( c => c.Uri = new Uri("ws://localhost:" + port + "/graphql")) .AddSQLitePersistence(connectionString); await using var services = serviceCollection.BuildServiceProvider(); await services.GetRequiredService <SQLitePersistence>().InitializeAsync(); var client = services.GetRequiredService <StarWarsGetHeroClient>(); // act string?name = null; client.GetHero .Watch(ExecutionStrategy.CacheFirst) .Subscribe(result => { name = result.Data !.Hero !.Name; }); while (name is null && !cts.Token.IsCancellationRequested) { await Task.Delay(10, cts.Token); } // assert Assert.Equal("NewName", name); } } finally { if (File.Exists(fileName)) { File.Delete(fileName); } } }