pool_test.go 574 B

123456789101112131415161718192021222324252627282930
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package syncs
  4. import "testing"
  5. func TestPool(t *testing.T) {
  6. var pool Pool[string]
  7. s := pool.Get() // should not panic
  8. if s != "" {
  9. t.Fatalf("got %q, want %q", s, "")
  10. }
  11. pool.New = func() string { return "new" }
  12. s = pool.Get()
  13. if s != "new" {
  14. t.Fatalf("got %q, want %q", s, "new")
  15. }
  16. var found bool
  17. for range 1000 {
  18. pool.Put("something")
  19. found = pool.Get() == "something"
  20. if found {
  21. break
  22. }
  23. }
  24. if !found {
  25. t.Fatalf("unable to get any value put in the pool")
  26. }
  27. }