Skip to content

Flink 1.17: Create JUnit5 version of TestFlinkScan - #9185

Merged
nastra merged 9 commits into
apache:mainfrom
cgpoh:create-junit5-testflinkscan
Jan 5, 2024
Merged

nastra merged 9 commits into
apache:mainfrom
cgpoh:create-junit5-testflinkscan

Conversation

@cgpoh

@cgpoh cgpoh commented Nov 30, 2023

Copy link
Copy Markdown
Contributor

Fixes #9077

import org.junit.jupiter.api.io.TempDir;

public class HadoopCatResource implements BeforeEachCallback, AfterEachCallback {
protected final @TempDir Path temporaryFolder;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the annotation here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch! Will remove the annotation.

protected String warehouse;
protected TableLoader tableLoader;

public HadoopCatResource(@TempDir Path temporaryFolder, String database, String tableName) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the annotation here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch! Will remove the annotation.


Assert.assertTrue(
"expected and actual should be both null or not null", expected != null && actual != null);
Assertions.assertThat(expected != null && actual != null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe static import Assertions.assertThat?

case FIXED:
Assertions.assertThat(expected).as("Should expect byte[]").isInstanceOf(byte[].class);
Assert.assertArrayEquals("binary should be equal", (byte[]) expected, (byte[]) actual);
Assertions.assertThat((byte[]) actual).as("binary should be equal").isEqualTo(expected);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do chaining?

Assertions.assertThat(expected)
  .as("Should expect byte[]")
  .isInstanceOf(byte[].class)
  .isEqualTo(actual);

super(fileFormat);
}

public class TestFlinkInputFormat extends TestFlinkSrc {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How many tests inherit from the TestFlinkSource?
Would it be too much to review in one run? Or would it be easier to move all of them at once.
If we move all of them at once, we would not need TestFlinkSrc, and we can keep, and migrate TestFlinkSource instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see the following classes impacted by TestFlinkSource:

  • TestFlinkScanSql

In TestFlinkScan, there's HadoopCatalogResource which is used by quite a number of unit tests. I had renamed HadoopCatalogResource to HadoopCatResource in this commit and migrate the impacted classes listed above.

import org.apache.iceberg.flink.TestFixtures;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;

public abstract class TestFlinkSrc extends TestScan {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for TestFlinkScan

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see the following classes impacted by TestFlinkScan:

