lnd/lnutils/context_test.go
Olaoluwa Osuntokun 532a7032d6 multi/test: add unit tests for gossip result helpers and context bridge
In this commit, we add test coverage for the new helper functions
introduced as part of the chan error -> Future[error] migration.

discovery/gossip_result_test.go covers AwaitGossipResult (success,
error propagation, and context cancellation) and the idempotency of
completeGossipResult (a second call must never block or overwrite the
first result).

lnutils/context_test.go covers ContextFromQuit, verifying that closing
the quit channel cancels the derived context and that calling cancel()
allows the internal goroutine to exit cleanly without leaking.

actor/future_test.go removes a stale doc comment that was left over
from a prior edit pass.
2026-04-10 19:16:49 -07:00

68 lines
1.6 KiB
Go

package lnutils
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// TestContextFromQuitQuitCancels verifies that closing the quit channel
// cancels the derived context.
func TestContextFromQuitQuitCancels(t *testing.T) {
t.Parallel()
quit := make(chan struct{})
ctx, cancel := ContextFromQuit(quit)
defer cancel()
// The context should not be done yet.
select {
case <-ctx.Done():
t.Fatal("context cancelled before quit was closed")
default:
}
// Closing the quit channel should cancel the context.
close(quit)
select {
case <-ctx.Done():
case <-time.After(time.Second):
t.Fatal("context was not cancelled after quit was closed")
}
require.ErrorIs(t, ctx.Err(), context.Canceled)
}
// TestContextFromQuitCancelCleansUp verifies that calling the returned cancel
// function cancels the context and allows the internal goroutine to exit
// cleanly, preventing a goroutine leak.
func TestContextFromQuitCancelCleansUp(t *testing.T) {
t.Parallel()
// Use a quit channel that is never closed to ensure the goroutine
// exits via the cancel path, not the quit path.
quit := make(chan struct{})
ctx, cancel := ContextFromQuit(quit)
// The context should not be done yet.
select {
case <-ctx.Done():
t.Fatal("context cancelled before cancel was called")
default:
}
// Calling cancel should cancel the context. The internal goroutine
// exits via <-ctx.Done().
cancel()
select {
case <-ctx.Done():
case <-time.After(time.Second):
t.Fatal("context was not cancelled after cancel() was called")
}
require.ErrorIs(t, ctx.Err(), context.Canceled)
}