TestConcurrentVerifyStatement runs VerifyStatement from multiple goroutines to exercise errgroup parallelism and catch race conditions. Run with: go test -race -count=1 -run TestConcurrentVerifyStatement ./pkg/policies/...
(t *testing.T)
| 34 | // to exercise errgroup parallelism and catch race conditions. |
| 35 | // Run with: go test -race -count=1 -run TestConcurrentVerifyStatement ./pkg/policies/... |
| 36 | func TestConcurrentVerifyStatement(t *testing.T) { |
| 37 | logger := zerolog.Nop() |
| 38 | |
| 39 | schema := &v12.CraftingSchema{ |
| 40 | Policies: &v12.Policies{ |
| 41 | Attestation: []*v12.PolicyAttachment{ |
| 42 | {Policy: &v12.PolicyAttachment_Ref{Ref: "file://testdata/workflow.yaml"}}, |
| 43 | {Policy: &v12.PolicyAttachment_Ref{Ref: "file://testdata/materials.yaml"}}, |
| 44 | }, |
| 45 | }, |
| 46 | } |
| 47 | |
| 48 | statement := loadStatementForTest(t, "testdata/statement.json") |
| 49 | |
| 50 | pv := NewPolicyVerifier(schema.GetPolicies(), nil, &logger) |
| 51 | |
| 52 | // Run multiple VerifyStatement calls concurrently |
| 53 | const goroutines = 10 |
| 54 | var wg sync.WaitGroup |
| 55 | errs := make([]error, goroutines) |
| 56 | results := make([][]*v1.PolicyEvaluation, goroutines) |
| 57 | |
| 58 | for i := range goroutines { |
| 59 | wg.Add(1) |
| 60 | go func() { |
| 61 | defer wg.Done() |
| 62 | res, err := pv.VerifyStatement(context.Background(), statement) |
| 63 | errs[i] = err |
| 64 | results[i] = res |
| 65 | }() |
| 66 | } |
| 67 | |
| 68 | wg.Wait() |
| 69 | |
| 70 | // All calls should succeed |
| 71 | for i := range goroutines { |
| 72 | assert.NoError(t, errs[i], "goroutine %d failed", i) |
| 73 | } |
| 74 | |
| 75 | // All calls should return the same number of evaluations |
| 76 | for i := 1; i < goroutines; i++ { |
| 77 | assert.Equal(t, len(results[0]), len(results[i]), |
| 78 | "goroutine %d returned different number of evaluations", i) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // TestConcurrentVerifyMaterial runs VerifyMaterial from multiple goroutines. |
| 83 | func TestConcurrentVerifyMaterial(t *testing.T) { |
nothing calls this directly
no test coverage detected