Skip to content

Commit

Permalink
Add concurrent builder test (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Feb 24, 2023
1 parent 3acd25a commit 5334816
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ on:
# See https://github.com/cristalhq/.github/.github/workflows
jobs:
build:
uses: cristalhq/.github/.github/workflows/build.yml@main

codeql:
uses: cristalhq/.github/.github/workflows/codeql.yml@main
uses: cristalhq/.github/.github/workflows/build.yml@3f2ea32d08cea472a5d81a15769eb911595118af # v0.2.1

vuln:
uses: cristalhq/.github/.github/workflows/vuln.yml@main
uses: cristalhq/.github/.github/workflows/vuln.yml@3f2ea32d08cea472a5d81a15769eb911595118af # v0.2.1

release:
if: github.event_name == 'workflow_dispatch'
uses: cristalhq/.github/.github/workflows/release.yml@main
uses: cristalhq/.github/.github/workflows/release.yml@3f2ea32d08cea472a5d81a15769eb911595118af # v0.2.1
with:
tag: ${{ github.event.input.tag }}
1 change: 1 addition & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func WithContentType(cty string) BuilderOption {
}

// Builder is used to create a new token.
// Safe to use concurrently.
type Builder struct {
signer Signer
header Header
Expand Down
37 changes: 37 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jwt

import (
"errors"
"sync"
"testing"
)

Expand Down Expand Up @@ -226,6 +227,42 @@ func TestBuildMalformed(t *testing.T) {
)
}

func TestBuilderConcurrently(t *testing.T) {
signer, err := NewSignerHS(HS256, []byte("test-key"))
if err != nil {
t.Fatal(err)
}

builder := NewBuilder(signer)

errCh := make(chan error, 10)
claims := "string-claims"

var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()

token, err := builder.Build(claims)
errCh <- err

if token.String() == "" {
panic("should not be empty")
}
}()
}

wg.Wait()
close(errCh)

for err := range errCh {
if err != nil {
t.Fatal(err)
}
}
}

type badSigner struct{}

func (badSigner) SignSize() int {
Expand Down

0 comments on commit 5334816

Please sign in to comment.