Github user srdo commented on a diff in the pull request:
https://github.com/apache/storm/pull/2218#discussion_r131123215
--- Diff:
storm-client/test/jvm/org/apache/storm/windowing/persistence/WindowStateTest.java
---
@@ -0,0 +1,247 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.storm.windowing.persistence;
+
+import org.apache.storm.state.KeyValueState;
+import org.apache.storm.tuple.Tuple;
+import org.apache.storm.windowing.Event;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.stubbing.Answer;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+
+import static org.mockito.AdditionalAnswers.returnsArgAt;
+
+/**
+ * Unit tests for {@link WindowState}
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class WindowStateTest {
+
+ @Mock
+ private KeyValueState<Long, WindowState.WindowPartition<Integer>>
windowState;
+ @Mock
+ private KeyValueState<String, Deque<Long>> partitionIdsState;
+ @Mock
+ private KeyValueState<String, Optional<?>> systemState;
+ @Mock
+ private Supplier<Map<String, Optional<?>>> supplier;
+ @Captor
+ private ArgumentCaptor<Long> longCaptor;
+ @Captor
+ private ArgumentCaptor<WindowState.WindowPartition<Integer>>
windowValuesCaptor;
+
+ private static final int MAX_EVENTS_PER_PARTITION = 1000;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void testAdd() throws Exception {
+ Mockito.when(partitionIdsState.get(Mockito.any(),
Mockito.any())).then(returnsArgAt(1));
+ Mockito.when(windowState.get(Mockito.any(),
Mockito.any())).then(returnsArgAt(1));
+
+ WindowState<Integer> ws = getWindowState(10 *
MAX_EVENTS_PER_PARTITION);
+
+ long partitions = 15;
+ long numEvents = partitions * MAX_EVENTS_PER_PARTITION;
+ for (int i = 0; i < numEvents; i++) {
+ ws.add(getEvent(i));
+ }
+ // 5 events evicted to window state
+ Mockito.verify(windowState,
Mockito.times(5)).put(longCaptor.capture(), windowValuesCaptor.capture());
+ Assert.assertEquals(5, longCaptor.getAllValues().size());
+ // each evicted partition has MAX_EVENTS_PER_PARTITION
+ windowValuesCaptor.getAllValues().forEach(wp -> {
+ Assert.assertEquals(MAX_EVENTS_PER_PARTITION, wp.size());
+ });
+ // last partition is not evicted
+ Assert.assertFalse(longCaptor.getAllValues().contains(partitions -
1));
+ }
+
+ @Test
+ public void testIterator() throws Exception {
+ Map<Long, WindowState.WindowPartition<Event<Tuple>>> partitionMap
= new HashMap<>();
+ Mockito.when(partitionIdsState.get(Mockito.any(),
Mockito.any())).then(returnsArgAt(1));
+ Mockito.when(windowState.get(Mockito.any(),
Mockito.any())).then(new Answer<Object>() {
+ @Override
+ public Object answer(InvocationOnMock invocation) throws
Throwable {
+ Object[] args = invocation.getArguments();
+ WindowState.WindowPartition<Event<Tuple>> evicted =
partitionMap.get(args[0]);
+ return evicted != null ? evicted : args[1];
+ }
+ });
+
+ Mockito.doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) throws
Throwable {
+ Object[] args = invocation.getArguments();
+ partitionMap.put((long)args[0],
(WindowState.WindowPartition<Event<Tuple>>)args[1]);
+ return null;
+ }
+ }).when(windowState).put(Mockito.any(), Mockito.any());
+
+ Mockito.doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) throws
Throwable {
+ Object[] args = invocation.getArguments();
+ partitionMap.remove(args[0]);
+ return null;
+ }
+ }).when(windowState).delete(Mockito.anyLong());
+
+ Mockito.when(supplier.get()).thenReturn(Collections.emptyMap());
+
+ WindowState<Integer> ws = getWindowState(10 *
MAX_EVENTS_PER_PARTITION);
+
+ long partitions = 15;
+
+ long numEvents = partitions * MAX_EVENTS_PER_PARTITION;
+ List<Event<Integer>> expected = new ArrayList<>();
+ for (int i = 0; i < numEvents; i++) {
+ Event<Integer> event = getEvent(i);
+ expected.add(event);
+ ws.add(event);
+ }
+
+ Assert.assertEquals(5, partitionMap.size());
+ Iterator<Event<Integer>> it = ws.iterator();
+ List<Event<Integer>> actual = new ArrayList<>();
+ it.forEachRemaining(actual::add);
+ Assert.assertEquals(expected, actual);
+
+ // iterate again
+ it = ws.iterator();
+ actual.clear();
+ it.forEachRemaining(actual::add);
+ Assert.assertEquals(expected, actual);
+
+ // remove
+ it = ws.iterator();
+ while (it.hasNext()) {
+ it.next();
+ it.remove();
+ }
+
+ it = ws.iterator();
+ actual.clear();
+ it.forEachRemaining(actual::add);
+ Assert.assertEquals(Collections.emptyList(), actual);
+ }
+
+ @Test
+ public void testIteratorPartitionNotEvicted() throws Exception {
+ Map<Long, WindowState.WindowPartition<Event<Tuple>>> partitionMap
= new HashMap<>();
+ Mockito.when(partitionIdsState.get(Mockito.any(),
Mockito.any())).then(returnsArgAt(1));
+ Mockito.when(windowState.get(Mockito.any(),
Mockito.any())).then(new Answer<Object>() {
+ @Override
+ public Object answer(InvocationOnMock invocation) throws
Throwable {
+ Object[] args = invocation.getArguments();
+ WindowState.WindowPartition<Event<Tuple>> evicted =
partitionMap.get(args[0]);
+ return evicted != null ? evicted : args[1];
+ }
+ });
+
+ Mockito.doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) throws
Throwable {
+ Object[] args = invocation.getArguments();
+ partitionMap.put((long)args[0],
(WindowState.WindowPartition<Event<Tuple>>)args[1]);
+ return null;
+ }
+ }).when(windowState).put(Mockito.any(), Mockito.any());
+
+ Mockito.when(supplier.get()).thenReturn(Collections.emptyMap());
+
+ WindowState<Integer> ws = getWindowState(10 *
MAX_EVENTS_PER_PARTITION);
+
+ long partitions = 10;
+
+ long numEvents = partitions * MAX_EVENTS_PER_PARTITION;
+ List<Event<Integer>> expected = new ArrayList<>();
+ for (int i = 0; i < numEvents; i++) {
+ Event<Integer> event = getEvent(i);
+ expected.add(event);
+ ws.add(event);
+ }
+
+ Iterator<Event<Integer>> it = ws.iterator();
+ for(int i=0; i<9500; i++) {
+ it.next();
+ }
+
+ for (int i = 0; i < numEvents; i++) {
+ Event<Integer> event = getEvent(i);
+ expected.add(event);
+ ws.add(event);
+ }
+
+ // 9 th partition should not have been evicted
--- End diff --
Isn't this the 10th?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---