Skip to content

Commit

Permalink
CR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gladysheva committed Feb 23, 2024
1 parent 8df5ca2 commit bb9dc99
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,37 +226,47 @@ public void UpdateAll_should_force_update()
[Test]
public void Should_delete_application_from_cache_if_app_and_env_nodes_were_deleted_when_observation_of_deleted_apps_is_disabled()
{
CreateEnvironmentNode("environment1");
CreateApplicationNode("environment1", "application1", new Dictionary<string, string> {{"key", "1/1"}});
const string environment = "environment1";
const string app = "application1";

var expectedTopology = ServiceTopology.Build(new Uri[0], new Dictionary<string, string> {{"key", "1/1"}});

using (var storage = GetApplicationsStorage(out var envStorage, observeNonExistentApplications: false))
{
var expectedTopology = ServiceTopology.Build(new Uri[0], new Dictionary<string, string> {{"key", "1/1"}});
ShouldReturnImmediately(
storage,
"environment1",
"application1",
expectedTopology);
for (var i = 0; i < 10; i++)
{
CreateEnvironmentNode(environment);
CreateApplicationNode(environment, app, new Dictionary<string, string> {{"key", "1/1"}});

DeleteApplicationNode("environment1", "application1");
DeleteEnvironmentNode("environment1");
envStorage.Get(environment).Should().Be(new EnvironmentInfo(environment, null, null));

envStorage.UpdateAll();
storage.UpdateAll();
ShouldReturnImmediately(
storage,
environment,
app,
expectedTopology);

storage.Contains("environment1", "application1").Should().BeFalse();
DeleteApplicationNode(environment, app);
DeleteEnvironmentNode(environment);

envStorage.UpdateAll();
storage.UpdateAll();

storage.Contains(environment, app).Should().BeFalse();
}
}
}

[Test]
public void Should_not_delete_application_from_cache_when_env_exists_and_observation_of_deleted_apps_is_disabled()
{
var environment = "environment1";
var app = "application1";
const string environment = "environment1";
const string app = "application1";

CreateEnvironmentNode(environment);
CreateApplicationNode(environment, app, new Dictionary<string, string> {{"key", "1/1"}});

using (var storage = GetApplicationsStorage(out var envStorage, observeNonExistentApplications: true))
using (var storage = GetApplicationsStorage(out var envStorage, observeNonExistentApplications: false))
{
var expectedTopology = ServiceTopology.Build(new Uri[0], new Dictionary<string, string> {{"key", "1/1"}});
ShouldReturnImmediately(
Expand All @@ -280,8 +290,8 @@ public void Should_not_delete_application_from_cache_when_env_exists_and_observa
[Test]
public void Should_not_delete_application_from_cache_when_observation_of_deleted_apps_is_disabled_and_client_disconnected()
{
var environment = "environment1";
var app = "application1";
const string environment = "environment1";
const string app = "application1";
CreateEnvironmentNode(environment);
CreateApplicationNode(environment, app, new Dictionary<string, string> {{"key", "1/1"}});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void Should_not_delete_environment_from_cache_if_node_was_deleted_when_ob
ShouldReturn(storage, "default", expectedInfo);
}
}

[Test]
public void Should_not_delete_environment_from_cache_when_observation_of_deleted_apps_is_disabled_and_client_disconnected()
{
Expand All @@ -195,19 +195,20 @@ public void Should_not_delete_environment_from_cache_when_observation_of_deleted
[Test]
public void Should_delete_environment_from_cache_if_node_was_deleted_when_observation_of_deleted_apps_is_disabled()
{
var expectedInfo = new EnvironmentInfo("default", "parent", null);

using (var storage = GetEnvironmentsStorage(observeNonExistentEnvironment: false))
{
CreateEnvironmentNode("default", "parent");
for (var i = 0; i < 10; i++)
{
CreateEnvironmentNode("default", "parent");

var expectedInfo = new EnvironmentInfo("default", "parent", null);
ShouldReturnImmediately(storage, "default", expectedInfo);
ShouldReturnImmediately(storage, "default", expectedInfo);

DeleteEnvironmentNode("default");
storage.UpdateAll();
storage.Contains("default").Should().BeFalse();

CreateEnvironmentNode("default", "parent");
ShouldReturnImmediately(storage, "default", expectedInfo);
DeleteEnvironmentNode("default");
storage.UpdateAll();
storage.Contains("default").Should().BeFalse();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ private void Update(string name)
{
if (!environments.TryGetValue(name, out var container))
{
log.Warn("Failed to update '{Environment}' environment: it does not exist in local cache.", name);
if (observeNonExistentEnvironments)
log.Warn("Failed to update '{Environment}' environment: it does not exist in local cache.", name);

return;
}

Expand All @@ -109,11 +111,8 @@ private void Update(string name, VersionedContainer<EnvironmentInfo> container)

if (environmentExists.Stat == null)
{
if (!observeNonExistentEnvironments)
{
environments.TryRemove(name, out _);
if (RemoveEnvironmentFromCacheIfNeeded(name))
return;
}

container.Clear();
}
Expand All @@ -125,11 +124,8 @@ private void Update(string name, VersionedContainer<EnvironmentInfo> container)
var environmentData = zooKeeperClient.GetData(new GetDataRequest(environmentPath) {Watcher = nodeWatcher});
if (environmentData.Status == ZooKeeperStatus.NodeNotFound)
{
if (!observeNonExistentEnvironments)
{
environments.TryRemove(name, out _);
if (RemoveEnvironmentFromCacheIfNeeded(name))
return;
}

container.Clear();
}
Expand All @@ -147,6 +143,15 @@ private void Update(string name, VersionedContainer<EnvironmentInfo> container)
}
}

private bool RemoveEnvironmentFromCacheIfNeeded(string name)
{
if (observeNonExistentEnvironments)
return false;

environments.TryRemove(name, out _);
return true;
}

private void OnNodeEvent(NodeChangedEventType type, string path)
{
if (isDisposed)
Expand Down

0 comments on commit bb9dc99

Please sign in to comment.