From 88cc1f5da8ded831b15f4970e8877494d449a471 Mon Sep 17 00:00:00 2001
From: Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com>
Date: Mon, 6 Jan 2020 12:02:46 +0100
Subject: Prune hierarchy of stale zones on read

---
 .../mimuw/cloudatlas/agent/modules/StanikTest.java | 29 ++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

(limited to 'src/test/java/pl/edu/mimuw/cloudatlas/agent/modules')

diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java
index 1ca8608..f3ea0b0 100644
--- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java
+++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java
@@ -35,10 +35,11 @@ public class StanikTest {
     private Stanik stanik;
     private MockExecutor executor;
     private ValueTime testTime;
+    private static final long freshnessPeriod = 1000;
 
     @Before
     public void setupLocals() {
-        stanik = new Stanik();
+        stanik = new Stanik(freshnessPeriod);
         executor = new MockExecutor(stanik);
         testTime = ValueUtils.currentTime();
     }
@@ -157,7 +158,7 @@ public class StanikTest {
     public void dontApplyWithStaleTimestamp() throws Exception {
         AttributesMap attributes = new AttributesMap();
         attributes.add("foo", new ValueInt(1337l));
-        attributes.add("timestamp", (ValueTime) testTime.subtract(new ValueDuration(61 * 1000l)));
+        attributes.add("timestamp", (ValueTime) testTime.subtract(new ValueDuration(freshnessPeriod + 100)));
         attributes.add("name", new ValueString("new"));
         UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes);
         stanik.handleTyped(message);
@@ -165,6 +166,30 @@ public class StanikTest {
         assertFalse(stanik.getHierarchy().descendantExists(new PathName("/new")));
     }
 
+    @Test
+    public void zoneRemovedAfterFreshnessPeriod() throws Exception {
+        AttributesMap attributes = new AttributesMap();
+        attributes.add("foo", new ValueInt(1337l));
+        attributes.add("timestamp", testTime);
+        attributes.add("name", new ValueString("new"));
+        UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes);
+        stanik.handleTyped(message);
+        Thread.sleep(freshnessPeriod + 100);
+
+        AttributesMap attributes2 = new AttributesMap();
+        attributes2.add("timestamp", ValueUtils.currentTime());
+        UpdateAttributesMessage message2 = new UpdateAttributesMessage("test_msg", 0, "/", attributes2);
+        stanik.handleTyped(message2);
+
+        GetStateMessage getStateMessage = new GetStateMessage("", 0, ModuleType.TEST, 0);
+        stanik.handleTyped(getStateMessage);
+
+        StateMessage newReceivedMessage = (StateMessage) executor.messagesToPass.poll();
+        assertNotNull(newReceivedMessage);
+        assertFalse(newReceivedMessage.getZMI().descendantExists(new PathName("/new")));
+        assertFalse(stanik.getHierarchy().descendantExists(new PathName("/new")));
+    }
+
     @Test
     public void addQuery() throws Exception {
         HashMap<Attribute, Entry<ValueQuery, ValueTime>> queries = new HashMap<Attribute, Entry<ValueQuery, ValueTime>>();
-- 
cgit v1.2.3