Skip to content

Commit

Permalink
call isolated
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <tsfarr@amazon.com>
  • Loading branch information
Xtansia committed Oct 31, 2023
1 parent 65e6835 commit f3e8884
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
13 changes: 10 additions & 3 deletions tests/Tests/Search/PointInTime/CreatePitApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

using System;
using System.Collections.Generic;
using FluentAssertions;
using OpenSearch.Client;
using OpenSearch.Net;
Expand All @@ -22,9 +23,15 @@ namespace Tests.Search.PointInTime;
public class CreatePitApiTests
: ApiIntegrationTestBase<ReadOnlyCluster, CreatePitResponse, ICreatePitRequest, CreatePitDescriptor, CreatePitRequest>
{
private static readonly Dictionary<string, string> Pits = new();

public CreatePitApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

private string _pitId;
private string CallIsolatedPit
{
get => Pits.TryGetValue(CallIsolatedValue, out var pit) ? pit : "default-for-unit-tests";
set => Pits[CallIsolatedValue] = value;
}

protected override bool ExpectIsValid => true;

Expand Down Expand Up @@ -58,12 +65,12 @@ protected override LazyResponses ClientUsage() => Calls(

protected override void ExpectResponse(CreatePitResponse response)
{
_pitId = response.PitId;
CallIsolatedPit = response.PitId;
response.ShouldBeValid();
response.PitId.Should().NotBeNullOrEmpty();
response.CreationTime.Should().BeCloseTo(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), 10000);
response.Shards.Should().NotBeNull();
}

protected override void OnAfterCall(IOpenSearchClient client) => client.DeletePit(d => d.PitId(_pitId));
protected override void OnAfterCall(IOpenSearchClient client) => client.DeletePit(d => d.PitId(CallIsolatedPit));
}
10 changes: 7 additions & 3 deletions tests/Tests/Search/PointInTime/DeleteAllPitsApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ namespace Tests.Search.PointInTime;
public class DeleteAllPitsApiTests
: ApiIntegrationTestBase<WritableCluster, DeleteAllPitsResponse, IDeleteAllPitsRequest, DeleteAllPitsDescriptor, DeleteAllPitsRequest>
{
private readonly List<string> _pitIds = new();
private static readonly Dictionary<string, List<string>> Pits = new();

public DeleteAllPitsApiTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

private List<string> CallIsolatedPits => Pits.TryGetValue(CallIsolatedValue, out var pits)
? pits
: Pits[CallIsolatedValue] = new List<string>();

protected override bool ExpectIsValid => true;

protected override object ExpectJson => null;
Expand Down Expand Up @@ -56,7 +60,7 @@ protected override void ExpectResponse(DeleteAllPitsResponse response)
response.Pits.Should()
.NotBeNull()
.And.HaveCount(5)
.And.BeEquivalentTo(_pitIds.Select(p => new DeletedPit
.And.BeEquivalentTo(CallIsolatedPits.Select(p => new DeletedPit
{
PitId = p,
Successful = true
Expand All @@ -71,7 +75,7 @@ protected override void OnBeforeCall(IOpenSearchClient client)
if (!pit.IsValid)
throw new Exception("Setup: Initial PIT failed.");

_pitIds.Add(pit.PitId);
CallIsolatedPits.Add(pit.PitId);
}
}
}
19 changes: 13 additions & 6 deletions tests/Tests/Search/PointInTime/DeletePitApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using OpenSearch.Client;
Expand All @@ -23,26 +24,32 @@ namespace Tests.Search.PointInTime;
public class DeletePitApiTests
: ApiIntegrationTestBase<ReadOnlyCluster, DeletePitResponse, IDeletePitRequest, DeletePitDescriptor, DeletePitRequest>
{
private string _pitId = "default-for-unit-tests";
private static readonly Dictionary<string, string> Pits = new();

public DeletePitApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

private string CallIsolatedPit
{
get => Pits.TryGetValue(CallIsolatedValue, out var pit) ? pit : "default-for-unit-tests";
set => Pits[CallIsolatedValue] = value;
}

protected override bool ExpectIsValid => true;

protected override object ExpectJson => new
{
pit_id = new[]
{
_pitId
CallIsolatedPit
}
};

protected override int ExpectStatusCode => 200;

protected override Func<DeletePitDescriptor, IDeletePitRequest> Fluent => d => d.PitId(_pitId);
protected override Func<DeletePitDescriptor, IDeletePitRequest> Fluent => d => d.PitId(CallIsolatedPit);
protected override HttpMethod HttpMethod => HttpMethod.DELETE;

protected override DeletePitRequest Initializer => new(_pitId);
protected override DeletePitRequest Initializer => new(CallIsolatedPit);
protected override bool SupportsDeserialization => false;
protected override string UrlPath => "/_search/point_in_time";

Expand All @@ -62,7 +69,7 @@ protected override void ExpectResponse(DeletePitResponse response)

var pit = response.Pits.First();
pit.Successful.Should().BeTrue();
pit.PitId.Should().Be(_pitId);
pit.PitId.Should().Be(CallIsolatedPit);
}

protected override void OnBeforeCall(IOpenSearchClient client)
Expand All @@ -71,6 +78,6 @@ protected override void OnBeforeCall(IOpenSearchClient client)
if (!pit.IsValid)
throw new Exception("Setup: Initial PIT failed.");

_pitId = pit.PitId;
CallIsolatedPit = pit.PitId;
}
}
15 changes: 9 additions & 6 deletions tests/Tests/Search/PointInTime/GetAllPitsApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
namespace Tests.Search.PointInTime;

[SkipVersion("<2.4.0", "Point-In-Time search support was added in version 2.4.0")]
public class GetAllPitsApiTests
public sealed class GetAllPitsApiTests
: ApiIntegrationTestBase<ReadOnlyCluster, GetAllPitsResponse, IGetAllPitsRequest, GetAllPitsDescriptor, GetAllPitsRequest>
{
private List<(string id, long creationTime)> _pits = new();
private static readonly Dictionary<string, List<(string id, long creationTime)>> Pits = new();

public GetAllPitsApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

private List<(string id, long creationTime)> CallIsolatedPits => Pits.TryGetValue(CallIsolatedValue, out var pits)
? pits
: Pits[CallIsolatedValue] = new List<(string id, long creationTime)>();

protected override bool ExpectIsValid => true;

protected override object ExpectJson => null;
Expand All @@ -52,12 +56,11 @@ protected override LazyResponses ClientUsage() => Calls(

protected override void ExpectResponse(GetAllPitsResponse response)
{
_pits.Should().HaveCount(5);
response.ShouldBeValid();
response.Pits.Should()
.NotBeNull()
.And.HaveCount(5)
.And.BeEquivalentTo(_pits.Select(p => new PitDetail
.And.BeEquivalentTo(CallIsolatedPits.Select(p => new PitDetail
{
PitId = p.id,
CreationTime = p.creationTime,
Expand All @@ -73,10 +76,10 @@ protected override void OnBeforeCall(IOpenSearchClient client)
if (!pit.IsValid)
throw new Exception("Setup: Initial PIT failed.");

_pits.Add((pit.PitId, pit.CreationTime));
CallIsolatedPits.Add((pit.PitId, pit.CreationTime));
}
}

protected override void OnAfterCall(IOpenSearchClient client) =>
client.DeletePit(d => d.PitId(_pits.Select(p => p.id)));
client.DeletePit(d => d.PitId(CallIsolatedPits.Select(p => p.id)));
}
19 changes: 13 additions & 6 deletions tests/Tests/Search/PointInTime/PitSearchApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using OpenSearch.Client;
Expand All @@ -28,9 +29,15 @@ public sealed class PitSearchApiTests :
SearchRequest<PitSearchApiTests.Doc>
>
{
private static readonly Dictionary<string, string> Pits = new();

public PitSearchApiTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

private string _pitId = "default-for-unit-tests";
private string CallIsolatedPit
{
get => Pits.TryGetValue(CallIsolatedValue, out var pit) ? pit : "default-for-unit-tests";
set => Pits[CallIsolatedValue] = value;
}

protected override object ExpectJson => new
{
Expand All @@ -40,7 +47,7 @@ public PitSearchApiTests(WritableCluster cluster, EndpointUsage usage) : base(cl
},
pit = new
{
id = _pitId,
id = CallIsolatedPit,
keep_alive = "1h"
},
track_total_hits = true
Expand All @@ -52,7 +59,7 @@ public PitSearchApiTests(WritableCluster cluster, EndpointUsage usage) : base(cl
protected override Func<SearchDescriptor<Doc>, ISearchRequest> Fluent => s => s
.Query(q => q.MatchAll())
.PointInTime(p => p
.Id(_pitId)
.Id(CallIsolatedPit)
.KeepAlive("1h"))
.TrackTotalHits();

Expand All @@ -64,7 +71,7 @@ public PitSearchApiTests(WritableCluster cluster, EndpointUsage usage) : base(cl
Query = new QueryContainer(new MatchAllQuery()),
PointInTime = new OpenSearch.Client.PointInTime
{
Id = _pitId,
Id = CallIsolatedPit,
KeepAlive = "1h"
},
TrackTotalHits = true
Expand Down Expand Up @@ -94,7 +101,7 @@ protected override void OnBeforeCall(IOpenSearchClient client)

var pitResp = client.CreatePit(CallIsolatedValue, p => p.KeepAlive("1h"));
pitResp.ShouldBeValid();
_pitId = pitResp.PitId;
CallIsolatedPit = pitResp.PitId;

bulkResp = client.Bulk(b => b
.Index(CallIsolatedValue)
Expand All @@ -103,7 +110,7 @@ protected override void OnBeforeCall(IOpenSearchClient client)
bulkResp.ShouldBeValid();
}

protected override void OnAfterCall(IOpenSearchClient client) => client.DeletePit(d => d.PitId(_pitId));
protected override void OnAfterCall(IOpenSearchClient client) => client.DeletePit(d => d.PitId(CallIsolatedPit));

protected override LazyResponses ClientUsage() => Calls(
(c, f) => c.Search(f),
Expand Down

0 comments on commit f3e8884

Please sign in to comment.