TestConcurrentVerifyMaterial runs VerifyMaterial from multiple goroutines.
(t *testing.T)
| 81 | |
| 82 | // TestConcurrentVerifyMaterial runs VerifyMaterial from multiple goroutines. |
| 83 | func TestConcurrentVerifyMaterial(t *testing.T) { |
| 84 | logger := zerolog.Nop() |
| 85 | |
| 86 | schema := &v12.CraftingSchema{ |
| 87 | Policies: &v12.Policies{ |
| 88 | Materials: []*v12.PolicyAttachment{ |
| 89 | { |
| 90 | Policy: &v12.PolicyAttachment_Ref{Ref: "file://testdata/sbom_syft.yaml"}, |
| 91 | }, |
| 92 | }, |
| 93 | }, |
| 94 | } |
| 95 | |
| 96 | material := &v1.Attestation_Material{ |
| 97 | Id: "sbom", |
| 98 | MaterialType: v12.CraftingSchema_Material_SBOM_SPDX_JSON, |
| 99 | M: &v1.Attestation_Material_Artifact_{ |
| 100 | Artifact: &v1.Attestation_Material_Artifact{}, |
| 101 | }, |
| 102 | } |
| 103 | |
| 104 | pv := NewPolicyVerifier(schema.GetPolicies(), nil, &logger) |
| 105 | |
| 106 | const goroutines = 10 |
| 107 | var wg sync.WaitGroup |
| 108 | errs := make([]error, goroutines) |
| 109 | results := make([][]*v1.PolicyEvaluation, goroutines) |
| 110 | |
| 111 | for i := range goroutines { |
| 112 | wg.Add(1) |
| 113 | go func() { |
| 114 | defer wg.Done() |
| 115 | res, err := pv.VerifyMaterial(context.Background(), material, "testdata/sbom-spdx.json") |
| 116 | errs[i] = err |
| 117 | results[i] = res |
| 118 | }() |
| 119 | } |
| 120 | |
| 121 | wg.Wait() |
| 122 | |
| 123 | for i := range goroutines { |
| 124 | assert.NoError(t, errs[i], "goroutine %d failed", i) |
| 125 | } |
| 126 | |
| 127 | for i := 1; i < goroutines; i++ { |
| 128 | assert.Equal(t, len(results[0]), len(results[i]), |
| 129 | "goroutine %d returned different number of evaluations", i) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // TestWithMaxConcurrency verifies the WithMaxConcurrency option is applied. |
| 134 | func TestWithMaxConcurrency(t *testing.T) { |
nothing calls this directly
no test coverage detected