-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregation_test.go
92 lines (74 loc) · 1.64 KB
/
aggregation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2025 Bob Vawter ([email protected])
// SPDX-License-Identifier: Apache-2.0
package notify
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestAggregationDrain(t *testing.T) {
r := require.New(t)
agg := NewAggregation()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
vars := make([]*Var[int], 10)
for i := range vars {
vars[i] = VarOf(i)
r.Equal(i, Aggregate(agg, vars[i]))
// Ensure double-registration is a no-op.
r.Equal(i, Aggregate(agg, vars[i]))
}
r.Equal(len(vars), agg.Len())
found, ok := agg.Choose()
r.Nil(found)
r.False(ok)
ch := agg.Updated(ctx)
select {
case <-ch:
r.Fail("channel should be open")
default:
}
for i, v := range vars {
_, _, err := v.Update(func(old int) (int, error) { return old + len(vars), nil })
r.NoError(err)
select {
case <-ch:
case <-ctx.Done():
r.NoError(ctx.Err())
}
found, ok := agg.Choose()
r.True(ok)
r.Same(v, found.(*Var[int]))
r.Equal(len(vars)-i-1, agg.Len())
}
}
func TestAggregationImmediate(t *testing.T) {
r := require.New(t)
agg := NewAggregation()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
vars := make([]*Var[int], 10)
for i := range vars {
vars[i] = VarOf(i)
r.Equal(i, Aggregate(agg, vars[i]))
vars[i].Set(99)
}
r.Equal(len(vars), agg.Len())
select {
case <-agg.Updated(ctx):
default:
r.Fail("should already have a change notification")
}
count := 0
for {
found, ok := agg.Choose()
if !ok {
break
}
count++
value, _ := found.(*Var[int]).Get()
r.Equal(99, value)
}
r.Equal(len(vars), count)
}