diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8f3e33e..2ccf4b5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 }} diff --git a/build.go b/build.go index 07bf116..cde41fe 100644 --- a/build.go +++ b/build.go @@ -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 diff --git a/build_test.go b/build_test.go index 8fa4cde..4826991 100644 --- a/build_test.go +++ b/build_test.go @@ -2,6 +2,7 @@ package jwt import ( "errors" + "sync" "testing" ) @@ -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 {