sqids / sqids-go

Official Go port of Sqids. Generate short unique IDs from numbers.
https://sqids.org/go
MIT License
479 stars 8 forks source link

Is sqids-go thread safe? #26

Open ceeji opened 3 months ago

ceeji commented 3 months ago

Is this package safe for running in multiple goroutine?

peterhellberg commented 2 months ago

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

prochac commented 1 month ago

-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 ./...