2022-09-30 04:34:14 +00:00
|
|
|
package bytesutil
|
2022-08-26 21:12:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInternStringSerial(t *testing.T) {
|
2023-09-01 07:34:16 +00:00
|
|
|
if err := testInternString(); err != nil {
|
2022-08-26 21:12:39 +00:00
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInternStringConcurrent(t *testing.T) {
|
|
|
|
concurrency := 5
|
|
|
|
resultCh := make(chan error, concurrency)
|
|
|
|
for i := 0; i < concurrency; i++ {
|
|
|
|
go func() {
|
2023-09-01 07:34:16 +00:00
|
|
|
resultCh <- testInternString()
|
2022-08-26 21:12:39 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
timer := time.NewTimer(5 * time.Second)
|
|
|
|
for i := 0; i < concurrency; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-resultCh:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 07:34:16 +00:00
|
|
|
func testInternString() error {
|
2022-08-26 21:12:39 +00:00
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
s := fmt.Sprintf("foo_%d", i)
|
|
|
|
s1 := InternString(s)
|
|
|
|
if s != s1 {
|
|
|
|
return fmt.Errorf("unexpected string returned from internString; got %q; want %q", s1, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|