Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file: migrate read lock test to new harness #3584 #5618

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class FileRoutes extends RouteBuilder {

@Override
public void configure() {
from("file://target/" + READ_LOCK_IN + "?"
from("file://target/test-files/read-lock-in?"
+ "initialDelay=0&"
+ "move=.done&"
+ "delay=1000&"
Expand All @@ -48,7 +48,7 @@ public void configure() {
+ "readLockCheckInterval=2000&"
+ "readLockLoggingLevel=TRACE&"
+ "readLockTimeout=5000")
.to("file://target/" + READ_LOCK_OUT);
.to("file://target/test-files/read-lock-out");

from("file://target/test-files/quartz-scheduled?scheduler=quartz&scheduler.cron=0/1+*+*+*+*+?&repeatCount=0")
.convertBodyTo(String.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
Expand All @@ -34,7 +31,6 @@
import io.restassured.path.json.JsonPath;
import io.restassured.response.ValidatableResponse;
import org.apache.camel.quarkus.core.util.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.apache.camel.quarkus.component.file.it.FileResource.CONSUME_BATCH;
Expand All @@ -50,21 +46,6 @@ class FileTest {
private static final String FILE_CONTENT_02 = "Hello2";
private static final String FILE_CONTENT_03 = "Hello3";

private List<Path> pathsToDelete = new LinkedList<>();

@AfterEach
public void afterEach() {
pathsToDelete.stream().forEach(p -> {
try {
Files.delete(p);
} catch (IOException e) {
// ignore
}
});

pathsToDelete.clear();
}

@Test
public void batch() throws InterruptedException, UnsupportedEncodingException {
// Create 2 files
Expand Down Expand Up @@ -126,42 +107,6 @@ public void fileWatchCreateUpdate() throws IOException, InterruptedException {
awaitEvent(dir, file, "DELETE");
}

@Test
public void fileReadLock_minLength() throws Exception {
// Create a new file
String filePath = RestAssured.given()
.contentType(ContentType.BINARY)
.body(new byte[] {})
.post("/file/create/{name}", FileRoutes.READ_LOCK_IN)
.then()
.statusCode(201)
.extract()
.body()
.asString();

String fileName = Paths.get(filePath).getFileName().toString();

Thread.sleep(10_000L);

// Read the file that should not be there (.done folder)
RestAssured
.get("/file/get/{folder}/{name}", FileRoutes.READ_LOCK_IN + "/.done", fileName)
.then()
.statusCode(204);

// Read the file that should not be there (output folder)
RestAssured
.get("/file/get/{folder}/{name}", FileRoutes.READ_LOCK_OUT, fileName)
.then()
.statusCode(204);

// Read the file that should be there (input folder)
RestAssured
.get("/file/get/{folder}/{name}", FileRoutes.READ_LOCK_IN, fileName)
.then()
.statusCode(200);
}

private static String createFile(String content, String path) throws UnsupportedEncodingException {
return createFile(content, path, "UTF-8", null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,14 @@
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.CHARSET_READ_FILE_CONTENT;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.CHARSET_WRITE_FILE_CONTENT;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.CHARSET_WRITE_FILE_CREATION_FOLDER;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.CHARSET_WRITE_FILE_NAME;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.FILE_CREATION_FILE_CONTENT;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.FILE_CREATION_FILE_NAME;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.FILE_CREATION_FOLDER;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.FILTER_NON_SKIPPED_FILE_CONTENT;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.IDEMPOTENT_FILE_CONTENT;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.IDEMPOTENT_FILE_NAME;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.POLL_ENRICH_FILE_CONTENT;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.QUARTZ_SCHEDULED_FILE_CONTENT;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.TEST_FILES_FOLDER;
import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.*;
import static org.apache.commons.io.FileUtils.readFileToString;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* This is an attempt to rewrite file tests from scratch in a cleaner way. It aims at having the test logic more
Expand Down Expand Up @@ -172,7 +161,22 @@ void writeFileWithIso8859_1CharsetShouldSucceed() throws IOException {
await().atMost(1, TimeUnit.SECONDS).until(() -> Files.exists(expectedFilePath));

assertEquals(CHARSET_WRITE_FILE_CONTENT, readFileToString(expectedFilePath.toFile(), charset));

}

@Test
void readLockCantBeAcquiredOnFileSmallerThanReadLockMinLength() throws InterruptedException {

Path inputFilePath = TEST_FILES_FOLDER.resolve(Paths.get(READ_LOCK_FOLDER_IN, READ_LOCK_FILE_NAME));
Path doneFilePath = TEST_FILES_FOLDER.resolve(Paths.get(READ_LOCK_FOLDER_IN, ".done", READ_LOCK_FILE_NAME));
Path outputFilePath = TEST_FILES_FOLDER.resolve(Paths.get(READ_LOCK_FOLDER_OUT, READ_LOCK_FILE_NAME));

Thread.sleep(10_000L);

// Check that the input file still reside in input folder
assertTrue(Files.exists(inputFilePath));

// Check that .done and output folder do not contain the input file
assertFalse(Files.exists(doneFilePath));
assertFalse(Files.exists(outputFilePath));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class NonFlakyFileTestResource implements QuarkusTestResourceLifecycleMan
static final String FILE_CREATION_FOLDER = "file-creation";
static final String FILE_CREATION_FILE_NAME = "should-be-created";
static final String FILE_CREATION_FILE_CONTENT = FILE_CREATION_FILE_NAME + "-CONTENT";
static final String READ_LOCK_FOLDER_IN = "read-lock-in";
static final String READ_LOCK_FOLDER_OUT = "read-lock-out";
static final String READ_LOCK_FILE_NAME = "read-lock-file";

private final Map<Path, byte[]> createdTestFilesExpectedContent = new HashMap<>();

Expand All @@ -71,8 +74,11 @@ public Map<String, String> start() {

createTestFile("charset-read", CHARSET_READ_FILE_NAME, CHARSET_READ_FILE_CONTENT, StandardCharsets.ISO_8859_1);

createTestFile("read-lock-in", READ_LOCK_FILE_NAME);

// Do not use createTestFile("file-creation"... as it is already reserved by the createFileShouldSucceed test
// Do not use createTestFile("charset-write"... as it is already reserved by the writeFileWithIso8859_1CharsetShouldSucceed test
// Do not use createTestFile("read-lock-out"... as it is already reserved by the readLockCantBeAcquiredOnFileSmallerThanReadLockMinLength test

ensureAllTestFilesCreatedWithExpectedContent();
} catch (Exception ex) {
Expand Down
Loading