### Question:
ParamFlowSlot extends AbstractLinkedProcessorSlot<DefaultNode> { @Override public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable { if (!ParamFlowRuleManager.hasRules(resourceWrapper.getName())) { fireEntry(context, resourceWrapper, node, count, prioritized, args); return; } checkFlow(resourceWrapper, count, args); fireEntry(context, resourceWrapper, node, count, prioritized, args); }  @Override void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,
                      boolean prioritized, Object... args); @Override void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args);  }### Answer:
@Test public void testNegativeParamIdx() throws Throwable { String resourceName = "testNegativeParamIdx"; ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN); ParamFlowRule rule = new ParamFlowRule(resourceName) .setCount(1) .setParamIdx(-1); ParamFlowRuleManager.loadRules(Collections.singletonList(rule)); paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi"); assertEquals(2, rule.getParamIdx().longValue()); rule.setParamIdx(-1); ParamFlowRuleManager.loadRules(Collections.singletonList(rule)); paramFlowSlot.entry(null, resourceWrapper, null, 1, false, null); assertEquals(-1, rule.getParamIdx().intValue()); rule.setParamIdx(-100); ParamFlowRuleManager.loadRules(Collections.singletonList(rule)); paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi"); assertEquals(100, rule.getParamIdx().longValue()); rule.setParamIdx(0); ParamFlowRuleManager.loadRules(Collections.singletonList(rule)); paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi"); assertEquals(0, rule.getParamIdx().longValue()); }
@Test public void testEntryWhenParamFlowRuleNotExists() throws Throwable { String resourceName = "testEntryWhenParamFlowRuleNotExists"; ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN); paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc"); assertNull(ParameterMetricStorage.getParamMetric(resourceWrapper)); }
@Test public void testEntryWhenParamFlowExists() throws Throwable { String resourceName = "testEntryWhenParamFlowExists"; ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN); long argToGo = 1L; double count = 1; ParamFlowRule rule = new ParamFlowRule(resourceName) .setCount(count) .setBurstCount(0) .setParamIdx(0); ParamFlowRuleManager.loadRules(Collections.singletonList(rule)); ParameterMetric metric = mock(ParameterMetric.class); CacheMap<Object, AtomicLong> map = new ConcurrentLinkedHashMapWrapper<>(4000); CacheMap<Object, AtomicLong> map2 = new ConcurrentLinkedHashMapWrapper<>(4000); when(metric.getRuleTimeCounter(rule)).thenReturn(map); when(metric.getRuleTokenCounter(rule)).thenReturn(map2); map.put(argToGo, new AtomicLong(TimeUtil.currentTimeMillis())); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); paramFlowSlot.entry(null, resourceWrapper, null, 1, false, argToGo); try { paramFlowSlot.entry(null, resourceWrapper, null, 1, false, argToGo); } catch (ParamFlowException ex) { assertEquals(String.valueOf(argToGo), ex.getMessage()); assertEquals(resourceName, ex.getResourceName()); return; } fail("The second entry should be blocked"); }