GJL commented on a change in pull request #7986: [FLINK-10517][rest] Add 
stability test
URL: https://github.com/apache/flink/pull/7986#discussion_r274038147
 
 

 ##########
 File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/rest/compatibility/CompatibilityRoutine.java
 ##########
 @@ -0,0 +1,114 @@
+/*
+ * 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.flink.runtime.rest.compatibility;
+
+import org.apache.flink.runtime.rest.messages.MessageHeaders;
+
+import org.junit.Assert;
+
+import java.util.Optional;
+import java.util.function.BiConsumer;
+import java.util.function.Function;
+
+import static 
org.apache.flink.runtime.rest.compatibility.Compatibility.COMPATIBLE;
+import static 
org.apache.flink.runtime.rest.compatibility.Compatibility.IDENTICAL;
+import static 
org.apache.flink.runtime.rest.compatibility.Compatibility.INCOMPATIBLE;
+
+/**
+ * Routine for checking the compatibility of a {@link MessageHeaders} pair.
+ *
+ * <p>The 'extractor' {@link Function} generates a 'container', a 
jackson-compatible object containing the data that
+ * the routine bases it's compatibility-evaluation on.
+ * The 'assertion' {@link BiConsumer} accepts a pair of containers and asserts 
the compatibility. Incompatibilities are
+ * signaled by throwing an {@link AssertionError}. This implies that the 
method body will typically contain jUnit
+ * assertions.
+ */
+final class CompatibilityRoutine<C> {
+
+       private final String key;
+       private final Class<C> containerClass;
+       private final Function<MessageHeaders<?, ?, ?>, C> extractor;
+       private final BiConsumer<C, C> assertion;
+
+       CompatibilityRoutine(
+                       final String key,
+                       final Class<C> containerClass,
+                       final Function<MessageHeaders<?, ?, ?>, C> extractor,
+                       final BiConsumer<C, C> assertion) {
+               this.key = key;
+               this.containerClass = containerClass;
+               this.extractor = extractor;
+               this.assertion = assertion;
+       }
+
+       String getKey() {
+               return key;
+       }
+
+       Class<C> getContainerClass() {
+               return containerClass;
+       }
+
+       C getContainer(final MessageHeaders<?, ?, ?> header) {
+               final C container = extractor.apply(header);
+               Assert.assertNotNull("Implementation error: Extractor returned 
null.", container);
+               return container;
+       }
+
+       public CompatibilityCheckResult checkCompatibility(final Optional<C> 
old, final Optional<C> cur) {
+               if (!old.isPresent() && !cur.isPresent()) {
+                       Assert.fail(String.format(
+                               "Implementation error: Compatibility check 
container for routine %s for both old and new version is null.", key));
+               }
+               if (!old.isPresent()) {
+                       // allow addition of new compatibility routines
+                       return new CompatibilityCheckResult(COMPATIBLE);
+               }
+               if (!cur.isPresent()) {
+                       // forbid removal of compatibility routines
+                       return new CompatibilityCheckResult(
+                               new AssertionError(String.format(
+                                       "Compatibility check container for 
routine %s not found in current version.", key)));
+               }
+
+               Compatibility backwardCompatibility;
+               AssertionError backwardIncompatibilityCause = null;
+               try {
+                       assertion.accept(old.get(), cur.get());
+                       backwardCompatibility = COMPATIBLE;
+               } catch (final Exception | AssertionError e) {
+                       backwardCompatibility = INCOMPATIBLE;
+                       backwardIncompatibilityCause = new AssertionError(key + 
": " + e.getMessage());
+               }
+
+               Compatibility forwardCompatibility;
+               try {
+                       assertion.accept(cur.get(), old.get());
+                       forwardCompatibility = COMPATIBLE;
+               } catch (final Exception | AssertionError e) {
 
 Review comment:
   If the logic in the `assertion` `BiConsumer` throws a runtime exception, I 
would forward it here and fail immediately (otherwise errors in the application 
logic will be hidden). Also, instead of `BiConsumer`, you could use 
`BiFunction<C, C, AssertionResult>` to make the logic not based on exceptions.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to