mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-30 15:00:51 +02:00
core/config: refactor change dispatcher (#4657)
* core/config: refactor change dispatcher * update test * close listener go routine when context is canceled * use cancel cause * use context * add more time * more time
This commit is contained in:
parent
53573dc046
commit
e0693e54f0
4 changed files with 241 additions and 14 deletions
53
internal/events/target_test.go
Normal file
53
internal/events/target_test.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package events_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/events"
|
||||
)
|
||||
|
||||
func TestTarget(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var target events.Target[int64]
|
||||
t.Cleanup(target.Close)
|
||||
|
||||
var calls1, calls2, calls3 atomic.Int64
|
||||
h1 := target.AddListener(func(_ context.Context, i int64) {
|
||||
calls1.Add(i)
|
||||
})
|
||||
h2 := target.AddListener(func(_ context.Context, i int64) {
|
||||
calls2.Add(i)
|
||||
})
|
||||
h3 := target.AddListener(func(_ context.Context, i int64) {
|
||||
calls3.Add(i)
|
||||
})
|
||||
|
||||
shouldBe := func(i1, i2, i3 int64) {
|
||||
t.Helper()
|
||||
|
||||
assert.Eventually(t, func() bool { return calls1.Load() == i1 }, time.Second, time.Millisecond)
|
||||
assert.Eventually(t, func() bool { return calls2.Load() == i2 }, time.Second, time.Millisecond)
|
||||
assert.Eventually(t, func() bool { return calls3.Load() == i3 }, time.Second, time.Millisecond)
|
||||
}
|
||||
|
||||
target.Dispatch(context.Background(), 1)
|
||||
shouldBe(1, 1, 1)
|
||||
|
||||
target.RemoveListener(h2)
|
||||
target.Dispatch(context.Background(), 2)
|
||||
shouldBe(3, 1, 3)
|
||||
|
||||
target.RemoveListener(h1)
|
||||
target.Dispatch(context.Background(), 3)
|
||||
shouldBe(3, 1, 6)
|
||||
|
||||
target.RemoveListener(h3)
|
||||
target.Dispatch(context.Background(), 4)
|
||||
shouldBe(3, 1, 6)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue