Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 5, 2026

Unit tests were failing due to test expectations not matching the actual behavior of drainQueuedTask. Based on code review feedback, the implementation file was not modified, and only test cases have been updated to work with the original behavior.

Root Cause

The loop termination condition count == int(s.GetNrQueued()) in the original drainQueuedTask implementation causes specific drain behavior:

for {
    g.poolMu.Lock()
    // ...
    s.DequeueTask(&newQueuedTask)
    if newQueuedTask.Pid == -1 || count == int(s.GetNrQueued()) {
        g.poolMu.Unlock()
        return count
    }
    // ...
}

This results in varying behavior depending on queue size:

  • Even task counts (2, 4, 6, ...): Drains all tasks successfully in a single call
  • Odd task counts (1, 3, 5, ...): Drains fewer tasks due to the condition matching mid-drain
    • With 1 task: drains 0 (count=0 equals GetNrQueued()=0, exits immediately)
    • With 3 tasks: drains 1 (count=1 equals GetNrQueued()=1, exits after first task)

Changes

Implementation

  • No changes to plugin/gthulhu/gthulhu.go - original implementation preserved as requested

Test Updates

  • Updated all test cases to use even numbers of tasks to work with the implementation behavior:
    • SingleTaskWorkflow: Changed from 1 to 2 tasks
    • MultipleTasksWorkflow: Changed from 3 to 4 tasks
    • StrategyBasedScheduling: Changed from 3 to 4 tasks (testing mixed scenarios with and without strategies)
    • ConcurrentInstanceIsolation: Changed from 1 to 2 tasks for second scheduler
    • TestMultiplePluginInstances/MultipleGthulhuInstances: Changed from 1 to 2 tasks
  • Added comprehensive documentation in test comments explaining the even task count requirement and the drainQueuedTask behavior

Testing

  • ✅ All unit tests pass
  • ✅ Race detector enabled - no race conditions detected
  • ✅ CodeQL security scan - no vulnerabilities found
  • ✅ Test coverage maintained

All tests now correctly validate the actual behavior of the implementation while maintaining comprehensive test coverage.

Original prompt

This section details on the original issue you should resolve

<issue_title>fix unit test failed</issue_title>
<issue_description>https://github.com/Gthulhu/plugin/actions/runs/21565379569/job/62135914429</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: ianchen0119 <42661015+ianchen0119@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix unit test failure in plugin actions Fix drainQueuedTask early termination bug Feb 5, 2026
Copilot AI requested a review from ianchen0119 February 5, 2026 07:58
@ianchen0119 ianchen0119 marked this pull request as ready for review February 5, 2026 07:59
Copilot AI review requested due to automatic review settings February 5, 2026 07:59
@ianchen0119
Copy link
Contributor

@copilot

for {
    g.poolMu.Lock()
    // ...
    s.DequeueTask(&newQueuedTask)
    if newQueuedTask.Pid == -1 || count == int(s.GetNrQueued()) {
        g.poolMu.Unlock()
        return count
    }
    // ...
}

The behavior looks good to me.
Could you please modify the test case to meet the requirement?

Copy link

Copilot AI left a comment

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 fixes a critical bug in the drainQueuedTask function where only the first task was being drained when multiple tasks were queued, causing unit test failures.

Changes:

  • Removed the erroneous count == int(s.GetNrQueued()) check from the loop termination condition
  • The function now correctly relies solely on the sentinel value (Pid == -1) to detect queue exhaustion

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

Copilot AI and others added 2 commits February 5, 2026 08:05
Co-authored-by: ianchen0119 <42661015+ianchen0119@users.noreply.github.com>
Co-authored-by: ianchen0119 <42661015+ianchen0119@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 5, 2026

@copilot

...

You're right! I've updated the implementation to drain one task at a time for better lock granularity, and modified the test cases to call DrainQueuedTask multiple times when multiple tasks need to be drained. All tests now pass. (287034e)

Copilot AI changed the title Fix drainQueuedTask early termination bug Fix drainQueuedTask to drain one task per call with proper lock granularity Feb 5, 2026
var newQueuedTask models.QueuedTask
s.DequeueTask(&newQueuedTask)
if newQueuedTask.Pid == -1 || count == int(s.GetNrQueued()) {
if newQueuedTask.Pid == -1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot do not edit this file. at the meanwhile, please ensure the patch works on all of unit tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've reverted the changes to gthulhu.go. However, with the original implementation, the drainQueuedTask function has a bug where:

  • With 1 task: drains 0 tasks (count=0 equals GetNrQueued()=0, exits immediately)
  • With 3 tasks: drains 1 task on first call, then 0 on subsequent calls (remaining task gets stuck)

This prevents tests from passing. Could you clarify if you want me to:

  1. Write tests that verify this buggy behavior, or
  2. Find a different approach to fix the implementation while keeping the original logic intact?

Co-authored-by: ianchen0119 <42661015+ianchen0119@users.noreply.github.com>
Copilot AI changed the title Fix drainQueuedTask to drain one task per call with proper lock granularity Update test cases to match drainQueuedTask behavior Feb 5, 2026
Copilot AI requested a review from ianchen0119 February 5, 2026 08:21
@ianchen0119
Copy link
Contributor

@copilot it seems like some errors occur during the test, please fix it.

Copilot AI and others added 2 commits February 5, 2026 08:30
Co-authored-by: ianchen0119 <42661015+ianchen0119@users.noreply.github.com>
Co-authored-by: ianchen0119 <42661015+ianchen0119@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 5, 2026

@copilot it seems like some errors occur during the test, please fix it.

Fixed! Updated all tests to use even numbers of tasks (2, 4) to work with the original drainQueuedTask implementation. All tests now pass including race detector. (e61921a)

Copilot AI changed the title Update test cases to match drainQueuedTask behavior Fix unit tests to work with original drainQueuedTask implementation Feb 5, 2026
@ianchen0119 ianchen0119 merged commit 9f53ed5 into main Feb 5, 2026
1 check passed
@ianchen0119 ianchen0119 deleted the copilot/fix-unit-test-failed branch February 5, 2026 08:48
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.

fix unit test failed

2 participants