Skip to content

[incubator-kie-issues#2181] make KOptListMoveIteratorTest deterministic using LinkedHashMap#3187

Open
hwu2024 wants to merge 1 commit into
apache:mainfrom
hwu2024:kie-issues#2181
Open

[incubator-kie-issues#2181] make KOptListMoveIteratorTest deterministic using LinkedHashMap#3187
hwu2024 wants to merge 1 commit into
apache:mainfrom
hwu2024:kie-issues#2181

Conversation

@hwu2024

@hwu2024 hwu2024 commented Dec 2, 2025

Copy link
Copy Markdown
Contributor

Issue

Fixes

We make the test deterministic by using a LinkedHashMap for entityToListSize, so the iteration order is the encounter order of entities

Map<Object, Integer> entityToListSize = Arrays.stream(entities)
        .collect(Collectors.toMap(
                Function.identity(),
                entity -> 2,
                Integer::sum,
                LinkedHashMap::new));

This preserves the expected ordering while keeping the rest of the test logic.

JIRA

Referenced pull requests

Checklist

  • Documentation updated if applicable.
  • Release notes updated if applicable.
  • Upgrade recipe provided if applicable.
How to replicate CI configuration locally?

Build Chain tool does "simple" maven build(s), the builds are just Maven commands, but because the repositories relates and depends on each other and any change in API or class method could affect several of those repositories there is a need to use build-chain tool to handle cross repository builds and be sure that we always use latest version of the code for each repository.

build-chain tool is a build tool which can be used on command line locally or in Github Actions workflow(s), in case you need to change multiple repositories and send multiple dependent pull requests related with a change you can easily reproduce the same build by executing it on Github hosted environment or locally in your development environment. See local execution details to get more information about it.

How to retest this PR or trigger a specific build:
  • for pull request checks
    Please add comment: Jenkins retest this

  • for a specific pull request check
    please add comment: Jenkins (re)run [optaplanner|optaplanner-quickstarts] tests

  • for a full downstream build

    • for jenkins job:
      please add comment: Jenkins run fdb
    • for github actions job:
      add the label run_fdb
  • for a compile downstream build
    please add comment: Jenkins run cdb

  • for a full production downstream build
    please add comment: Jenkins execute product fdb

  • for an upstream build
    please add comment: Jenkins run upstream

  • for quarkus branch checks
    Run checks against Quarkus current used branch
    Please add comment: Jenkins run quarkus-branch

  • for a quarkus branch specific check
    Run checks against Quarkus current used branch
    Please add comment: Jenkins (re)run [optaplanner|optaplanner-quickstarts] quarkus-branch

  • for quarkus main checks
    Run checks against Quarkus main branch
    Please add comment: Jenkins run quarkus-main

  • for a specific quarkus main check
    Run checks against Quarkus main branch
    Please add comment: Jenkins (re)run [optaplanner|optaplanner-quickstarts] quarkus-branch

  • for quarkus lts checks
    Run checks against Quarkus lts branch
    Please add comment: Jenkins run quarkus-lts

  • for a specific quarkus lts check
    Run checks against Quarkus lts branch
    Please add comment: Jenkins (re)run [optaplanner|optaplanner-quickstarts] quarkus-lts

  • for native checks
    Run native checks
    Please add comment: Jenkins run native

  • for a specific native check
    Run native checks
    Please add comment: Jenkins (re)run [optaplanner|optaplanner-quickstarts] native

  • for native lts checks
    Run native checks against quarkus lts branch
    Please add comment: Jenkins run native-lts

  • for a specific native lts check
    Run native checks against quarkus lts branch
    Please add comment: Jenkins (re)run [optaplanner|optaplanner-quickstarts] native-lts

CI Status

You can check OptaPlanner repositories CI status from Chain Status webpage.

How to backport a pull request to a different branch?

In order to automatically create a backporting pull request please add one or more labels having the following format backport-<branch-name>, where <branch-name> is the name of the branch where the pull request must be backported to (e.g., backport-7.67.x to backport the original PR to the 7.67.x branch).

NOTE: backporting is an action aiming to move a change (usually a commit) from a branch (usually the main one) to another one, which is generally referring to a still maintained release branch. Keeping it simple: it is about to move a specific change or a set of them from one branch to another.

Once the original pull request is successfully merged, the automated action will create one backporting pull request per each label (with the previous format) that has been added.

If something goes wrong, the author will be notified and at this point a manual backporting is needed.

NOTE: this automated backporting is triggered whenever a pull request on main branch is labeled or closed, but both conditions must be satisfied to get the new PR created.

@Rikkola Rikkola self-requested a review December 4, 2025 10:29
@Rikkola

Rikkola commented Dec 4, 2025

Copy link
Copy Markdown
Contributor

Hi @hwu2024 is it possible to make an unit test that proves the bug?

@hwu2024

hwu2024 commented Dec 4, 2025

Copy link
Copy Markdown
Contributor Author

Hi @Rikkola thanks for the review!

The core problem is that setupValidNonsequential4OptMove used Collectors.toMap() with the default HashMap, and then iterated entityToListSize.entrySet(). That makes the per‑entity lists depend on the HashMap iteration order, which HashMap makes no guarantees as to the order of the map. NonDex exposes this by shuffling that iterator and we can also trace the error by running the debug mode:

mvn -pl core/optaplanner-core-impl edu.illinois:nondex-maven-plugin:2.2.1:debug -Dtest=org.optaplanner.core.impl.heuristic.selector.move.generic.list.kopt.KOptListMoveIteratorTest#testNonsequentialKOptOnDifferentEntity

which the debug stack shows that

java.util.HashMap$EntrySet.iterator(HashMap.java:1106)
org.optaplanner.core.impl.heuristic.selector.move.generic.list.kopt.KOptListMoveIteratorTest.setupValidNonsequential4OptMove(KOptListMoveIteratorTest.java:233)

We can also reproduce by manually building two maps with the same keys/values but different iteration order (simulating HashMap), and runs the same list‑building logic as setupValidNonsequential4OptMove.

@Test
void entityToListDependsOnMapIterationOrder() {
    int k = 4;
    Object[] data = new Object[2 * k + 8];
    for (int i = 0; i < data.length; i++) {
        data[i] = "v" + i;
    }

    Map<Object, Integer> entityToListSizeOrder1 = new LinkedHashMap<>();
    entityToListSizeOrder1.put("e1", 6);
    entityToListSizeOrder1.put("e2", 4);

    Map<Object, Integer> entityToListSizeOrder2 = new LinkedHashMap<>();
    entityToListSizeOrder2.put("e2", 4);
    entityToListSizeOrder2.put("e1", 6);

    Map<Object, List<Object>> entityToList1 =
            buildEntityToListLikeNonsequentialHelper(entityToListSizeOrder1, data);
    Map<Object, List<Object>> entityToList2 =
            buildEntityToListLikeNonsequentialHelper(entityToListSizeOrder2, data);

    assertThat(entityToList1).isEqualTo(entityToList2);
}

private static Map<Object, List<Object>> buildEntityToListLikeNonsequentialHelper(
        Map<Object, Integer> entityToListSize, Object[] data) {
    Map<Object, List<Object>> entityToList = new HashMap<>();
    int offset = 0;
    for (Map.Entry<Object, Integer> entry : entityToListSize.entrySet()) {
        Object entity = entry.getKey();
        int listSize = entry.getValue();
        List<Object> entityList = new ArrayList<>(Arrays.asList(data).subList(offset, offset + listSize));
        for (int i = 0; i < listSize; i++) {
            entityList.add(2 * i + 1, entity + "-extra-" + i);
        }
        entityList.add(0, entity + "-start");
        entityList.add(entity + "-end");
        entityToList.put(entity, entityList);
        offset += listSize;
    }
    return entityToList;
}

and if we run

mvn -pl core/optaplanner-core-impl test -Dtest=org.optaplanner.core.impl.heuristic.selector.move.generic.list.kopt.KOptListMoveIteratorTest#entityToListDependsOnMapIterationOrder

we get

expected: 
  {"e1"=["e1-start",
      "v4",
      "e1-extra-0",
      "v5",
      "e1-extra-1",
      "v6",
      "e1-extra-2",
      "v7",
      "e1-extra-3",
      "v8",
      "e1-extra-4",
      "v9",
      "e1-extra-5",
      "e1-end"], "e2"=["e2-start",
      "v0",
      "e2-extra-0",
      "v1",
      "e2-extra-1",
      "v2",
      "e2-extra-2",
      "v3",
      "e2-extra-3",
      "e2-end"]}
 but was: 
  {"e1"=["e1-start",
      "v0",
      "e1-extra-0",
      "v1",
      "e1-extra-1",
      "v2",
      "e1-extra-2",
      "v3",
      "e1-extra-3",
      "v4",
      "e1-extra-4",
      "v5",
      "e1-extra-5",
      "e1-end"], "e2"=["e2-start",
      "v6",
      "e2-extra-0",
      "v7",
      "e2-extra-1",
      "v8",
      "e2-extra-2",
      "v9",
      "e2-extra-3",
      "e2-end"]}

It shows that the old logic using default HashMap was order‑sensitive. The LinkedHashMap change in this PR then removes that bug.

Thanks again for your time to follow up!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses non-deterministic test failures in KOptListMoveIteratorTest by ensuring predictable iteration order when processing entities. The fix uses LinkedHashMap instead of the default HashMap to preserve insertion order.

Key changes:

  • Added LinkedHashMap import
  • Modified Collectors.toMap call in setupValidNonsequential4OptMove to use LinkedHashMap::new as the map supplier

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +229 to +230
Integer::sum,
LinkedHashMap::new));

Copilot AI Jan 5, 2026

Copy link

Choose a reason for hiding this comment

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

This fix is incomplete. The same non-deterministic issue exists in the setupValidOddSequentialKOptMove method at lines 134-137, where entityToListSize is also created using Collectors.toMap without specifying LinkedHashMap::new as the map supplier. Both methods iterate over entityToListSize.entrySet() (line 142 in setupValidOddSequentialKOptMove, line 235 in this method) and rely on the iteration order matching the entities array order for correctness. The setupValidOddSequentialKOptMove method should receive the same fix to ensure full test determinism.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-deterministic order in KOptListMoveIteratorTest due to HashMap iteration

3 participants