Skip to content

Commit

Permalink
tests: Skip testing deprecated non-async client methods in latest Ela…
Browse files Browse the repository at this point in the history
…stic.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
  • Loading branch information
nr-ahemsath authored Jun 22, 2024
1 parent 74d388d commit 7a86f61
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
// 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;
using Elastic.Clients.Elasticsearch.QueryDsl;
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
Expand All @@ -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)).
Expand All @@ -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)]
Expand All @@ -86,7 +92,7 @@ public override async Task<bool> 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<FlightRecord>(s => s
.Index(IndexName)
.From(0)
Expand All @@ -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)]
Expand All @@ -124,13 +131,15 @@ public override async Task<long> 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)]
Expand All @@ -148,26 +157,11 @@ public override async Task<bool> 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<FlightRecord>() },
new MultisearchBody { From = 0, Query = new MatchAllQuery() }
)
]
};
#pragma warning disable CS0618 // obsolete usage is ok here
var response = _client.MultiSearch<FlightRecord>(req);
#pragma warning restore CS0618
#else
#pragma warning disable CS0618 // obsolete usage is ok here
#if SYNC_METHODS_OK
var response = _client.MultiSearch<FlightRecord>();
#pragma warning restore CS0618
#endif

#else
throw new NotImplementedException(NonAsyncDeprecationMessage);
#endif
}

[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
Expand All @@ -194,7 +188,7 @@ public override async Task<long> 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)
Expand All @@ -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())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -27,7 +27,7 @@ private enum ClientType
/// Sets the library to use for further actions
/// </summary>
/// <param name="client">ElasticsearchNet, NEST, or ElasticClients</param>
public void SetClient(string clientType)
public async Task SetClient(string clientType)
{
if (Enum.TryParse(clientType, out ClientType client))
{
Expand All @@ -44,7 +44,7 @@ public void SetClient(string clientType)
_client = new ElasticsearchElasticClient();
break;
}
_client.Connect();
await _client.ConnectAsync();
}
else
{
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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).
Expand All @@ -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)]
Expand Down Expand Up @@ -176,7 +176,7 @@ public override async Task<long> 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).
Expand All @@ -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}");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<StringResponse>();
}

[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
Expand Down Expand Up @@ -210,7 +211,7 @@ public override async Task<long> 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)
Expand All @@ -219,7 +220,7 @@ public override void GenerateError()
.RequestTimeout(TimeSpan.FromMinutes(2));

var client = new ElasticLowLevelClient(settings);
var response = client.Ping<StringResponse>();
var response = await client.PingAsync<StringResponse>();

if (response.Success)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -28,7 +28,7 @@ protected abstract string Password

public ElasticsearchTestClient() { }

public abstract void Connect();
public abstract Task ConnectAsync();

public abstract void Index();

Expand All @@ -46,7 +46,7 @@ public ElasticsearchTestClient() { }

public abstract Task<long> MultiSearchAsync();

public abstract void GenerateError();
public abstract Task GenerateErrorAsync();
}

public class FlightRecord
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,27 @@ 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)
{
_fixture = fixture;
_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));
Expand All @@ -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
(
Expand All @@ -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");
}

Expand Down Expand Up @@ -131,9 +153,9 @@ public void MultiSearchAsync()
}

[Fact]
public void Error()
public void ErrorAsync()
{
ValidateError("GenerateError");
ValidateError("GenerateErrorAsync");
}

private void ValidateError(string operationName)
Expand Down
Loading

0 comments on commit 7a86f61

Please sign in to comment.