Skip to content

Commit 5334816

Browse files
authored
Add concurrent builder test (#136)
1 parent 3acd25a commit 5334816

3 files changed

Lines changed: 41 additions & 6 deletions

File tree

.github/workflows/build.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@ on:
1717
# See https://github.com/cristalhq/.github/.github/workflows
1818
jobs:
1919
build:
20-
uses: cristalhq/.github/.github/workflows/build.yml@main
21-
22-
codeql:
23-
uses: cristalhq/.github/.github/workflows/codeql.yml@main
20+
uses: cristalhq/.github/.github/workflows/build.yml@3f2ea32d08cea472a5d81a15769eb911595118af # v0.2.1
2421

2522
vuln:
26-
uses: cristalhq/.github/.github/workflows/vuln.yml@main
23+
uses: cristalhq/.github/.github/workflows/vuln.yml@3f2ea32d08cea472a5d81a15769eb911595118af # v0.2.1
2724

2825
release:
2926
if: github.event_name == 'workflow_dispatch'
30-
uses: cristalhq/.github/.github/workflows/release.yml@main
27+
uses: cristalhq/.github/.github/workflows/release.yml@3f2ea32d08cea472a5d81a15769eb911595118af # v0.2.1
3128
with:
3229
tag: ${{ github.event.input.tag }}

build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func WithContentType(cty string) BuilderOption {
1919
}
2020

2121
// Builder is used to create a new token.
22+
// Safe to use concurrently.
2223
type Builder struct {
2324
signer Signer
2425
header Header

build_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package jwt
22

33
import (
44
"errors"
5+
"sync"
56
"testing"
67
)
78

@@ -226,6 +227,42 @@ func TestBuildMalformed(t *testing.T) {
226227
)
227228
}
228229

230+
func TestBuilderConcurrently(t *testing.T) {
231+
signer, err := NewSignerHS(HS256, []byte("test-key"))
232+
if err != nil {
233+
t.Fatal(err)
234+
}
235+
236+
builder := NewBuilder(signer)
237+
238+
errCh := make(chan error, 10)
239+
claims := "string-claims"
240+
241+
var wg sync.WaitGroup
242+
wg.Add(10)
243+
for i := 0; i < 10; i++ {
244+
go func() {
245+
defer wg.Done()
246+
247+
token, err := builder.Build(claims)
248+
errCh <- err
249+
250+
if token.String() == "" {
251+
panic("should not be empty")
252+
}
253+
}()
254+
}
255+
256+
wg.Wait()
257+
close(errCh)
258+
259+
for err := range errCh {
260+
if err != nil {
261+
t.Fatal(err)
262+
}
263+
}
264+
}
265+
229266
type badSigner struct{}
230267

231268
func (badSigner) SignSize() int {

0 commit comments

Comments
 (0)