klion26 commented on a change in pull request #13885: URL: https://github.com/apache/flink/pull/13885#discussion_r516440861
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/state/filesystem/FSDataBufferedInputStreamTest.java ########## @@ -0,0 +1,155 @@ +/* + * 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.state.filesystem; + +import org.apache.flink.core.fs.FSDataInputStream; +import org.apache.flink.core.fs.Path; +import org.apache.flink.core.memory.DataInputViewStreamWrapper; +import org.apache.flink.core.memory.DataOutputViewStreamWrapper; +import org.apache.flink.runtime.state.StreamStateHandle; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.IOException; +import java.util.UUID; + +/** + * Unit tests for the {@link FSDataBufferedInputStream}. + */ +public class FSDataBufferedInputStreamTest { + + private static final int KB = 1024; + private static final int MB = 1024 * KB; + + FsCheckpointStreamFactory.FsCheckpointStateOutputStream outputStream; + DataOutputViewStreamWrapper outView; + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Before + public void before() throws IOException { + String path = temporaryFolder.newFolder().getAbsolutePath(); + File outFile = new File(path, String.valueOf(UUID.randomUUID())); + + Path outPath = new Path(outFile.toURI()); + outputStream = new FsCheckpointStreamFactory.FsCheckpointStateOutputStream( + outPath, outPath.getFileSystem(), MB, KB); + outView = new DataOutputViewStreamWrapper(outputStream); + } + + @Test + public void testOrderRead() throws Exception { + // write + int num = 10_000_000; + StreamStateHandle stateHandle = writeData(num); + Assert.assertNotNull(stateHandle); + + int expectedStateSize = 2 * num * Integer.BYTES; + Assert.assertEquals(expectedStateSize, stateHandle.getStateSize()); + + // start read + FSDataInputStream fsDataInputStream = stateHandle.openInputStream(); + FSDataBufferedInputStream bufferedInputStream = new FSDataBufferedInputStream(fsDataInputStream); + DataInputViewStreamWrapper inputView = new DataInputViewStreamWrapper(bufferedInputStream); + + for (int i = 1; i <= num; i++) { + int read = inputView.readInt(); + Assert.assertEquals(i, read); + } + for (int i = num; i >= 1; i--) { + int read = inputView.readInt(); + Assert.assertEquals(i, read); + } + bufferedInputStream.close(); + stateHandle.discardState(); + } + + @Test + public void testSeek() throws Exception { Review comment: Do we need to add some bound test cases for `seek()` (such as seek out of bounding) here? ---------------------------------------------------------------- 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: [email protected]
