~james/core

core/spindle/workflow_defs_test.go -rw-r--r-- 1.8 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package spindle

import (
	"errors"
	"fmt"
	"testing"

	"github.com/bluesky-social/indigo/atproto/syntax"

	"tangled.org/core/spindle/models"
	"tangled.org/core/spindle/xrpc"
)

func TestWorkflowDefinitionsAreCachedAndRateLimitedPerRepo(t *testing.T) {
	var c workflowDefCache
	loads := 0
	knotRead := func(repo syntax.DID) func() ([]*models.WorkflowDefinition, error) {
		return func() ([]*models.WorkflowDefinition, error) {
			loads++
			return []*models.WorkflowDefinition{{ID: "ci"}}, c.admit(repo)
		}
	}

	for range 20 {
		if _, err := c.list("did:plc:repo", "HEAD", knotRead("did:plc:repo")); err != nil {
			t.Fatal(err)
		}
	}
	if loads != 1 {
		t.Fatalf("%d loads for 20 reads of the same ref, want 1", loads)
	}

	// distinct refs skip the cache and each one is a knot read
	var busy error
	for i := range 3 * workflowDefLoadBurst {
		if _, err := c.list("did:plc:repo", fmt.Sprintf("refs/heads/b%d", i), knotRead("did:plc:repo")); err != nil {
			busy = err
			break
		}
	}
	if !errors.Is(busy, xrpc.ErrDefinitionsBusy) {
		t.Fatalf("%d uncached reads of one repo in a row ended with %v, want ErrDefinitionsBusy", 3*workflowDefLoadBurst, busy)
	}
	if _, err := c.list("did:plc:otherrepo", "HEAD", knotRead("did:plc:otherrepo")); err != nil {
		t.Fatalf("another repo was refused after the first one got busy: %v", err)
	}
}

func TestWorkflowDefinitionRateLimitOnlyCountsKnotReads(t *testing.T) {
	var c workflowDefCache
	local := func() ([]*models.WorkflowDefinition, error) { return nil, nil }
	for i := range 10 * workflowDefLoadBurst {
		if _, err := c.list("did:plc:repo", fmt.Sprintf("%040d", i), local); err != nil {
			t.Fatalf("local read %d was limited: %v", i, err)
		}
	}
	if err := c.admit("did:plc:repo"); err != nil {
		t.Fatalf("local reads used up the knot budget: %v", err)
	}
}