mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-28 18:06:34 +02:00
This also replaces instances where we manually write "return ctx.Err()" with "return context.Cause(ctx)" which is functionally identical, but will also correctly propagate cause errors if present.
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package enabler_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/pomerium/pomerium/internal/enabler"
|
|
)
|
|
|
|
func TestEnabler(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("enabled immediately", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
e := enabler.New("test", enabler.HandlerFunc(func(_ context.Context) error {
|
|
return errors.New("ERROR")
|
|
}), true)
|
|
err := e.Run(context.Background())
|
|
assert.Error(t, err)
|
|
})
|
|
t.Run("enabled delayed", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
e := enabler.New("test", enabler.HandlerFunc(func(_ context.Context) error {
|
|
return errors.New("ERROR")
|
|
}), false)
|
|
time.AfterFunc(time.Millisecond*10, e.Enable)
|
|
err := e.Run(context.Background())
|
|
assert.Error(t, err)
|
|
})
|
|
t.Run("disabled", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
t.Cleanup(cancel)
|
|
|
|
var started, stopped atomic.Int64
|
|
e := enabler.New("test", enabler.HandlerFunc(func(ctx context.Context) error {
|
|
started.Add(1)
|
|
<-ctx.Done()
|
|
stopped.Add(1)
|
|
return context.Cause(ctx)
|
|
}), true)
|
|
time.AfterFunc(time.Millisecond*10, e.Disable)
|
|
go e.Run(ctx)
|
|
|
|
assert.Eventually(t, func() bool { return stopped.Load() == 1 }, time.Second, time.Millisecond*100,
|
|
"should stop RunEnabled")
|
|
|
|
e.Enable()
|
|
|
|
assert.Eventually(t, func() bool { return started.Load() == 2 }, time.Second, time.Millisecond*100,
|
|
"should re-start RunEnabled")
|
|
})
|
|
}
|