2024-03-09 20:00:12 +03:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-03-09 22:52:24 +03:00
|
|
|
func TestWaitGroupContext(t *testing.T) {
|
2024-03-09 20:00:12 +03:00
|
|
|
wg := NewWaitGroup()
|
|
|
|
_ = t
|
|
|
|
wg.Add(1)
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
wg.WaitContext(ctx)
|
|
|
|
}
|
2024-03-09 22:52:24 +03:00
|
|
|
|
|
|
|
func TestWaitGroupReuse(t *testing.T) {
|
|
|
|
wg := NewWaitGroup()
|
|
|
|
defer func() {
|
|
|
|
if wg.Waiters() != 0 {
|
|
|
|
t.Fatal("lost goroutines")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
defer wg.Done()
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
wg.WaitContext(ctx)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
defer wg.Done()
|
|
|
|
ctx, cancel = context.WithTimeout(context.TODO(), 1*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
wg.WaitContext(ctx)
|
|
|
|
}
|