pomerium/internal/identity/manager/schedulers_test.go
Caleb Doxsey 2eaa4291ae add test
2024-04-19 15:39:34 -06:00

54 lines
1.1 KiB
Go

package manager
import (
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestUpdateUserInfoScheduler(t *testing.T) {
t.Parallel()
var mu sync.Mutex
var calls []time.Time
ctx := context.Background()
userUpdateInfoInterval := 100 * time.Millisecond
uuis := newUpdateUserInfoScheduler(ctx, userUpdateInfoInterval, func(ctx context.Context, userID string) {
mu.Lock()
calls = append(calls, time.Now())
mu.Unlock()
}, "U1")
t.Cleanup(uuis.Stop)
// should eventually trigger
assert.Eventually(t, func() bool {
mu.Lock()
n := len(calls)
mu.Unlock()
return n == 1
}, 3*userUpdateInfoInterval, userUpdateInfoInterval/10, "should trigger once")
uuis.Reset()
uuis.Reset()
uuis.Reset()
assert.Eventually(t, func() bool {
mu.Lock()
n := len(calls)
mu.Unlock()
return n == 2
}, 3*userUpdateInfoInterval, userUpdateInfoInterval/10, "should trigger once after multiple resets")
mu.Lock()
var diff time.Duration
if len(calls) >= 2 {
diff = calls[len(calls)-1].Sub(calls[len(calls)-2])
}
mu.Unlock()
assert.GreaterOrEqual(t, diff, userUpdateInfoInterval, "delay should exceed interval")
}