Fix rendering notifications from the state at event time (#32401)

This commit is contained in:
andig 2026-08-01 16:37:20 +02:00 • committed by GitHub
parent ad255ff54c
commit a4da257bb5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 52 additions and 33 deletions

View file

@ -31,15 +31,9 @@ type ParamCache struct {
val map[string]Param
}
// flush is the value type used as parameter for flushing the cache.
// Flushing is implemented by closing the channel. At this time, it is guaranteed
// that the cache has catched up processing all pending messages.
type flush chan struct{}
// Flusher returns a new flush channel
func Flusher() flush {
return make(flush)
}
// Snapshot requests a copy of the cache state at the parameter's position in
// the stream. It runs on the cache's goroutine and must not block for long.
type Snapshot func([]Param)
// NewCache creates cache
func NewParamCache() *ParamCache {
@ -51,8 +45,8 @@ func NewParamCache() *ParamCache {
// Run adds input channel's values to cache
func (c *ParamCache) Run(in <-chan Param) {
for p := range in {
if flushC, ok := p.Val.(flush); ok {
close(flushC)
if snapshot, ok := p.Val.(Snapshot); ok {
snapshot(c.All())
continue
}

View file

@ -21,3 +21,20 @@ func TestParam(t *testing.T) {
func TestParamCache(t *testing.T) {
NewParamCache().Add("foo", Param{})
}
func TestParamCacheSnapshot(t *testing.T) {
in := make(chan Param)
go NewParamCache().Run(in)
in <- Param{Key: "before", Val: 1}
res := make(chan []Param, 1)
in <- Param{Val: Snapshot(func(state []Param) { res <- state })}
// published after the snapshot request, must not be included
in <- Param{Key: "after", Val: 2}
state := <-res
assert.Len(t, state, 1)
assert.Equal(t, "before", state[0].Key)
}