oncefunc_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. *
  3. * Copyright 2022 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpcsync
  19. import (
  20. "sync"
  21. "sync/atomic"
  22. "testing"
  23. "time"
  24. )
  25. // TestOnceFunc tests that a OnceFunc is executed only once even with multiple
  26. // simultaneous callers of it.
  27. func (s) TestOnceFunc(t *testing.T) {
  28. var v int32
  29. inc := OnceFunc(func() { atomic.AddInt32(&v, 1) })
  30. const numWorkers = 100
  31. var wg sync.WaitGroup // Blocks until all workers have called inc.
  32. wg.Add(numWorkers)
  33. block := NewEvent() // Signal to worker goroutines to call inc
  34. for i := 0; i < numWorkers; i++ {
  35. go func() {
  36. <-block.Done() // Wait for a signal.
  37. inc() // Call the OnceFunc.
  38. wg.Done()
  39. }()
  40. }
  41. time.Sleep(time.Millisecond) // Allow goroutines to get to the block.
  42. block.Fire() // Unblock them.
  43. wg.Wait() // Wait for them to complete.
  44. if v != 1 {
  45. t.Fatalf("OnceFunc() called %v times; want 1", v)
  46. }
  47. }