Skip to content

Commit

Permalink
Test for env storage
Browse files Browse the repository at this point in the history
  • Loading branch information
gladysheva committed Feb 12, 2024
1 parent 42f7d55 commit 0afec84
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,45 @@ public void UpdateAll_should_force_update()
}
}

[Test]
public void Should_not_delete_environment_from_cache_if_node_was_deleted_when_observation_of_deleted_apps_is_enabled()
{
using (var storage = GetEnvironmentsStorage(observeNonExistentEnvironment: true))
{
CreateEnvironmentNode("default", "parent");

var expectedInfo = new EnvironmentInfo("default", "parent", null);
storage.Get("default").Should().BeEquivalentTo(expectedInfo);

DeleteEnvironmentNode("default");
storage.UpdateAll();
storage.EnvironmentsInStorage.Should().Be(1);
storage.Get("default").Should().BeNull();

CreateEnvironmentNode("default", "parent");
storage.Get("default").Should().BeEquivalentTo(expectedInfo);
}
}

[Test]
public void Should_delete_environment_from_cache_if_node_was_deleted_when_observation_of_deleted_apps_is_disabled()
{
using (var storage = GetEnvironmentsStorage(observeNonExistentEnvironment: false))
{
CreateEnvironmentNode("default", "parent");

var expectedInfo = new EnvironmentInfo("default", "parent", null);
storage.Get("default").Should().BeEquivalentTo(expectedInfo);

DeleteEnvironmentNode("default");
storage.UpdateAll();
storage.EnvironmentsInStorage.Should().Be(0);

CreateEnvironmentNode("default", "parent");
storage.Get("default").Should().BeEquivalentTo(expectedInfo);
}
}

private static void ShouldReturn(EnvironmentsStorage storage, string name, EnvironmentInfo info)
{
Action assertion = () => { ShouldReturnImmediately(storage, name, info); };
Expand All @@ -166,9 +205,9 @@ private static void ShouldReturnImmediately(EnvironmentsStorage storage, string
storage.Get(name).Should().BeEquivalentTo(info);
}

private EnvironmentsStorage GetEnvironmentsStorage()
private EnvironmentsStorage GetEnvironmentsStorage(bool observeNonExistentEnvironment = true)
{
return new EnvironmentsStorage(ZooKeeperClient, PathHelper, EventsQueue, true, Log);
return new EnvironmentsStorage(ZooKeeperClient, PathHelper, EventsQueue, observeNonExistentEnvironment, Log);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using Vostok.Commons.Threading;
Expand All @@ -21,26 +20,28 @@ private readonly ConcurrentDictionary<string, Lazy<VersionedContainer<Environmen
private readonly IZooKeeperClient zooKeeperClient;
private readonly ServiceDiscoveryPathHelper pathHelper;
private readonly ActionsQueue eventsHandler;
private readonly bool observeNonExistentApplication;
private readonly bool observeNonExistentEnvironments;
private readonly ILog log;
private readonly AdHocNodeWatcher nodeWatcher;
private readonly AdHocNodeWatcher existsWatcher;
private readonly AtomicBoolean isDisposed = new AtomicBoolean(false);

public int EnvironmentsInStorage => environments.Count;

public EnvironmentsStorage(
IZooKeeperClient zooKeeperClient,
ServiceDiscoveryPathHelper pathHelper,
ActionsQueue eventsHandler,
bool observeNonExistentApplication,
bool observeNonExistentEnvironments,
ILog log)
{
this.zooKeeperClient = zooKeeperClient;
this.pathHelper = pathHelper;
this.eventsHandler = eventsHandler;
this.observeNonExistentApplication = observeNonExistentApplication;
this.observeNonExistentEnvironments = observeNonExistentEnvironments;
this.log = log;
nodeWatcher = new AdHocNodeWatcher(OnNodeEvent);
existsWatcher = this.observeNonExistentApplication ? nodeWatcher : null;
existsWatcher = this.observeNonExistentEnvironments ? nodeWatcher : null;
}

public EnvironmentInfo Get(string name)
Expand Down Expand Up @@ -108,7 +109,7 @@ private void Update(string name, VersionedContainer<EnvironmentInfo> container)

if (environmentExists.Stat == null)
{
if (!observeNonExistentApplication)
if (!observeNonExistentEnvironments)
{
environments.TryRemove(name, out _);
return;
Expand Down

0 comments on commit 0afec84

Please sign in to comment.