Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is sqids-go thread safe? #26

Open
ceeji opened this issue Mar 16, 2024 · 2 comments
Open

Is sqids-go thread safe? #26

ceeji opened this issue Mar 16, 2024 · 2 comments
Assignees

Comments

@ceeji
Copy link

ceeji commented Mar 16, 2024

Is this package safe for running in multiple goroutine?

@peterhellberg
Copy link
Collaborator

@ceeji I have not yet had time to spend time looking into this.

@prochac
Copy link

prochac commented May 29, 2024

-race haven't detected any data race.

package main

import (
	"sync"
	"testing"

	"github.com/sqids/sqids-go"
)

func TestRace(t *testing.T) {
	s, _ := sqids.New()

	const n = 1_000_000
	var wg sync.WaitGroup
	wg.Add(n)
	for _ = range n {
		go func() {
			defer wg.Done()
			id, err := s.Encode([]uint64{42})
			if err != nil {
				t.Error(err)
				panic(err)
			}
			if id != "Jg" {
				t.Error("unexpected id:", id)
				panic(err)
			}
			nums := s.Decode(id)
			if len(nums) != 1 && nums[0] != 42 {
				t.Error("unexpected nums:", nums)
				panic(err)
			}
		}()
	}
	wg.Wait()
}
$ go test -test.run ^\QTestRace\E$ -race ./...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants