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

fix test parseManyAliasesForCollections #127

Merged
merged 4 commits into from
Jan 21, 2024
Merged
Changes from 2 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 @@ -13,11 +13,6 @@
*/
package org.snakeyaml.engine.usecases.references;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.LinkedHashMap;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand All @@ -26,6 +21,12 @@
import org.snakeyaml.engine.v2.api.Load;
import org.snakeyaml.engine.v2.api.LoadSettings;

import java.time.Duration;
import java.time.Instant;
import java.util.LinkedHashMap;

import static org.junit.jupiter.api.Assertions.*;

@Tag("fast")
public class ReferencesTest {

Expand Down Expand Up @@ -85,7 +86,7 @@ public void referencesWithRecursiveKeysNotAllowedByDefault() {
fail();
} catch (Exception e) {
assertEquals("Recursive key for mapping is detected but it is not configured to be allowed.",
e.getMessage());
e.getMessage());
}
long time2 = System.currentTimeMillis();
float duration = (time2 - time1) / 1000;
Expand All @@ -95,22 +96,23 @@ public void referencesWithRecursiveKeysNotAllowedByDefault() {
@Test
@DisplayName("Parsing with aliases may take a lot of time, CPU and memory")
public void parseManyAliasesForCollections() {
String output = createDump(25);
// Load
long time1 = System.currentTimeMillis();
LoadSettings settings =
final var minDuration = Duration.ofMillis(400);
final var maxDuration = Duration.ofSeconds(15);
assertTimeout(maxDuration, () -> {
String output = createDump(25);
// Load
final var start = Instant.now();
LoadSettings settings =
LoadSettings.builder().setAllowRecursiveKeys(true).setMaxAliasesForCollections(50).build();
Load load = new Load(settings);
load.loadFromString(output);
long time2 = System.currentTimeMillis();
double duration = (time2 - time1) / 1000.0;
int cores = Runtime.getRuntime().availableProcessors();
double minDuration = 0.7;
if (cores > 4) {
minDuration = 0.4;
}
assertTrue(duration > minDuration, "It should take time. Time was " + duration + " seconds.");
assertTrue(duration < 9.0, "Time was " + duration + " seconds.");
Load load = new Load(settings);
load.loadFromString(output);
final var finish = Instant.now();
final var timeElapsed = Duration.between(start, finish);
assertTrue(
timeElapsed.compareTo(minDuration) > 0, /* timeElapsed > minDuration */
"Expected elapsed time (" + (timeElapsed.toMillis() / 1000f) + ") seconds > minDuration (" + (minDuration.toMillis() / 1000f) + " seconds)"
);
});
}

@Test
Expand All @@ -121,14 +123,14 @@ public void referencesWithRestrictedAliases() {
// Load
long time1 = System.currentTimeMillis();
LoadSettings settings =
LoadSettings.builder().setAllowRecursiveKeys(true).setMaxAliasesForCollections(40).build();
LoadSettings.builder().setAllowRecursiveKeys(true).setMaxAliasesForCollections(40).build();
Load load = new Load(settings);
try {
load.loadFromString(bigYAML);
fail();
} catch (Exception e) {
assertEquals("Number of aliases for non-scalar nodes exceeds the specified max=40",
e.getMessage());
e.getMessage());
}
long time2 = System.currentTimeMillis();
float duration = (time2 - time1) / 1000;
Expand Down