Skip to content

Commit

Permalink
feat: Validate that client and credential universe domain are the same.
Browse files Browse the repository at this point in the history
  • Loading branch information
amanda-tarafa committed Jan 30, 2024
1 parent 95c5d28 commit c12567d
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 64 deletions.
49 changes: 37 additions & 12 deletions Google.Api.Gax.Grpc.IntegrationTests/ChannelPoolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ public class ChannelPoolTest
{
private static readonly GrpcAdapter Grpc = GrpcCoreAdapter.Instance;
private static readonly ServiceMetadata ServiceMetadata = TestServiceMetadata.TestService;
private const string DefaultUniverseDomain = TestServiceMetadata.DefaultUniverseDomain;

[Fact]
public void SameEndpoint_SameChannel()
{
var pool = new ChannelPool(ServiceMetadata);
using (var fixture = new TestServiceFixture())
{
var channel1 = pool.GetChannel(Grpc, fixture.Endpoint, GrpcChannelOptions.Empty);
var channel2 = pool.GetChannel(Grpc, fixture.Endpoint, GrpcChannelOptions.Empty);
Assert.Same(channel1, channel2);
var channel1 = pool.GetChannel(Grpc, DefaultUniverseDomain, fixture.Endpoint, GrpcChannelOptions.Empty);
var channel2 = pool.GetChannel(Grpc, DefaultUniverseDomain, fixture.Endpoint, GrpcChannelOptions.Empty);
Assert.Same(channel1, channel2);
}
}

Expand All @@ -33,8 +34,8 @@ public void DifferentEndpoint_DifferentChannel()
var pool = new ChannelPool(ServiceMetadata);
using (TestServiceFixture fixture1 = new TestServiceFixture(), fixture2 = new TestServiceFixture())
{
var channel1 = pool.GetChannel(Grpc, fixture1.Endpoint, GrpcChannelOptions.Empty);
var channel2 = pool.GetChannel(Grpc, fixture2.Endpoint, GrpcChannelOptions.Empty);
var channel1 = pool.GetChannel(Grpc, DefaultUniverseDomain, fixture1.Endpoint, GrpcChannelOptions.Empty);
var channel2 = pool.GetChannel(Grpc, DefaultUniverseDomain, fixture2.Endpoint, GrpcChannelOptions.Empty);
Assert.NotSame(channel1, channel2);
}
}
Expand All @@ -47,8 +48,8 @@ public void SameOptions_SameChannel()
var pool = new ChannelPool(ServiceMetadata);
using (var fixture = new TestServiceFixture())
{
var channel1 = pool.GetChannel(Grpc, fixture.Endpoint, options1);
var channel2 = pool.GetChannel(Grpc, fixture.Endpoint, options2);
var channel1 = pool.GetChannel(Grpc, DefaultUniverseDomain, fixture.Endpoint, options1);
var channel2 = pool.GetChannel(Grpc, DefaultUniverseDomain, fixture.Endpoint, options2);
Assert.Same(channel1, channel2);
}
}
Expand All @@ -61,8 +62,32 @@ public void DifferentOptions_DifferentChannel()
var pool = new ChannelPool(ServiceMetadata);
using (var fixture = new TestServiceFixture())
{
var channel1 = pool.GetChannel(Grpc, fixture.Endpoint, options1);
var channel2 = pool.GetChannel(Grpc, fixture.Endpoint, options2);
var channel1 = pool.GetChannel(Grpc, DefaultUniverseDomain, fixture.Endpoint, options1);
var channel2 = pool.GetChannel(Grpc, DefaultUniverseDomain, fixture.Endpoint, options2);
Assert.NotSame(channel1, channel2);
}
}

[Fact]
public void SameUniverseDomain_SameChannel()
{
var pool = new ChannelPool(ServiceMetadata);
using (var fixture = new TestServiceFixture())
{
var channel1 = pool.GetChannel(Grpc, "nowhere.com", fixture.Endpoint, GrpcChannelOptions.Empty);
var channel2 = pool.GetChannel(Grpc, "nowhere.com", fixture.Endpoint, GrpcChannelOptions.Empty);
Assert.Same(channel1, channel2);
}
}

