Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Discovery/src/Eureka/EurekaDiscoveryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public sealed partial class EurekaDiscoveryClient : IDiscoveryClient
private readonly Timer? _cacheRefreshTimer;
private readonly SemaphoreSlim _registerUnregisterAsyncLock = new(1);
private readonly SemaphoreSlim _registryFetchAsyncLock = new(1);
private volatile bool _hasRegistered;
private volatile bool _hasFirstHeartbeatCompleted;

private volatile ApplicationInfoCollection _remoteApps;
Expand Down Expand Up @@ -195,7 +196,7 @@ public async Task ShutdownAsync(CancellationToken cancellationToken)

try
{
if (!ReferenceEquals(_appInfoManager.Instance, InstanceInfo.Disabled))
if (_hasRegistered && !ReferenceEquals(_appInfoManager.Instance, InstanceInfo.Disabled))
{
await DeregisterAsync(cancellationToken);
}
Expand Down Expand Up @@ -268,6 +269,7 @@ internal async Task RegisterAsync(bool requireDirtyInstance, CancellationToken c
LogRegistrationSucceeded(snapshot.AppName, snapshot.InstanceId);

snapshot.IsDirty = false;
_hasRegistered = true;
}
}
finally
Expand Down
57 changes: 57 additions & 0 deletions src/Discovery/test/Eureka.Test/EurekaDiscoveryClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,63 @@ public async Task UnRegisterAsync_Succeeds_WhenOKStatusReturned()
handler.Mock.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task ShutdownAsync_Unregisters_WhenRegistered()
{
var appSettings = new Dictionary<string, string?>
{
["Eureka:Client:ShouldFetchRegistry"] = "false",
["Eureka:Client:ShouldRegisterWithEureka"] = "true",
["Eureka:Instance:AppName"] = "FOO",
["Eureka:Instance:InstanceId"] = "localhost:foo"
};

WebApplicationBuilder builder = TestWebApplicationBuilderFactory.Create();
builder.Configuration.AddInMemoryCollection(appSettings);
builder.Services.AddEurekaDiscoveryClient();

var handler = new DelegateToMockHttpClientHandler();
handler.Mock.Expect(HttpMethod.Post, "http://localhost:8761/eureka/apps/FOO").Respond(HttpStatusCode.OK);
handler.Mock.Expect(HttpMethod.Delete, "http://localhost:8761/eureka/apps/FOO/localhost%3Afoo").Respond(HttpStatusCode.OK);

await using WebApplication webApplication = builder.Build();
webApplication.Services.GetRequiredService<HttpClientHandlerFactory>().Using(handler);

var discoveryClient = webApplication.Services.GetRequiredService<EurekaDiscoveryClient>();

await discoveryClient.ShutdownAsync(TestContext.Current.CancellationToken);

handler.Mock.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task ShutdownAsync_DoesNotUnregister_WhenNotRegistered()
{
var appSettings = new Dictionary<string, string?>
{
["Eureka:Client:ShouldFetchRegistry"] = "false",
["Eureka:Client:ShouldRegisterWithEureka"] = "true",
["Eureka:Instance:AppName"] = "FOO",
["Eureka:Instance:InstanceId"] = "localhost:foo"
};

WebApplicationBuilder builder = TestWebApplicationBuilderFactory.Create();
builder.Configuration.AddInMemoryCollection(appSettings);
builder.Services.AddEurekaDiscoveryClient();

var handler = new DelegateToMockHttpClientHandler();
handler.Mock.Expect(HttpMethod.Post, "http://localhost:8761/eureka/apps/FOO").Respond(HttpStatusCode.NotFound);

await using WebApplication webApplication = builder.Build();
webApplication.Services.GetRequiredService<HttpClientHandlerFactory>().Using(handler);

var discoveryClient = webApplication.Services.GetRequiredService<EurekaDiscoveryClient>();

await discoveryClient.ShutdownAsync(TestContext.Current.CancellationToken);

handler.Mock.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task GetInstancesAsync_ReturnsExpected()
{
Expand Down
Loading