aboutsummaryrefslogtreecommitdiff
path: root/internal/ingest/service_order_test.go
blob: 952e411127a552ff354f966e22ce569a39728a7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package ingest

import (
	"github.com/Fuwn/plutia/internal/types"
	"testing"
)

func TestCollectVerifiedInOrder_OutOfOrderInput(t *testing.T) {
	results := make(chan verifyResult, 3)

	results <- verifyResult{index: 2, op: types.ParsedOperation{Sequence: 3}}
	results <- verifyResult{index: 0, op: types.ParsedOperation{Sequence: 1}}
	results <- verifyResult{index: 1, op: types.ParsedOperation{Sequence: 2}}

	close(results)

	ordered, err := collectVerifiedInOrder(3, results)

	if err != nil {
		t.Fatalf("collect verified: %v", err)
	}

	if len(ordered) != 3 {
		t.Fatalf("ordered length mismatch: got %d want 3", len(ordered))
	}

	for i := 0; i < 3; i++ {
		if ordered[i].op.Sequence != uint64(i+1) {
			t.Fatalf("unexpected sequence at index %d: got %d want %d", i, ordered[i].op.Sequence, i+1)
		}
	}
}

func TestCollectVerifiedInOrder_MissingResult(t *testing.T) {
	results := make(chan verifyResult, 2)

	results <- verifyResult{index: 0, op: types.ParsedOperation{Sequence: 1}}
	results <- verifyResult{index: 2, op: types.ParsedOperation{Sequence: 3}}

	close(results)

	if _, err := collectVerifiedInOrder(3, results); err == nil {
		t.Fatalf("expected missing result error")
	}
}