You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/11/01 21:34:17 UTC

[GitHub] [nifi] thenatog commented on a diff in pull request #6606: NIFI-10738 - Updated SNMP tests to retry when BindException occurs.

thenatog commented on code in PR #6606:
URL: https://github.com/apache/nifi/pull/6606#discussion_r1010923342


##########
nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/factory/core/SNMPSocketSupport.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.nifi.snmp.factory.core;
+
+import org.apache.nifi.remote.io.socket.NetworkUtils;
+import org.apache.nifi.snmp.configuration.SNMPConfiguration;
+import org.snmp4j.Snmp;
+import org.snmp4j.Target;
+import org.snmp4j.security.SecurityLevel;
+import java.net.BindException;
+import java.util.function.Function;
+
+import static org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.SECURITY_NAME;
+import static org.apache.nifi.snmp.helper.configurations.SNMPConfigurationFactory.LOCALHOST;
+import static org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PASSPHRASE;
+import static org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PROTOCOL;
+import static org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PASSPHRASE;
+import static org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PROTOCOL;
+
+public class SNMPSocketSupport {
+
+    protected static final int RETRIES = 3;
+
+    protected SNMPConfiguration getSnmpConfiguration(int managerPort, String targetPort) {
+        return new SNMPConfiguration.Builder()
+                .setRetries(RETRIES)
+                .setManagerPort(managerPort)
+                .setTargetHost(LOCALHOST)
+                .setTargetPort(targetPort)
+                .setSecurityLevel(SecurityLevel.authPriv.name())
+                .setSecurityName(SECURITY_NAME)
+                .setAuthProtocol(AUTH_PROTOCOL)
+                .setAuthPassphrase(AUTH_PASSPHRASE)
+                .setPrivacyProtocol(PRIV_PROTOCOL)
+                .setPrivacyPassphrase(PRIV_PASSPHRASE)
+                .build();
+    }
+
+    protected Snmp createSnmpManagerInstance(final Function<SNMPConfiguration, Snmp> runnable, final int retries) {
+        int attempts = 0;
+        while (attempts < retries) {
+            try {
+                return runnable.apply(getSnmpConfiguration(NetworkUtils.getAvailableUdpPort(), String.valueOf(NetworkUtils.getAvailableUdpPort())));
+            } catch (Exception e) {
+                if (e instanceof BindException) {
+                    attempts++;
+                    if (attempts == retries) {
+                        throw e;
+                    }
+                }
+            }
+        }
+        return null;

Review Comment:
   If I understood this right - I updated the method to throw an IllegalStateException instead of returning null



-- 
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: issues-unsubscribe@nifi.apache.org

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