Files
notarius/internal/framework/pipeline/runner_session_test.go

92 lines
3.0 KiB
Go

package pipeline
import (
"context"
"fmt"
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)
func TestRunnerUsesProvidedSessionWithoutSourceFallback(t *testing.T) {
for _, test := range []struct {
name string
session string
want string
}{
{name: "provided", session: " supplied-session ", want: "supplied-session"},
{name: "empty"},
} {
t.Run(test.name, func(t *testing.T) {
prepared := preparedConcurrentPipeline(t, 1)
var session string
calls := 0
installExtractOperation(prepared, 0, func(_ context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) {
calls++
session = request.SessionID
return erasedTypedResult{Value: typedValueForLane(0, request.Chunk.Index)}, nil
})
_, err := New().Run(context.Background(), RunInput{
Prepared: prepared,
RawInput: []byte("input"),
SessionID: test.session,
Workers: 1,
})
if err != nil {
t.Fatal(err)
}
if calls != 1 || session != test.want {
t.Fatalf("extract calls=%d session=%q, want one call and %q", calls, session, test.want)
}
})
}
}
func TestRunnerSharesWholeSourceIndexAcrossJobsAndSteps(t *testing.T) {
prepared := preparedOrderedPipeline(t, 3, orderedLaneSpec{"first", "notes"}, orderedLaneSpec{"second", "notes"})
indexes := make(chan *source.DocumentIndex, 6)
for step := range prepared.Steps {
lane := &prepared.Steps[step].lanes[0]
original := lane.typed.extract
lane.typed.extract = func(ctx context.Context, impl any, request contracts.ExtractionInput, execution ExecutionContext[any]) (erasedTypedResult, error) {
if request.SourceIndex == nil {
return erasedTypedResult{}, fmt.Errorf("missing source index")
}
for position, unit := range request.Source.Units {
if got, ok := request.SourceIndex.Position(unit.ID); !ok || got != position {
return erasedTypedResult{}, fmt.Errorf("source index position for %d = %d, %t, want %d", unit.ID, got, ok, position)
}
}
indexes <- request.SourceIndex
return original(ctx, impl, request, execution)
}
}
var previous *source.DocumentIndex
for run := 0; run < 2; run++ {
doc := typedTestDocumentWithUnits(3)
if run == 1 {
doc.Units[0], doc.Units[2] = doc.Units[2], doc.Units[0]
doc.Digest, _ = source.DigestDocument(doc)
}
prepared.input.(*typedTestInput).doc = doc
prepared.chunker = &typedTestChunker{key: "typed/chunk", plan: typedTestPlan(doc)}
if _, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Workers: 3}); err != nil {
t.Fatal(err)
}
if len(indexes) != 6 {
t.Fatalf("observed %d extraction jobs, want 6", len(indexes))
}
first := <-indexes
for i := 1; i < 6; i++ {
if got := <-indexes; got != first {
t.Fatal("jobs did not share the run source index")
}
}
if first == previous {
t.Fatal("separate runs shared a source index")
}
previous = first
}
}