jdeppe-pivotal commented on a change in pull request #7408:
URL: https://github.com/apache/geode/pull/7408#discussion_r825629126



##########
File path: 
geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/eventing/EventDistributorTest.java
##########
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache 
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the 
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software 
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+ * or implied. See the License for the specific language governing permissions 
and limitations under
+ * the License.
+ */
+
+package org.apache.geode.redis.internal.eventing;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+import java.util.stream.Collectors;
+
+import org.junit.Test;
+
+import org.apache.geode.redis.ConcurrentLoopingThreads;
+import org.apache.geode.redis.internal.commands.RedisCommandType;
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.test.awaitility.GeodeAwaitility;
+
+public class EventDistributorTest {
+
+  public static class TestEventListener implements EventListener {
+    private final List<RedisKey> keys;
+    private int fired = 0;
+    private final long timeout;
+
+    public TestEventListener(RedisKey... keys) {
+      this(0, keys);
+    }
+
+    public TestEventListener(long timeout, RedisKey... keys) {
+      this.keys = Arrays.stream(keys).collect(Collectors.toList());
+      this.timeout = timeout;
+    }
+
+    public int getFired() {
+      return fired;
+    }
+
+    @Override
+    public EventResponse process(RedisCommandType commandType, RedisKey key) {
+      fired += 1;
+      return EventResponse.REMOVE_AND_STOP;
+    }
+
+    @Override
+    public List<RedisKey> keys() {
+      return keys;
+    }
+
+    @Override
+    public void resubmitCommand() {}
+
+    @Override
+    public long getTimeout() {
+      return timeout;
+    }
+
+    @Override
+    public void timeout() {}
+
+    @Override
+    public void setCleanupTask(Runnable r) {}
+
+    @Override
+    public void cleanup() {}
+  }
+
+  @Test
+  public void firingEventRemovesListener() {
+    RedisKey keyA = new RedisKey("a".getBytes());
+    RedisKey keyB = new RedisKey("b".getBytes());
+    EventDistributor distributor = new EventDistributor();
+    TestEventListener listener = new TestEventListener(keyA, keyB);
+    distributor.registerListener(listener);
+
+    distributor.fireEvent(null, keyA);
+    assertThat(listener.getFired()).isEqualTo(1);
+    assertThat(distributor.size()).isEqualTo(0);
+  }
+
+  @Test
+  public void firingEventRemovesFirstListener_whenMultipleExist() {
+    RedisKey keyA = new RedisKey("a".getBytes());
+    RedisKey keyB = new RedisKey("b".getBytes());
+    EventDistributor distributor = new EventDistributor();
+    TestEventListener listener1 = new TestEventListener(keyA, keyB);
+    TestEventListener listener2 = new TestEventListener(keyA, keyB);
+    distributor.registerListener(listener1);
+    distributor.registerListener(listener2);
+
+    assertThat(distributor.size()).isEqualTo(4);
+
+    distributor.fireEvent(null, keyA);
+    assertThat(listener1.getFired()).isEqualTo(1);
+    assertThat(listener2.getFired()).isEqualTo(0);
+    assertThat(distributor.size()).isEqualTo(2);
+
+    distributor.fireEvent(null, keyA);
+    assertThat(listener1.getFired()).isEqualTo(1);
+    assertThat(listener2.getFired()).isEqualTo(1);
+    assertThat(distributor.size()).isEqualTo(0);
+  }
+
+  @Test
+  public void listenerIsRemovedAfterTimeout() {
+    RedisKey keyA = new RedisKey("a".getBytes());
+    EventDistributor distributor = new EventDistributor();
+    TestEventListener listener1 = new TestEventListener(1, keyA);

Review comment:
       So I removed the timeout value completely. Turned out to be unnecessary 
because of other refactoring.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@geode.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to