From 7a86f61ba944d85a053e3120da71440298f33066 Mon Sep 17 00:00:00 2001 From: Alex Hemsath <57361211+nr-ahemsath@users.noreply.github.com> Date: Fri, 21 Jun 2024 17:40:11 -0700 Subject: [PATCH] tests: Skip testing deprecated non-async client methods in latest Elastic.Clients.Elasticsearch versions (#2569) * Do not test non-async library methods for latest Elastic.Clients.Elasticsearch * Make GenerateError be async all the way down * Same thing for Connect * Make it clear that non-async test methods are being skipped in latest Elastic.Clients.Elasticsearch * PR feedback --- .../ElasticsearchElasticClient.cs | 66 ++++++++----------- .../Elasticsearch/ElasticsearchExerciser.cs | 8 +-- .../Elasticsearch/ElasticsearchNestClient.cs | 10 +-- .../Elasticsearch/ElasticsearchNetClient.cs | 9 +-- .../Elasticsearch/ElasticsearchTestClient.cs | 6 +- .../Elasticsearch/ElasticsearchTests.cs | 44 +++++++++---- .../UnboundedIntegrationTests.csproj | 3 +- 7 files changed, 81 insertions(+), 65 deletions(-) diff --git a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchElasticClient.cs b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchElasticClient.cs index 7ec16bc80d..24ea84ff13 100644 --- a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchElasticClient.cs +++ b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchElasticClient.cs @@ -1,11 +1,13 @@ // Copyright 2020 New Relic, Inc. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +// Non-async client methods are deprecated in the latest Elastic.Clients.Elasticsearch +#if !NET481_OR_GREATER && !NET8_0_OR_GREATER +#define SYNC_METHODS_OK +#endif + using System; -using System.Collections.Generic; -using System.Net; using System.Runtime.CompilerServices; -using System.Threading; using System.Threading.Tasks; using Elastic.Clients.Elasticsearch; using Elastic.Clients.Elasticsearch.Core.MSearch; @@ -13,11 +15,15 @@ using Elastic.Transport; using NewRelic.Agent.IntegrationTests.Shared; + namespace MultiFunctionApplicationHelpers.NetStandardLibraries.Elasticsearch { internal class ElasticsearchElasticClient : ElasticsearchTestClient { private ElasticsearchClient _client; + + private const string NonAsyncDeprecationMessage = "Non-async methods are deprecated in the latest Elasticsearch clients."; + protected override Uri Address { get @@ -41,7 +47,7 @@ protected override string Password } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - public override void Connect() + public override async Task ConnectAsync() { var settings = new ElasticsearchClientSettings(Address) .Authentication(new BasicAuthentication(Username, Password)). @@ -51,23 +57,23 @@ public override void Connect() // This isn't necessary but will log the response, which can help troubleshoot if // you're having connection errors -#pragma warning disable CS0618 // obsolete usage is ok here - _client.Ping(); -#pragma warning restore CS0618 + _ = await _client.PingAsync(); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] public override void Index() { +#if SYNC_METHODS_OK var record = FlightRecord.GetSample(); -#pragma warning disable CS0618 // Type or member is obsolete var response = _client.Index(record, (IndexName)IndexName); -#pragma warning restore CS0618 // Type or member is obsolete if (!response.IsSuccess()) { throw new Exception($"Response was not successful. {response.ElasticsearchServerError}"); } +#else + throw new NotImplementedException(NonAsyncDeprecationMessage); +#endif } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] @@ -86,7 +92,7 @@ public override async Task IndexAsync() [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] public override void Search() { -#pragma warning disable CS0618 // obsolete usage is ok here +#if SYNC_METHODS_OK var response = _client.Search(s => s .Index(IndexName) .From(0) @@ -97,9 +103,10 @@ public override void Search() ) ) ); -#pragma warning restore CS0618 - AssertResponseIsSuccess(response); +#else + throw new NotImplementedException(NonAsyncDeprecationMessage); +#endif } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] @@ -124,13 +131,15 @@ public override async Task SearchAsync() [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] public override void IndexMany() { +#if SYNC_METHODS_OK var records = FlightRecord.GetSamples(3); -#pragma warning disable CS0618 // Type or member is obsolete var response = _client.IndexMany(records, (IndexName)IndexName); -#pragma warning restore CS0618 // Type or member is obsolete AssertResponseIsSuccess(response); +#else + throw new NotImplementedException(NonAsyncDeprecationMessage); +#endif } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] @@ -148,26 +157,11 @@ public override async Task IndexManyAsync() [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] public override void MultiSearch() { -#if NET8_0_OR_GREATER || NET481_OR_GREATER - var req = new MultiSearchRequest - { - Searches = - [ - new SearchRequestItem( - new MultisearchHeader { Indices = Infer.Index() }, - new MultisearchBody { From = 0, Query = new MatchAllQuery() } - ) - ] - }; -#pragma warning disable CS0618 // obsolete usage is ok here - var response = _client.MultiSearch(req); -#pragma warning restore CS0618 -#else -#pragma warning disable CS0618 // obsolete usage is ok here +#if SYNC_METHODS_OK var response = _client.MultiSearch(); -#pragma warning restore CS0618 -#endif - +#else + throw new NotImplementedException(NonAsyncDeprecationMessage); +#endif } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] @@ -194,7 +188,7 @@ public override async Task MultiSearchAsync() } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - public override void GenerateError() + public override async Task GenerateErrorAsync() { // This isn't the password, so connection should fail, but we won't get an error until the Ping var settings = new ElasticsearchClientSettings(Address) @@ -204,9 +198,7 @@ public override void GenerateError() var client = new ElasticsearchClient(settings); -#pragma warning disable CS0618 // obsolete usage is ok here - var response = client.Ping(); -#pragma warning restore CS0618 + var response = await client.PingAsync(); if (response.IsSuccess()) { diff --git a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchExerciser.cs b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchExerciser.cs index 7aa5bcd25d..2f6753f4dc 100644 --- a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchExerciser.cs +++ b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchExerciser.cs @@ -1,4 +1,4 @@ -// Copyright 2020 New Relic, Inc. All rights reserved. +// Copyright 2020 New Relic, Inc. All rights reserved. // SPDX-License-Identifier: Apache-2.0 using System; @@ -27,7 +27,7 @@ private enum ClientType /// Sets the library to use for further actions /// /// ElasticsearchNet, NEST, or ElasticClients - public void SetClient(string clientType) + public async Task SetClient(string clientType) { if (Enum.TryParse(clientType, out ClientType client)) { @@ -44,7 +44,7 @@ public void SetClient(string clientType) _client = new ElasticsearchElasticClient(); break; } - _client.Connect(); + await _client.ConnectAsync(); } else { @@ -94,6 +94,6 @@ public void SetClient(string clientType) [LibraryMethod] [Transaction] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - public void GenerateError() => _client.GenerateError(); + public async Task GenerateErrorAsync() => await _client.GenerateErrorAsync(); } } diff --git a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchNestClient.cs b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchNestClient.cs index 96ce98fd2f..fdc14fc728 100644 --- a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchNestClient.cs +++ b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchNestClient.cs @@ -1,4 +1,4 @@ -// Copyright 2020 New Relic, Inc. All rights reserved. +// Copyright 2020 New Relic, Inc. All rights reserved. // SPDX-License-Identifier: Apache-2.0 using System; @@ -36,7 +36,7 @@ protected override string Password } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - public override void Connect() + public override async Task ConnectAsync() { var settings = new ConnectionSettings(Address). BasicAuthentication(Username,Password). @@ -46,7 +46,7 @@ public override void Connect() // This isn't necessary but will log the response, which can help troubleshoot if // you're having connection errors - _client.Ping(); + await _client.PingAsync(); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] @@ -176,7 +176,7 @@ public override async Task MultiSearchAsync() } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - public override void GenerateError() + public override async Task GenerateErrorAsync() { // This isn't the password, so connection should fail, but we won't get an error until the Ping var settings = new ConnectionSettings(Address). @@ -186,7 +186,7 @@ public override void GenerateError() var client = new ElasticClient(settings); - var response = client.Ping(); + var response = await client.PingAsync(); if (response.IsValid) { throw new Exception($"Response was successful but we expected an error. {response.ServerError}"); diff --git a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchNetClient.cs b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchNetClient.cs index 9c821b4e0f..b71df23041 100644 --- a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchNetClient.cs +++ b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchNetClient.cs @@ -1,4 +1,4 @@ -// Copyright 2020 New Relic, Inc. All rights reserved. +// Copyright 2020 New Relic, Inc. All rights reserved. // SPDX-License-Identifier: Apache-2.0 using System; @@ -36,13 +36,14 @@ protected override string Password } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - public override void Connect() + public override async Task ConnectAsync() { var settings = new ConnectionConfiguration(Address) .BasicAuthentication(Username, Password) .RequestTimeout(TimeSpan.FromMinutes(2)); _client = new ElasticLowLevelClient(settings); + await _client.PingAsync(); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] @@ -210,7 +211,7 @@ public override async Task MultiSearchAsync() } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - public override void GenerateError() + public override async Task GenerateErrorAsync() { // This isn't the password, so connection should fail, but we won't get an error until the Ping var settings = new ConnectionConfiguration(Address) @@ -219,7 +220,7 @@ public override void GenerateError() .RequestTimeout(TimeSpan.FromMinutes(2)); var client = new ElasticLowLevelClient(settings); - var response = client.Ping(); + var response = await client.PingAsync(); if (response.Success) { diff --git a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchTestClient.cs b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchTestClient.cs index f199d35f3b..69cc21bffa 100644 --- a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchTestClient.cs +++ b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/NetStandardLibraries/Elasticsearch/ElasticsearchTestClient.cs @@ -1,4 +1,4 @@ -// Copyright 2020 New Relic, Inc. All rights reserved. +// Copyright 2020 New Relic, Inc. All rights reserved. // SPDX-License-Identifier: Apache-2.0 using System; @@ -28,7 +28,7 @@ protected abstract string Password public ElasticsearchTestClient() { } - public abstract void Connect(); + public abstract Task ConnectAsync(); public abstract void Index(); @@ -46,7 +46,7 @@ public ElasticsearchTestClient() { } public abstract Task MultiSearchAsync(); - public abstract void GenerateError(); + public abstract Task GenerateErrorAsync(); } public class FlightRecord diff --git a/tests/Agent/IntegrationTests/UnboundedIntegrationTests/Elasticsearch/ElasticsearchTests.cs b/tests/Agent/IntegrationTests/UnboundedIntegrationTests/Elasticsearch/ElasticsearchTests.cs index bb931ef81c..73c88b8568 100644 --- a/tests/Agent/IntegrationTests/UnboundedIntegrationTests/Elasticsearch/ElasticsearchTests.cs +++ b/tests/Agent/IntegrationTests/UnboundedIntegrationTests/Elasticsearch/ElasticsearchTests.cs @@ -33,6 +33,9 @@ protected enum ClientType const string IndexName = "flights"; + protected readonly bool _syncMethodsOk; + const string SyncMethodSkipReason = "Synchronous methods are deprecated in latest Elastic.Clients.Elasticsearch"; + protected ElasticsearchTestsBase(TFixture fixture, ITestOutputHelper output, ClientType clientType) : base(fixture) { @@ -40,6 +43,17 @@ protected ElasticsearchTestsBase(TFixture fixture, ITestOutputHelper output, Cli _fixture.TestLogger = output; _clientType = clientType; + // non-async methods are deprecated in the latest Elastic.Clients.Elasticsearch versions + if (_clientType != ClientType.ElasticClients || + (_fixture.GetType() != typeof(ConsoleDynamicMethodFixtureCoreLatest) && _fixture.GetType() != typeof(ConsoleDynamicMethodFixtureFWLatest))) + { + _syncMethodsOk = true; + } + else + { + _syncMethodsOk = false; + } + _host = GetHostFromElasticServer(_clientType); _fixture.SetTimeout(TimeSpan.FromMinutes(2)); @@ -51,13 +65,17 @@ protected ElasticsearchTestsBase(TFixture fixture, ITestOutputHelper output, Cli _fixture.AddCommand($"ElasticsearchExerciser SearchAsync"); _fixture.AddCommand($"ElasticsearchExerciser IndexManyAsync"); _fixture.AddCommand($"ElasticsearchExerciser MultiSearchAsync"); + _fixture.AddCommand($"ElasticsearchExerciser GenerateErrorAsync"); // Sync operations - _fixture.AddCommand($"ElasticsearchExerciser Index"); - _fixture.AddCommand($"ElasticsearchExerciser Search"); - _fixture.AddCommand($"ElasticsearchExerciser IndexMany"); - _fixture.AddCommand($"ElasticsearchExerciser MultiSearch"); - _fixture.AddCommand($"ElasticsearchExerciser GenerateError"); + if (_syncMethodsOk ) + { + _fixture.AddCommand($"ElasticsearchExerciser Index"); + _fixture.AddCommand($"ElasticsearchExerciser Search"); + _fixture.AddCommand($"ElasticsearchExerciser IndexMany"); + _fixture.AddCommand($"ElasticsearchExerciser MultiSearch"); + + } _fixture.AddActions ( @@ -82,27 +100,31 @@ protected ElasticsearchTestsBase(TFixture fixture, ITestOutputHelper output, Cli _fixture.Initialize(); } - [Fact] + [SkippableFact] public void Index() { + Skip.IfNot(_syncMethodsOk, SyncMethodSkipReason); ValidateOperation("Index"); } - [Fact] + [SkippableFact] public void Search() { + Skip.IfNot(_syncMethodsOk, SyncMethodSkipReason); ValidateOperation("Search"); } - [Fact] + [SkippableFact] public void IndexMany() { + Skip.IfNot(_syncMethodsOk, SyncMethodSkipReason); ValidateOperation("IndexMany"); } - [Fact] + [SkippableFact] public void MultiSearch() { + Skip.IfNot(_syncMethodsOk, SyncMethodSkipReason); ValidateOperation("MultiSearch"); } @@ -131,9 +153,9 @@ public void MultiSearchAsync() } [Fact] - public void Error() + public void ErrorAsync() { - ValidateError("GenerateError"); + ValidateError("GenerateErrorAsync"); } private void ValidateError(string operationName) diff --git a/tests/Agent/IntegrationTests/UnboundedIntegrationTests/UnboundedIntegrationTests.csproj b/tests/Agent/IntegrationTests/UnboundedIntegrationTests/UnboundedIntegrationTests.csproj index fc24b8e8cf..91ef2ffc3e 100644 --- a/tests/Agent/IntegrationTests/UnboundedIntegrationTests/UnboundedIntegrationTests.csproj +++ b/tests/Agent/IntegrationTests/UnboundedIntegrationTests/UnboundedIntegrationTests.csproj @@ -1,4 +1,4 @@ - + NewRelic.Agent.UnboundedIntegrationTests NewRelic.Agent.UnboundedIntegrationTests @@ -58,6 +58,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive +