From f64498f096c899b4784b0c6c97cae259df01e06f Mon Sep 17 00:00:00 2001 From: Hangxiang Yu Date: Mon, 1 Apr 2024 16:27:27 +0800 Subject: [PATCH] [env] Add test cases in flink-env test suite --- env/flink/env_flink.cc | 19 +++-- env/flink/env_flink_test_suite.cc | 122 +++++++++++++++++++++++++++--- env/flink/env_flink_test_suite.h | 7 +- 3 files changed, 132 insertions(+), 16 deletions(-) diff --git a/env/flink/env_flink.cc b/env/flink/env_flink.cc index b963fe508..26deba9e7 100644 --- a/env/flink/env_flink.cc +++ b/env/flink/env_flink.cc @@ -66,7 +66,7 @@ class FlinkWritableFile : public FSWritableFile { jobject fsDataOutputStream = jniEnv->CallObjectMethod( file_system_instance_, fileSystemCreateMethod.javaMethod, pathInstance); jniEnv->DeleteLocalRef(pathInstance); - if (fsDataOutputStream == nullptr) { + if (fsDataOutputStream == nullptr || jniEnv->ExceptionCheck()) { return CheckThenError( std::string( "CallObjectMethod Exception when Init FlinkWritableFile, ") @@ -193,7 +193,7 @@ class FlinkReadableFile : virtual public FSSequentialFile, jobject fsDataInputStream = jniEnv->CallObjectMethod( file_system_instance_, openMethod.javaMethod, pathInstance); jniEnv->DeleteLocalRef(pathInstance); - if (fsDataInputStream == nullptr) { + if (fsDataInputStream == nullptr || jniEnv->ExceptionCheck()) { return CheckThenError( std::string( "CallObjectMethod Exception when Init FlinkReadableFile, ") @@ -355,7 +355,7 @@ Status FlinkFileSystem::Init() { jobject fileSystemInstance = jniEnv->CallStaticObjectMethod( fileSystemClass.javaClass, fileSystemGetMethod.javaMethod, uriInstance); jniEnv->DeleteLocalRef(uriInstance); - if (fileSystemInstance == nullptr) { + if (fileSystemInstance == nullptr || jniEnv->ExceptionCheck()) { return CheckThenError( std::string( "CallStaticObjectMethod Exception when Init FlinkFileSystem, ") @@ -504,7 +504,7 @@ IOStatus FlinkFileSystem::GetChildren(const std::string& file_name, auto fileStatusArray = (jobjectArray)jniEnv->CallObjectMethod( file_system_instance_, listStatusMethod.javaMethod, pathInstance); jniEnv->DeleteLocalRef(pathInstance); - if (fileStatusArray == nullptr) { + if (fileStatusArray == nullptr || jniEnv->ExceptionCheck()) { return CheckThenError( std::string("Exception when CallObjectMethod in GetChildren, ") .append(listStatusMethod.ToString()) @@ -516,7 +516,7 @@ IOStatus FlinkFileSystem::GetChildren(const std::string& file_name, jsize fileStatusArrayLen = jniEnv->GetArrayLength(fileStatusArray); for (jsize i = 0; i < fileStatusArrayLen; i++) { jobject fileStatusObj = jniEnv->GetObjectArrayElement(fileStatusArray, i); - if (fileStatusObj == nullptr) { + if (fileStatusObj == nullptr || jniEnv->ExceptionCheck()) { jniEnv->DeleteLocalRef(fileStatusArray); return CheckThenError( "Exception when GetObjectArrayElement in GetChildren"); @@ -527,7 +527,7 @@ IOStatus FlinkFileSystem::GetChildren(const std::string& file_name, jobject subPath = jniEnv->CallObjectMethod(fileStatusObj, getPathMethod.javaMethod); jniEnv->DeleteLocalRef(fileStatusObj); - if (subPath == nullptr) { + if (subPath == nullptr || jniEnv->ExceptionCheck()) { jniEnv->DeleteLocalRef(fileStatusArray); return CheckThenError( std::string("Exception when CallObjectMethod in GetChildren, ") @@ -539,6 +539,13 @@ IOStatus FlinkFileSystem::GetChildren(const std::string& file_name, auto subPathStr = (jstring)jniEnv->CallObjectMethod( subPath, pathToStringMethod.javaMethod); jniEnv->DeleteLocalRef(subPath); + if (subPathStr == nullptr || jniEnv->ExceptionCheck()) { + jniEnv->DeleteLocalRef(fileStatusArray); + return CheckThenError( + std::string("Exception when CallObjectMethod in GetChildren, ") + .append(pathToStringMethod.ToString())); + } + const char* str = jniEnv->GetStringUTFChars(subPathStr, nullptr); result->emplace_back(str); jniEnv->ReleaseStringUTFChars(subPathStr, str); diff --git a/env/flink/env_flink_test_suite.cc b/env/flink/env_flink_test_suite.cc index 2b1a312ab..d7d849d0a 100644 --- a/env/flink/env_flink_test_suite.cc +++ b/env/flink/env_flink_test_suite.cc @@ -28,6 +28,8 @@ std::abort(); \ } +#define LOG(message) (std::cout << (message) << std::endl) + namespace ROCKSDB_NAMESPACE { EnvFlinkTestSuites::EnvFlinkTestSuites(const std::string& basePath) @@ -35,7 +37,15 @@ EnvFlinkTestSuites::EnvFlinkTestSuites(const std::string& basePath) void EnvFlinkTestSuites::runAllTestSuites() { setUp(); - testFileExist(); + LOG("Stage 1: setUp OK"); + testDirOperation(); + LOG("Stage 2: testDirOperation OK"); + testFileOperation(); + LOG("Stage 3: testFileOperation OK"); + testGetChildren(); + LOG("Stage 4: testGetChildren OK"); + testFileReadAndWrite(); + LOG("Stage 5: testFileReadAndWrite OK"); } void EnvFlinkTestSuites::setUp() { @@ -45,11 +55,108 @@ void EnvFlinkTestSuites::setUp() { } } -void EnvFlinkTestSuites::testFileExist() { - std::string fileName("test-file"); - Status result = flink_env_->FileExists(fileName); - ASSERT_TRUE(result.IsNotFound()); +void EnvFlinkTestSuites::testDirOperation() { + const std::string dir_name = "test-dir"; + ASSERT_TRUE(flink_env_->FileExists(dir_name).IsNotFound()); + ASSERT_TRUE(flink_env_->CreateDir(dir_name).ok()); + ASSERT_TRUE(flink_env_->CreateDirIfMissing(dir_name).ok()); + ASSERT_TRUE(!flink_env_->CreateDir(dir_name).ok()); + + bool is_dir; + ASSERT_TRUE(flink_env_->IsDirectory(dir_name, &is_dir).ok() && is_dir); + ASSERT_TRUE(flink_env_->FileExists(dir_name).ok()); + ASSERT_TRUE(flink_env_->DeleteDir(dir_name).ok()); + ASSERT_TRUE(flink_env_->FileExists(dir_name).IsNotFound()); +} + +void EnvFlinkTestSuites::testFileOperation() { + const std::string file_name = "test-file"; + const std::string not_exist_file_name = "not-exist-file"; + + // test file exists + ASSERT_TRUE(flink_env_->FileExists(file_name).IsNotFound()); + generateFile(file_name); + ASSERT_TRUE(flink_env_->FileExists(file_name).ok()); + + // test file status + uint64_t file_size, file_mtime; + ASSERT_TRUE(flink_env_->GetFileSize(file_name, &file_size).ok()); + ASSERT_TRUE(!flink_env_->GetFileSize(not_exist_file_name, &file_size).ok()); + ASSERT_TRUE(file_size > 0); + ASSERT_TRUE(flink_env_->GetFileModificationTime(file_name, &file_mtime).ok()); + ASSERT_TRUE( + !flink_env_->GetFileModificationTime(not_exist_file_name, &file_mtime) + .ok()); + ASSERT_TRUE(file_mtime > 0); + + // test renaming file + const std::string file_name_2 = "test-file-2"; + flink_env_->RenameFile(file_name, file_name_2); + ASSERT_TRUE(flink_env_->FileExists(file_name).IsNotFound()); + ASSERT_TRUE(flink_env_->FileExists(file_name_2).ok()); + ASSERT_TRUE(flink_env_->DeleteFile(file_name_2).ok()); + ASSERT_TRUE(flink_env_->FileExists(file_name_2).IsNotFound()); +} + +void EnvFlinkTestSuites::testGetChildren() { + const std::string dir_name = "test-dir"; + const std::string sub_dir_name = dir_name + "/test-sub-dir"; + const std::string file_name_1 = dir_name + "/test-file-1"; + const std::string file_name_2 = dir_name + "/test-file-2"; + ASSERT_TRUE(flink_env_->CreateDirIfMissing(dir_name).ok()); + ASSERT_TRUE(flink_env_->CreateDirIfMissing(sub_dir_name).ok()); + generateFile(file_name_1); + generateFile(file_name_2); + std::vector result, + expected{base_path_ + sub_dir_name, base_path_ + file_name_1, + base_path_ + file_name_2}; + ASSERT_TRUE(flink_env_->GetChildren(dir_name, &result).ok()); + ASSERT_TRUE(result.size() == 3); + std::sort(result.begin(), result.end()); + std::sort(expected.begin(), expected.end()); + ASSERT_TRUE(expected == result); +} + +void EnvFlinkTestSuites::testFileReadAndWrite() { + const std::string file_name = "test-file"; + const std::string content1 = "Hello World", content2 = ", Hello ForSt", + content = content1 + content2; + + std::unique_ptr write_result; + ASSERT_TRUE( + flink_env_->NewWritableFile(file_name, &write_result, EnvOptions()).ok()); + write_result->Append(content1); + write_result->Append(content2); + write_result->Sync(); + write_result->Flush(); + write_result->Close(); + + std::unique_ptr sequential_result; + ASSERT_TRUE( + flink_env_->NewSequentialFile(file_name, &sequential_result, EnvOptions()) + .ok()); + Slice sequential_data; + std::string sequential_scratch; + sequential_result->Skip(content1.size()); + sequential_result->Read(content2.size(), &sequential_data, + (char*)sequential_scratch.data()); + ASSERT_TRUE(sequential_data.data() == content2); + + std::unique_ptr random_access_result; + ASSERT_TRUE( + flink_env_ + ->NewRandomAccessFile(file_name, &random_access_result, EnvOptions()) + .ok()); + Slice random_access_data; + std::string random_access_scratch; + random_access_result->Read(content1.size(), content.size() - content1.size(), + &random_access_data, + (char*)random_access_scratch.data()); + ASSERT_TRUE(random_access_data.data() == content2); +} + +void EnvFlinkTestSuites::generateFile(const std::string& fileName) { // Generate a file manually const std::string prefix = "file:"; std::string writeFileName = base_path_ + fileName; @@ -57,10 +164,7 @@ void EnvFlinkTestSuites::testFileExist() { writeFileName = writeFileName.substr(prefix.size()); } std::ofstream writeFile(writeFileName); - writeFile << "testFileExist"; + writeFile << "Hello World"; writeFile.close(); - - result = flink_env_->FileExists(fileName); - ASSERT_TRUE(result.ok()); } } // namespace ROCKSDB_NAMESPACE \ No newline at end of file diff --git a/env/flink/env_flink_test_suite.h b/env/flink/env_flink_test_suite.h index 3826060d5..c7512b031 100644 --- a/env/flink/env_flink_test_suite.h +++ b/env/flink/env_flink_test_suite.h @@ -29,6 +29,11 @@ class EnvFlinkTestSuites { std::unique_ptr flink_env_; const std::string base_path_; void setUp(); - void testFileExist(); + void testDirOperation(); + void testFileOperation(); + void testGetChildren(); + void testFileReadAndWrite(); + + void generateFile(const std::string& fileName); }; } // namespace ROCKSDB_NAMESPACE \ No newline at end of file