[Fact]
public void DifferentUniverseDomain_DifferentChannel()
{
var pool = new ChannelPool(ServiceMetadata);
using (TestServiceFixture fixture = new TestServiceFixture())
{
var channel1 = pool.GetChannel(Grpc, "nowhere.com", fixture.Endpoint, GrpcChannelOptions.Empty);
var channel2 = pool.GetChannel(Grpc, "somewhere.com", fixture.Endpoint, GrpcChannelOptions.Empty);
Assert.NotSame(channel1, channel2);
}
}
Expand All @@ -73,7 +98,7 @@ public async Task ShutdownAsync_ShutsDownChannel()
var pool = new ChannelPool(ServiceMetadata);
using (var fixture = new TestServiceFixture())
{
var channel = (Channel) pool.GetChannel(Grpc, fixture.Endpoint, GrpcChannelOptions.Empty);
var channel = (Channel) pool.GetChannel(Grpc, DefaultUniverseDomain, fixture.Endpoint, GrpcChannelOptions.Empty);
Assert.NotEqual(ChannelState.Shutdown, channel.State);
await pool.ShutdownChannelsAsync();
Assert.Equal(ChannelState.Shutdown, channel.State);
Expand All @@ -86,10 +111,10 @@ public void ShutdownAsync_EmptiesPool()
var pool = new ChannelPool(ServiceMetadata);
using (var fixture = new TestServiceFixture())
{
var channel1 = pool.GetChannel(Grpc, fixture.Endpoint, GrpcChannelOptions.Empty);
var channel1 = pool.GetChannel(Grpc, DefaultUniverseDomain, fixture.Endpoint, GrpcChannelOptions.Empty);
// Note: *not* waiting for this to complete.
pool.ShutdownChannelsAsync();
var channel2 = pool.GetChannel(Grpc, fixture.Endpoint, GrpcChannelOptions.Empty);
var channel2 = pool.GetChannel(Grpc, DefaultUniverseDomain, fixture.Endpoint, GrpcChannelOptions.Empty);
Assert.NotSame(channel1, channel2);
}
}
Expand Down
36 changes: 31 additions & 5 deletions Google.Api.Gax.Grpc.IntegrationTests/ClientBuilderBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task DefaultsToChannelPool()
{
var builder = new SampleClientBuilder();

ChannelBase channelFromPool = builder.ChannelPool.GetChannel(fakeGrpcAdapter, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);
ChannelBase channelFromPool = builder.ChannelPool.GetChannel(fakeGrpcAdapter, TestServiceMetadata.DefaultUniverseDomain, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);

Action<CallInvoker> validator = invoker =>
{
Expand All @@ -71,8 +71,8 @@ public async Task DifferentEndpoint_StillFromChannelPool()
var endpoint = "custom.nowhere.com";
var builder = new SampleClientBuilder { Endpoint = endpoint };

ChannelBase channelFromPoolWithDefaultEndpoint = builder.ChannelPool.GetChannel(fakeGrpcAdapter, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);
ChannelBase channelFromPoolWithCustomEndpoint = builder.ChannelPool.GetChannel(fakeGrpcAdapter, "custom.nowhere.com", SampleClientBuilder.DefaultOptions);
ChannelBase channelFromPoolWithDefaultEndpoint = builder.ChannelPool.GetChannel(fakeGrpcAdapter, TestServiceMetadata.DefaultUniverseDomain, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);
ChannelBase channelFromPoolWithCustomEndpoint = builder.ChannelPool.GetChannel(fakeGrpcAdapter, TestServiceMetadata.DefaultUniverseDomain, "custom.nowhere.com", SampleClientBuilder.DefaultOptions);

Action<CallInvoker> validator = invoker =>
{
Expand All @@ -84,6 +84,26 @@ public async Task DifferentEndpoint_StillFromChannelPool()
await ValidateResultAsync(builder, validator);
}

[Fact]
public async Task DifferentUniverseDomain_StillFromChannelPool()
{
var universeDomain = "nowhere.com";
var builder = new SampleClientBuilder { UniverseDomain = universeDomain };

ChannelBase channelFromPoolWithDefaultUniverseDomain = builder.ChannelPool.GetChannel(fakeGrpcAdapter, TestServiceMetadata.DefaultUniverseDomain, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);
// The endpoint changes with the universe domain so we cannot use the default endpoint here, even if we are just changing the universe domain.
ChannelBase channelFromPoolWithCustomUniverseDomain = builder.ChannelPool.GetChannel(fakeGrpcAdapter, universeDomain, builder.GetEffectiveEndpoint(), SampleClientBuilder.DefaultOptions);

Action<CallInvoker> validator = invoker =>
{
var channelFromBuilder = GetChannel(invoker);
Assert.Same(channelFromPoolWithCustomUniverseDomain, channelFromBuilder);
Assert.NotSame(channelFromPoolWithDefaultUniverseDomain, channelFromBuilder);
Assert.Null(builder.LastCreatedChannel);
};
await ValidateResultAsync(builder, validator);
}

[Fact]
public async Task CustomChannelCredentials()
{
Expand Down Expand Up @@ -195,7 +215,7 @@ public async Task TokenAccessMethod()
public async Task JwtClientEnabledTest(bool clientUsesJwt, bool poolUsesJwt)
{
var builder = new SampleClientBuilder(clientUsesJwt, poolUsesJwt) { JsonCredentials = DummyServiceAccountCredentialFileContents };
ChannelBase channelFromPool = builder.ChannelPool.GetChannel(fakeGrpcAdapter, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);
ChannelBase channelFromPool = builder.ChannelPool.GetChannel(fakeGrpcAdapter, TestServiceMetadata.DefaultUniverseDomain, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);

// Jwt of client does not match pool, so we won't use channel pool
await ValidateResultAsync(builder, AssertNonChannelPool(builder));
Expand All @@ -206,7 +226,7 @@ public async Task JwtClientEnabledTest(bool clientUsesJwt, bool poolUsesJwt)
public async Task JwtClientAndPoolEnabledTest(bool enabledJwts)
{
var builder = new SampleClientBuilder(enabledJwts, enabledJwts);
ChannelBase channelFromPool = builder.ChannelPool.GetChannel(fakeGrpcAdapter, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);
ChannelBase channelFromPool = builder.ChannelPool.GetChannel(fakeGrpcAdapter, TestServiceMetadata.DefaultUniverseDomain, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);

// Jwt is either enabled or disabled for both client and pool
// We use channel pool
Expand Down Expand Up @@ -290,6 +310,12 @@ public class SampleClientBuilder : ClientBuilderBase<CallInvoker>
public string EndpointUsedToCreateChannel { get; private set; }
public ChannelCredentials CredentialsUsedToCreateChannel { get; private set; }

public new string EffectiveUniverseDomain => base.EffectiveUniverseDomain;

public new string GetEffectiveEndpoint() => base.GetEffectiveEndpoint();



private readonly string _name;

/// <summary>
Expand Down
39 changes: 23 additions & 16 deletions Google.Api.Gax.Grpc.IntegrationTests/Gcp/GrpcCallInvokerPoolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
*/

using Google.Api.Gax.Grpc.IntegrationTests;
using Google.Protobuf.Reflection;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Google.Api.Gax.Grpc.Gcp.IntegrationTests
Expand All @@ -20,23 +17,23 @@ public class GcpCallInvokerPoolTest
private static readonly ApiConfig Config1 = new ApiConfig { ChannelPool = new ChannelPoolConfig { MaxSize = 5 } };

[Fact]
public void SameEndpointAndOptions_SameCallInvoker()
public void SameEndpointAndOptionsAndUniverseDomain_SameCallInvoker()
{
var pool = new GcpCallInvokerPool(TestServiceMetadata.TestService);
var options = GrpcChannelOptions.Empty.WithPrimaryUserAgent("abc");
var callInvoker1 = pool.GetCallInvoker("endpoint", options, Config1, FakeAdapter);
var callInvoker2 = pool.GetCallInvoker("endpoint", options, Config1, FakeAdapter);
var callInvoker1 = pool.GetCallInvoker("domain", "endpoint", options, Config1, FakeAdapter);
var callInvoker2 = pool.GetCallInvoker("domain", "endpoint", options, Config1, FakeAdapter);
Assert.Same(callInvoker1, callInvoker2);
}

[Fact]
public void SameEndpointAndEqualOptions_SameCallInvoker()
public void SameEndpointAndUniverseDomainAndEqualOptions_SameCallInvoker()
{
var pool = new GcpCallInvokerPool(TestServiceMetadata.TestService);
var options1 = GrpcChannelOptions.Empty.WithPrimaryUserAgent("abc");
var options2 = GrpcChannelOptions.Empty.WithPrimaryUserAgent("abc");
var callInvoker1 = pool.GetCallInvoker("endpoint", options1, Config1, FakeAdapter);
var callInvoker2 = pool.GetCallInvoker("endpoint", options2, Config1, FakeAdapter);
var callInvoker1 = pool.GetCallInvoker("domain", "endpoint", options1, Config1, FakeAdapter);
var callInvoker2 = pool.GetCallInvoker("domain", "endpoint", options2, Config1, FakeAdapter);
Assert.Same(callInvoker1, callInvoker2);
}

Expand All @@ -45,8 +42,8 @@ public void DifferentEndpoint_DifferentCallInvoker()
{
var pool = new GcpCallInvokerPool(TestServiceMetadata.TestService);
var options = GrpcChannelOptions.Empty.WithPrimaryUserAgent("abc");
var callInvoker1 = pool.GetCallInvoker("endpoint1", options, Config1, FakeAdapter);
var callInvoker2 = pool.GetCallInvoker("endpoint2", options, Config1, FakeAdapter);
var callInvoker1 = pool.GetCallInvoker("domain", "endpoint1", options, Config1, FakeAdapter);
var callInvoker2 = pool.GetCallInvoker("domain", "endpoint2", options, Config1, FakeAdapter);
Assert.NotSame(callInvoker1, callInvoker2);
}

Expand All @@ -56,23 +53,33 @@ public void DifferentOptions_DifferentCallInvoker()
var pool = new GcpCallInvokerPool(TestServiceMetadata.TestService);
var options1 = GrpcChannelOptions.Empty.WithPrimaryUserAgent("abc");
var options2 = GrpcChannelOptions.Empty.WithPrimaryUserAgent("def");
var callInvoker1 = pool.GetCallInvoker("endpoint", options1, Config1, FakeAdapter);
var callInvoker2 = pool.GetCallInvoker("endpoint", options2, Config1, FakeAdapter);
var callInvoker1 = pool.GetCallInvoker("domain", "endpoint", options1, Config1, FakeAdapter);
var callInvoker2 = pool.GetCallInvoker("domain", "endpoint", options2, Config1, FakeAdapter);
Assert.NotSame(callInvoker1, callInvoker2);
var callInvoker3 = pool.GetCallInvoker("endpoint", options: null, Config1, FakeAdapter);
var callInvoker3 = pool.GetCallInvoker("domain", "endpoint", options: null, Config1, FakeAdapter);
Assert.NotSame(callInvoker1, callInvoker3);
}

[Fact]
public void DifferentUniverseDomain_DifferentCallInvoker()
{
var pool = new GcpCallInvokerPool(TestServiceMetadata.TestService);
var options = GrpcChannelOptions.Empty.WithPrimaryUserAgent("abc");
var callInvoker1 = pool.GetCallInvoker("domain1", "endpoint", options, Config1, FakeAdapter);
var callInvoker2 = pool.GetCallInvoker("domain2", "endpoint", options, Config1, FakeAdapter);
Assert.NotSame(callInvoker1, callInvoker2);
}

// TODO: equal/non-equal configs, different adapters.

[Fact]
public void ShutdownAsync_EmptiesPool()
{
var pool = new GcpCallInvokerPool(TestServiceMetadata.TestService);
var callInvoker1 = pool.GetCallInvoker("endpoint", options: null, Config1, FakeAdapter);
var callInvoker1 = pool.GetCallInvoker("domain", "endpoint", options: null, Config1, FakeAdapter);
// Note: *not* waiting for this to complete.
pool.ShutdownChannelsAsync();
var callInvoker2 = pool.GetCallInvoker("endpoint", options: null, Config1, FakeAdapter);
var callInvoker2 = pool.GetCallInvoker("domain", "endpoint", options: null, Config1, FakeAdapter);
Assert.NotSame(callInvoker1, callInvoker2);
}
}
Expand Down
2 changes: 2 additions & 0 deletions Google.Api.Gax.Grpc.IntegrationTests/TestServiceMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Google.Api.Gax.Grpc.IntegrationTests
{
internal static class TestServiceMetadata
{
internal const string DefaultUniverseDomain = "googleapis.com";

internal static ApiMetadata ApiMetadata { get; } =
new ApiMetadata("Google.Api.Gax.Grpc.IntegrationTests", new[] { TestServiceReflection.Descriptor });
internal static ServiceMetadata TestService { get; } =
Expand Down
Loading

0 comments on commit c12567d

Please sign in to comment.