Files
lazygit/pkg/tasks/read_request_queue_test.go
Stefan HallerandClaude Opus 5.5 b8b9c8ad2b Leave reads to the end to the task that replaces the one reading
Some actions read the content of the main view to the end before they
act on it: the jump-to-file menu, going to the bottom, searching, and
jumping to a file from the diffstat. If a re-render replaces the task
while it reads, these actions are told that the content has been read
although only a part of it has, so the menu lists only the files in
that part. A background fetch re-renders the main view once a minute,
so now and then the menu is incomplete. The same happens if a read is
asked for after a render has been asked for but before its task has
started.

These actions act on the content of the task asked for last, so leave
the reads waiting for content to that task. Hold them from the moment
the task is asked for, hand over the ones a replaced task hasn't
answered, including the one it was reading for, and answer them once
the task asked for last has given the view all of its content. The
replacing task still reads its own first lines first, so that the reads
it takes over don't delay its first paint.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-16 07:14:39 +02:00

78 lines
2.0 KiB
Go

package tasks
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestReadRequestQueueHandsBackWhatNoTaskWillServe(t *testing.T) {
queue := newReadRequestQueue()
// No task has been asked for, so the request comes straight back to its caller.
assert.False(t, queue.enqueue(LinesToRead{Total: 1}))
// Once one has, requests are held for it, and the task serves its own first
// request ahead of them.
queue.hold()
assert.True(t, queue.enqueue(LinesToRead{Total: 2}))
queue.beginServing(LinesToRead{Total: 1})
assert.True(t, queue.enqueue(LinesToRead{Total: 3}))
request, ok := queue.dequeue()
assert.True(t, ok)
assert.Equal(t, 1, request.Total)
// A request that a task was serving when it was stopped goes back ahead of the
// ones made after it.
queue.requeue(request)
request, _ = queue.dequeue()
assert.Equal(t, 1, request.Total)
request, _ = queue.dequeue()
assert.Equal(t, 2, request.Total)
// What no task got to comes back when the queue stops holding requests, and
// nothing is taken from a caller after that.
unanswered := queue.stopServing()
assert.Len(t, unanswered, 1)
assert.Equal(t, 3, unanswered[0].Total)
assert.False(t, queue.enqueue(LinesToRead{Total: 4}))
_, ok = queue.dequeue()
assert.False(t, ok)
}
func TestReadRequestQueueRingsTheDoorbell(t *testing.T) {
queue := newReadRequestQueue()
doorbell := queue.beginServing(LinesToRead{Total: 1})
// The task's first request is there to be served right away.
select {
case <-doorbell:
default:
assert.Fail(t, "the doorbell didn't ring for the first request")
}
_, _ = queue.dequeue()
select {
case <-doorbell:
assert.Fail(t, "the doorbell rang with nothing queued")
default:
}
// A burst leaves one token, which stands for everything queued.
queue.enqueue(LinesToRead{Total: 2})
queue.enqueue(LinesToRead{Total: 3})
select {
case <-doorbell:
default:
assert.Fail(t, "the doorbell didn't ring")
}
select {
case <-doorbell:
assert.Fail(t, "the doorbell rang twice for one wake")
default:
}
}