  • TestIcebergSourceBounded
  • TestIcebergSourceBoundedSql

In TestFlinkScan, there's HadoopCatalogResource which is used by quite a number of unit tests. I had renamed HadoopCatalogResource to HadoopCatResource in this commit and migrate the impacted classes listed above.

import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class HadoopCatResource implements BeforeEachCallback, AfterEachCallback {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't HadoopCatalogExtension a substitute for this?

@nastra nastra Dec 5, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I think we can just use HadoopCatalogExtension, thus we can remove HadoopCatResource

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I didn’t know about HadoopCatalogExtension, will use that instead

this.fileFormat = FileFormat.fromString(fileFormat);
public static Stream<Arguments> parameters() {
return Stream.of(
Arguments.of(FileFormat.fromString("avro")),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be FileFormat.PARQUET, FileFormat.ORC, FileFormat.AVRO?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please join the discussion on #9161 to decide a general way to implement parametrized tests

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pvary !

import org.assertj.core.api.Assertions;

/** Helper for appending {@link DataFile} to a table or appending {@link Record}s to a table. */
public class GenAppenderHelper {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more like a question...
If I understand correctly GenericAppenderHelper is only used by Flink tests.
If we do not see it used in other places, we might want to consider it moving out from here, to the Flink specific code, and maybe with a package switch we can keep the class name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me see whether we can do that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pvary , GenericAppenderHelper is also used by iceberg-mr package. If I migrate GenericAppenderHelper, I have to migrate the test in iceberg-mr package too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that is the case, then we should keep this here. Thanks

Assert.assertTrue(
"expected Record and actual RowData should be both null or not null",
expectedRecord != null && actualRowData != null);
assertThat(expectedRecord != null && actualRowData != null)

@nastra nastra Dec 5, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be assertThat(expectedRecord).isNotNull() and then assertThat(actualRowData).isNotNull().
We want to avoid using isTrue/isFalse as much as possible, because it's more difficult to debug when the assertion fails

"expected and actual should be both null or not null", expected != null && actual != null);
assertThat(expected != null && actual != null)
.as("expected and actual should be both null or not null")
.isTrue();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

List<Types.NestedField> fields = schema.asStruct().fields();
Assert.assertEquals(fields.size(), record.getSchema().getFields().size());
Assert.assertEquals(fields.size(), row.getArity());
assertThat(fields.size()).isEqualTo(record.getSchema().getFields().size());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertThat(xyz).hasSameSizeAs(...)

Assert.assertEquals(fields.size(), record.getSchema().getFields().size());
Assert.assertEquals(fields.size(), row.getArity());
assertThat(fields.size()).isEqualTo(record.getSchema().getFields().size());
assertThat(fields.size()).isEqualTo(row.getArity());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assertThat(fields.size()).isEqualTo(row.getArity());
assertThat(fields).hasSize(row.getArity());

"expected and actual should be both null or not null", expected != null && actual != null);
assertThat(expected != null && actual != null)
.as("expected and actual should be both null or not null")
.isTrue();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

"Deleted files count must match", expected.deletedFilesCount(), actual.deletedFilesCount());
Assert.assertEquals(
"Deleted rows count must match", expected.deletedRowsCount(), actual.deletedRowsCount());
assertThat(expected != null && actual != null).as("Should not be null.").isTrue();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

"PartitionFieldSummary size does not match",
expectedSummaries.size(),
actualSummaries.size());
assertThat(actualSummaries.size())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use hasSameSizeAs()

Assert.assertEquals("Path", expected.path(), actual.path());
Assert.assertEquals("Format", expected.format(), actual.format());
Assert.assertEquals("Partition size", expected.partition().size(), actual.partition().size());
assertThat(expected != null && actual != null).as("Shouldn't be null.").isTrue();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@nastra nastra changed the title chore: junit 5 migration for TestFlinkScan Flink: Create JUnit5 version of TestFlinkScan Dec 5, 2023
…use hasSameSizeAs to check collection size

FileAppender<Record> appender = appenderFactory.newAppender(Files.localOutput(file), format);
try (FileAppender<Record> fileAppender = appender) {
fileAppender.addAll(records);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we log if we have exceptions?

actualSummaries.size());
assertThat(actualSummaries)
.as("PartitionFieldSummary size does not match")
.hasSameSizeAs(expectedSummaries.size());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this is the right way to use hasSameSizeAs assertions.
the parameter of

.hasSameSizeAs

shouldn't be a number, but instead another collection.
Please make changes accordingly to the rest of the codebase

break;
case BINARY:
Assertions.assertThat(expected)
assertThat(expected)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the actual/expected is the wrong way here as well. Can you please check all the other places in this file to make sure that we're using `assertThat(actual)...isEqualTo(expected)

import org.assertj.core.api.Assertions;

/** Helper for appending {@link DataFile} to a table or appending {@link Record}s to a table. */
public class GenAppenderHelper {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this class. We can just update GenericAppenderHelper to work with JUnit4's TemporaryFolder and JUnit5's Path by applying this patch:

diff --git a/data/src/test/java/org/apache/iceberg/data/GenericAppenderHelper.java b/data/src/test/java/org/apache/iceberg/data/GenericAppenderHelper.java
index beccc8bb7..8521004ca 100644
--- a/data/src/test/java/org/apache/iceberg/data/GenericAppenderHelper.java
+++ b/data/src/test/java/org/apache/iceberg/data/GenericAppenderHelper.java
@@ -20,6 +20,7 @@ package org.apache.iceberg.data;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Path;
 import java.util.List;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.iceberg.AppendFiles;
@@ -44,13 +45,24 @@ public class GenericAppenderHelper {
   private final Table table;
   private final FileFormat fileFormat;
   private final TemporaryFolder tmp;
+  private final Path temp;
   private final Configuration conf;
 
+  @Deprecated
   public GenericAppenderHelper(
       Table table, FileFormat fileFormat, TemporaryFolder tmp, Configuration conf) {
     this.table = table;
     this.fileFormat = fileFormat;
     this.tmp = tmp;
+    this.temp = null;
+    this.conf = conf;
+  }
+
+  public GenericAppenderHelper(Table table, FileFormat fileFormat, Path temp, Configuration conf) {
+    this.table = table;
+    this.fileFormat = fileFormat;
+    this.tmp = null;
+    this.temp = temp;
     this.conf = conf;
   }
 
@@ -94,14 +106,14 @@ public class GenericAppenderHelper {
 
   public DataFile writeFile(List<Record> records) throws IOException {
     Preconditions.checkNotNull(table, "table not set");
-    File file = tmp.newFile();
+    File file = null != tmp ? tmp.newFile() : File.createTempFile("junit", null, temp.toFile());
     Assert.assertTrue(file.delete());
     return appendToLocalFile(table, file, fileFormat, null, records, conf);
   }
 
   public DataFile writeFile(StructLike partition, List<Record> records) throws IOException {
     Preconditions.checkNotNull(table, "table not set");
-    File file = tmp.newFile();
+    File file = null != tmp ? tmp.newFile() : File.createTempFile("junit", null, temp.toFile());
     Assert.assertTrue(file.delete());
     return appendToLocalFile(table, file, fileFormat, partition, records, conf);
   }

that way we don't have to maintain two files.

@cgpoh can you please remove GenAppenderHelper and apply the Assert -> AssertJ conversions to GenericAppenderHelper?

@TestTemplate
public void testReadPartitionColumn() throws Exception {
Assume.assumeTrue("Temporary skip ORC", FileFormat.ORC != fileFormat);
Assumptions.assumeTrue(FileFormat.ORC != fileFormat, "Temporary skip ORC");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use Assumptions from AssertJ: `assumeThat(fileFormat).as("Temporary skip ORC").isNotEqualTo(FileFormat.ORC)

public class TestFlinkInputFormat extends TestFlinkSource {

public TestFlinkInputFormat(String fileFormat) {
public TestFlinkInputFormat(FileFormat fileFormat) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of the constructors in the subclasses can be removed

public final HadoopCatalogResource catalogResource =
new HadoopCatalogResource(TEMPORARY_FOLDER, TestFixtures.DATABASE, TestFixtures.TABLE);
@RegisterExtension
public static final HadoopCatalogExtension catalogExtension =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static final HadoopCatalogExtension catalogExtension =
protected static final HadoopCatalogExtension catalogExtension =


TestFlinkScan(String fileFormat) {
this.fileFormat = FileFormat.fromString(fileFormat);
TestFlinkScan(FileFormat fileFormat) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the constructor needs to be removed because the file format will be set by the parameterized environment

new HadoopCatalogExtension(TestFixtures.DATABASE, TestFixtures.TABLE);

// parametrized variables
protected final FileFormat fileFormat;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected final FileFormat fileFormat;
@Parameter protected FileFormat fileFormat;

private volatile TableEnvironment tEnv;

public TestIcebergSourceBoundedSql(String fileFormat) {
public TestIcebergSourceBoundedSql(FileFormat fileFormat) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor can be removed

@nastra
nastra force-pushed the create-junit5-testflinkscan branch from ecd6a56 to 87c58bd Compare December 22, 2023 11:20
@nastra
nastra force-pushed the create-junit5-testflinkscan branch from 87c58bd to e10733d Compare December 22, 2023 11:24
@nastra

nastra commented Dec 22, 2023 •

Copy link
Copy Markdown
Contributor

@cgpoh I took the liberty to address the remaining items so that we can get this in. @pvary could you do a final review on this one please?
I think we would also want to apply this to Flink 1.18

@cgpoh

cgpoh commented Dec 28, 2023

Copy link
Copy Markdown
Contributor Author

@cgpoh I took the liberty to address the remaining items so that we can get this in. @pvary could you do a final review on this one please? I think we would also want to apply this to Flink 1.18

Thanks @nastra for addressing the remaining items. I don’t have access to my laptop for now until new year :)

@pvary

pvary commented Jan 4, 2024

Copy link
Copy Markdown
Contributor

Do we have a way to check if the new tests were running on the CI? With the correct parameters?

@nastra

nastra commented Jan 4, 2024

Copy link
Copy Markdown
Contributor

Do we have a way to check if the new tests were running on the CI? With the correct parameters?

I don't think so, but I was verifying them all locally

@pvary

pvary commented Jan 5, 2024

Copy link
Copy Markdown
Contributor

Do we have a way to check if the new tests were running on the CI? With the correct parameters?

I don't think so, but I was verifying them all locally

In this case, I am find with these changes.
In the next PR we should port this changes to 1.18, and 1.16 too

@nastra

nastra commented Jan 5, 2024

Copy link
Copy Markdown
Contributor

@cgpoh can you please port these changes to other Flink versions?

@nastra nastra changed the title Flink: Create JUnit5 version of TestFlinkScan Flink 1.17: Create JUnit5 version of TestFlinkScan Jan 5, 2024
@nastra
nastra merged commit 0bf2602 into apache:main Jan 5, 2024
@cgpoh

cgpoh commented Jan 5, 2024

Copy link
Copy Markdown
Contributor Author

@cgpoh can you please port these changes to other Flink versions?

Sure. Will try to port these changes next week.

@cgpoh

cgpoh commented Jan 21, 2024

Copy link
Copy Markdown
Contributor Author

@nastra, thanks for the backport. Sorry that I'm not able to work on it earlier.

geruh pushed a commit to geruh/iceberg that referenced this pull request Jan 26, 2024
adnanhemani pushed a commit to adnanhemani/iceberg that referenced this pull request Jan 30, 2024
devangjhabakh pushed a commit to cdouglas/iceberg that referenced this pull request Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create JUnit5-version of TestFlinkScan

4 participants