From b73f47de1d56092aa8a59124ac697959cbb07556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Tue, 2 Sep 2025 10:25:53 +0200 Subject: [PATCH] chore: test query logging --- .../SampleModel/SpannerSampleDbContext.cs | 8 ++- .../SampleRunner.cs | 67 ++++++++++++++++--- .../EntityFrameworkMockServerTests.cs | 1 + 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/Google.Cloud.EntityFrameworkCore.Spanner.Samples/SampleModel/SpannerSampleDbContext.cs b/Google.Cloud.EntityFrameworkCore.Spanner.Samples/SampleModel/SpannerSampleDbContext.cs index fd60ba87..e0128633 100644 --- a/Google.Cloud.EntityFrameworkCore.Spanner.Samples/SampleModel/SpannerSampleDbContext.cs +++ b/Google.Cloud.EntityFrameworkCore.Spanner.Samples/SampleModel/SpannerSampleDbContext.cs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; using Google.Cloud.EntityFrameworkCore.Spanner.Extensions; using Google.Cloud.EntityFrameworkCore.Spanner.Infrastructure; using Google.Cloud.EntityFrameworkCore.Spanner.Metadata; @@ -65,9 +66,14 @@ public SpannerSampleDbContext(string connectionString, DbContextOptions Invoices { get; set; } public virtual DbSet InvoiceLines { get; set; } + public void Log(string message) + { + Console.WriteLine(message); + } + protected override void OnConfiguring(DbContextOptionsBuilder options) // Configure Entity Framework to use a Cloud Spanner database. - => options.UseSpanner(_connectionString); + => options.LogTo(Log).UseSpanner(_connectionString).UseMutations(MutationUsage.Never); protected override void OnModelCreating(ModelBuilder modelBuilder) { diff --git a/Google.Cloud.EntityFrameworkCore.Spanner.Samples/SampleRunner.cs b/Google.Cloud.EntityFrameworkCore.Spanner.Samples/SampleRunner.cs index 24ad0e1c..f11e612e 100644 --- a/Google.Cloud.EntityFrameworkCore.Spanner.Samples/SampleRunner.cs +++ b/Google.Cloud.EntityFrameworkCore.Spanner.Samples/SampleRunner.cs @@ -98,15 +98,30 @@ private static void RunSample(string sampleName, bool failOnException) private static async Task RunSampleAsync(Func sampleMethod) { - var emulatorRunner = new EmulatorRunner(); + bool.TryParse(Environment.GetEnvironmentVariable("USE_EXISTING_EMULATOR") ?? "false", out var useExistingEmulator); + var emulatorRunner = useExistingEmulator ? null : new EmulatorRunner(); try { - Console.WriteLine(""); - Console.WriteLine("Starting emulator..."); - var portBinding = await emulatorRunner.StartEmulator(); - Console.WriteLine($"Emulator started on port {portBinding.HostPort}"); - Console.WriteLine(""); - Environment.SetEnvironmentVariable("SPANNER_EMULATOR_HOST", $"localhost:{portBinding.HostPort}"); + if (useExistingEmulator) + { + var host = Environment.GetEnvironmentVariable("SPANNER_EMULATOR_HOST"); + if (host == null) + { + host = "localhost:9010"; + Environment.SetEnvironmentVariable("SPANNER_EMULATOR_HOST", host); + } + Console.WriteLine(""); + Console.WriteLine($"Using existing emulator on {host}..."); + } + else + { + Console.WriteLine(""); + Console.WriteLine("Starting emulator..."); + var portBinding = await emulatorRunner.StartEmulator(); + Console.WriteLine($"Emulator started on port {portBinding.HostPort}"); + Console.WriteLine(""); + Environment.SetEnvironmentVariable("SPANNER_EMULATOR_HOST", $"localhost:{portBinding.HostPort}"); + } var projectId = "sample-project"; var instanceId = "sample-instance"; @@ -117,6 +132,10 @@ private static async Task RunSampleAsync(Func sampleMethod) { EmulatorDetection = EmulatorDetection.EmulatorOnly, }; + if (useExistingEmulator) + { + await MaybeDeleteInstanceOnEmulatorAsync(databaseName.ProjectId, databaseName.InstanceId); + } await MaybeCreateInstanceOnEmulatorAsync(databaseName.ProjectId, databaseName.InstanceId); await MaybeCreateDatabaseOnEmulatorAsync(databaseName); @@ -129,10 +148,13 @@ private static async Task RunSampleAsync(Func sampleMethod) } finally { - Console.WriteLine(""); - Console.WriteLine("Stopping emulator..."); - emulatorRunner.StopEmulator().WaitWithUnwrappedExceptions(); - Console.WriteLine(""); + if (!useExistingEmulator) + { + Console.WriteLine(""); + Console.WriteLine("Stopping emulator..."); + emulatorRunner.StopEmulator().WaitWithUnwrappedExceptions(); + Console.WriteLine(""); + } } } @@ -164,6 +186,29 @@ private static MethodInfo GetSampleMethod(string sampleName) } } + private static async Task MaybeDeleteInstanceOnEmulatorAsync(string projectId, string instanceId) + { + // Try to delete the instance on the emulator and ignore any NotFound error. + var adminClientBuilder = new InstanceAdminClientBuilder + { + EmulatorDetection = EmulatorDetection.EmulatorOnly + }; + var instanceAdminClient = await adminClientBuilder.BuildAsync(); + + var instanceName = InstanceName.FromProjectInstance(projectId, instanceId); + try + { + await instanceAdminClient.DeleteInstanceAsync(new DeleteInstanceRequest + { + InstanceName = InstanceName.FromProjectInstance(projectId, instanceId), + }); + } + catch (RpcException e) when (e.StatusCode == StatusCode.NotFound) + { + // Ignore + } + } + private static async Task MaybeCreateInstanceOnEmulatorAsync(string projectId, string instanceId) { // Try to create an instance on the emulator and ignore any AlreadyExists error. diff --git a/Google.Cloud.EntityFrameworkCore.Spanner.Tests/EntityFrameworkMockServerTests.cs b/Google.Cloud.EntityFrameworkCore.Spanner.Tests/EntityFrameworkMockServerTests.cs index 49e4f607..53389cce 100644 --- a/Google.Cloud.EntityFrameworkCore.Spanner.Tests/EntityFrameworkMockServerTests.cs +++ b/Google.Cloud.EntityFrameworkCore.Spanner.Tests/EntityFrameworkMockServerTests.cs @@ -32,6 +32,7 @@ using System.Text.Json; using System.Text.RegularExpressions; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Xunit; using SpannerDate = Google.Cloud.EntityFrameworkCore.Spanner.Storage.SpannerDate; using V1 = Google.Cloud.Spanner.V1;