forked from googleapis/google-cloud-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Stop using message strings to determine if an error message "s…
…ession expired" Fixes googleapis#10620.
- Loading branch information
Showing
4 changed files
with
153 additions
and
6 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...Spanner.Data/Google.Cloud.Spanner.Data.IntegrationTests/V1/Internal/ExecuteHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Google.Cloud.Spanner.Data; | ||
using Google.Cloud.Spanner.Data.IntegrationTests; | ||
using Grpc.Core; | ||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Google.Cloud.Spanner.V1.Internal.IntegrationTests; | ||
|
||
[Collection(nameof(SpannerDatabaseFixture))] | ||
[CommonTestDiagnostics] | ||
public class ExecuteHelperTests | ||
{ | ||
private readonly SpannerDatabaseFixture _fixture; | ||
|
||
public ExecuteHelperTests(SpannerDatabaseFixture fixture) => | ||
_fixture = fixture; | ||
|
||
[Fact] | ||
public Task SessionNotFound() => WithSessionPool(async pool => | ||
{ | ||
var session = await pool.Client.CreateSessionAsync(_fixture.DatabaseName); | ||
await pool.Client.DeleteSessionAsync(session.SessionName); | ||
// The session doesn't become invalid immediately after deletion. | ||
// Wait for a minute to ensure the session is really expired. | ||
await Task.Delay(TimeSpan.FromMinutes(1)); | ||
var request = new ExecuteSqlRequest | ||
{ | ||
Sql = $"SELECT 1", | ||
Session = session.Name | ||
}; | ||
var exception = await Assert.ThrowsAsync<RpcException>(() => pool.Client.ExecuteSqlAsync(request)); | ||
Assert.True(ExecuteHelper.IsSessionExpiredError(exception)); | ||
}); | ||
|
||
// This code is separated out in case we need more tests. It's really just fluff. | ||
private async Task WithSessionPool(Func<SessionPool, Task> action) | ||
{ | ||
var builder = new SpannerConnectionStringBuilder(_fixture.ConnectionString); | ||
var pool = await builder.AcquireSessionPoolAsync(); | ||
try | ||
{ | ||
await action(pool); | ||
} | ||
finally | ||
{ | ||
builder.SessionPoolManager.Release(pool); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...ogle.Cloud.Spanner.Data/Google.Cloud.Spanner.Data.Tests/V1/Internal/ExecuteHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Google.Protobuf; | ||
using Google.Rpc; | ||
using Grpc.Core; | ||
using Xunit; | ||
|
||
namespace Google.Cloud.Spanner.V1.Internal.Tests; | ||
|
||
public class ExecuteHelperTests | ||
{ | ||
private static readonly ResourceInfo s_sessionResourceInfo = new ResourceInfo { ResourceType = ExecuteHelper.SessionResourceType }; | ||
|
||
[Fact] | ||
public void IsSessionExpiredError_WrongStatusCode() => | ||
Assert.False(CreateException(StatusCode.Aborted, s_sessionResourceInfo.ToByteArray()).IsSessionExpiredError()); | ||
|
||
[Fact] | ||
public void IsSessionExpiredError_NoResourceInfo() => | ||
Assert.False(CreateException(StatusCode.NotFound, null).IsSessionExpiredError()); | ||
|
||
[Fact] | ||
public void IsSessionExpiredError_WrongResourceInfo() => | ||
Assert.False(CreateException(StatusCode.NotFound, new ResourceInfo { ResourceType = "not-a-session" }.ToByteArray()).IsSessionExpiredError()); | ||
|
||
[Fact] | ||
public void IsSessionExpiredError_InvalidResourceInfo() => | ||
Assert.False(CreateException(StatusCode.NotFound, new byte[1]).IsSessionExpiredError()); | ||
|
||
[Fact] | ||
public void IsSessionExpiredError_Valid() => | ||
Assert.True(CreateException(StatusCode.NotFound, s_sessionResourceInfo.ToByteArray()).IsSessionExpiredError()); | ||
|
||
private static RpcException CreateException(StatusCode code, byte[] resourceInfoData) | ||
{ | ||
var trailers = new Metadata(); | ||
if (resourceInfoData is not null) | ||
{ | ||
trailers.Add(ExecuteHelper.ResourceInfoMetadataKey, resourceInfoData); | ||
} | ||
return new RpcException(new Grpc.Core.Status(code, "Bang"), trailers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters