Files
tasks/common/goro/adaptive_pool_test.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

146 lines
3.5 KiB
Go

package goro_test
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.temporal.io/server/common/clock"
"go.temporal.io/server/common/goro"
)
func block() { <-make(chan struct{}) }
func nothing() {}
func TestAdaptivePool_CallsF(t *testing.T) {
t.Parallel()
ts := clock.NewEventTimeSource()
p := goro.NewAdaptivePool(ts, 1, 10, 10*time.Millisecond, 10)
defer p.Stop()
var wg sync.WaitGroup
wg.Add(1)
p.Do(wg.Done)
wg.Wait()
}
func TestAdaptivePool_Grows(t *testing.T) {
t.Parallel()
ts := clock.NewEventTimeSource()
p := goro.NewAdaptivePool(ts, 5, 10, 10*time.Millisecond, 10)
defer p.Stop()
// occupy five workers
p.Do(block)
p.Do(block)
p.Do(block)
p.Do(block)
p.Do(block)
// sixth call will still start new worker after delay
doneCh := make(chan struct{})
go func() {
p.Do(block)
doneCh <- struct{}{}
}()
// wait for goroutine to block in Do
// there should be one timer
assert.Eventually(t, func() bool { return ts.NumTimers() == 1 }, time.Second, time.Millisecond)
select {
case <-doneCh:
t.Error("should be blocked")
return
default:
}
assert.Equal(t, 5, p.NumWorkers()) // still 5 here
ts.Advance(15 * time.Millisecond)
<-doneCh
assert.Equal(t, 6, p.NumWorkers()) // now 6 here
}
func TestAdaptivePool_DoesntGrowPastMax(t *testing.T) {
t.Parallel()
ts := clock.NewEventTimeSource()
p := goro.NewAdaptivePool(ts, 5, 5, 10*time.Millisecond, 10)
defer p.Stop()
// occupy five workers, one is interruptible
p.Do(block)
p.Do(block)
interruptCh := make(chan struct{})
p.Do(func() { interruptCh <- struct{}{} })
p.Do(block)
p.Do(block)
// sixth call will block
doneCh := make(chan struct{})
go func() {
p.Do(block)
doneCh <- struct{}{}
}()
// wait for goroutine to block in Do
// we can't use NumTimers since it doesn't create a timer
time.Sleep(10 * time.Millisecond)
select {
case <-doneCh:
t.Error("should be blocked")
return
default:
}
// unblock fifth, which will allow sixth to run immediately
<-interruptCh
// wait for sixth
<-doneCh
assert.Equal(t, 5, p.NumWorkers()) // still 5
}
func TestAdaptivePool_ShrinksAgain(t *testing.T) {
t.Parallel()
ts := clock.NewEventTimeSource()
p := goro.NewAdaptivePool(ts, 1, 5, 10*time.Millisecond, 1)
defer p.Stop()
// make 3 calls to force it to grow to 3 workers
p.Do(block)
syncCh := make(chan struct{}, 10)
go p.Do(func() { syncCh <- struct{}{}; block() })
// wait for goroutine to block in Do
assert.Eventually(t, func() bool { return ts.NumTimers() == 1 }, time.Second, time.Millisecond)
ts.Advance(10 * time.Millisecond) // allow it to start another
<-syncCh // wait for it to call the function
go p.Do(func() { syncCh <- struct{}{} })
assert.Eventually(t, func() bool { return ts.NumTimers() == 1 }, time.Second, time.Millisecond)
ts.Advance(10 * time.Millisecond) // allow it to start another
<-syncCh // wait for it to call the function
assert.Equal(t, 3, p.NumWorkers())
// now there are 3 workers with one free, another call or three should start immediately
p.Do(nothing)
p.Do(nothing)
p.Do(nothing)
// after no more than 10ms, the free worker should exit
// wait until worker is blocked on timer
assert.Eventually(t, func() bool { return ts.NumTimers() == 1 }, time.Second, time.Millisecond)
ts.Advance(20 * time.Millisecond) // let timer fire
// wait for worker to exit
assert.Eventually(t, func() bool { return p.NumWorkers() == 2 }, time.Second, time.Millisecond)
}