-
Notifications
You must be signed in to change notification settings - Fork 1
/
parallel_stabilize_test.go
377 lines (298 loc) · 9.44 KB
/
parallel_stabilize_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package incr
import (
"bytes"
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/wcharczuk/go-incr/testutil"
)
func Test_ParallelStabilize(t *testing.T) {
ctx := testContext()
g := New()
v0 := Var(g, "foo")
v1 := Var(g, "bar")
m0 := Map2(g, v0, v1, func(a, b string) string {
return a + " " + b
})
_ = MustObserve(g, m0)
err := g.ParallelStabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, 0, v0.Node().setAt)
testutil.Equal(t, 0, v0.Node().changedAt)
testutil.Equal(t, 0, v1.Node().setAt)
testutil.Equal(t, 0, v1.Node().changedAt)
testutil.Equal(t, 1, m0.Node().changedAt)
testutil.Equal(t, 0, v0.Node().recomputedAt)
testutil.Equal(t, 0, v1.Node().recomputedAt)
testutil.Equal(t, 1, m0.Node().recomputedAt)
testutil.Equal(t, "foo bar", m0.Value())
v0.Set("not foo")
testutil.Equal(t, 2, v0.Node().setAt)
testutil.Equal(t, 0, v1.Node().setAt)
err = g.ParallelStabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, 2, v0.Node().changedAt)
testutil.Equal(t, 0, v1.Node().changedAt)
testutil.Equal(t, 2, m0.Node().changedAt)
testutil.Equal(t, 2, v0.Node().recomputedAt)
testutil.Equal(t, 0, v1.Node().recomputedAt)
testutil.Equal(t, 2, m0.Node().recomputedAt)
testutil.Equal(t, "not foo bar", m0.Value())
}
func Test_ParallelStabilize_alreadyStabilizing(t *testing.T) {
ctx := testContext()
graph := New()
graph.status = StatusStabilizing
err := graph.ParallelStabilize(ctx)
testutil.NotNil(t, err)
}
func Test_ParallelStabilize_jsDocs(t *testing.T) {
ctx := testContext()
g := New()
type Entry struct {
Entry string
Time time.Time
}
now := time.Date(2022, 05, 04, 12, 11, 10, 9, time.UTC)
data := []Entry{
{"0", now},
{"1", now.Add(time.Second)},
{"2", now.Add(2 * time.Second)},
{"3", now.Add(3 * time.Second)},
{"4", now.Add(4 * time.Second)},
}
i := Var(g, data)
output := Map(
g,
i,
func(entries []Entry) (output []string) {
for _, e := range entries {
if e.Time.Sub(now) > 2*time.Second {
output = append(output, e.Entry)
}
}
return
},
)
_ = MustObserve(g, output)
err := g.ParallelStabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, 2, len(output.Value()))
data = append(data, Entry{
"5", now.Add(5 * time.Second),
})
err = g.ParallelStabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, 2, len(output.Value()))
i.Set(data)
err = g.ParallelStabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, 3, len(output.Value()))
}
func Test_ParallelStabilize_error_noClear(t *testing.T) {
ctx := testContext()
g := New(
OptGraphClearRecomputeHeapOnError(false),
)
var didCallAbortedHandler bool
v0 := Var(g, "hello")
m0 := Map(g, v0, ident)
m1 := Map(g, m0, ident)
m1.Node().OnAborted(func(_ context.Context, err error) {
didCallAbortedHandler = true
})
f0 := Func(g, func(ctx context.Context) (string, error) {
return "", fmt.Errorf("this is only a test")
})
_ = MustObserve(g, f0)
_ = MustObserve(g, m1)
testutil.Equal(t, true, g.recomputeHeap.has(m1))
testutil.Equal(t, true, g.recomputeHeap.has(f0))
err := g.ParallelStabilize(ctx)
testutil.NotNil(t, err)
testutil.Equal(t, true, g.recomputeHeap.has(m1), "we should not clear the recompute heap on error")
testutil.Equal(t, false, g.recomputeHeap.has(f0))
testutil.Equal(t, false, didCallAbortedHandler)
}
func Test_ParallelStabilize_error_shouldClear(t *testing.T) {
ctx := testContext()
g := New(
OptGraphClearRecomputeHeapOnError(true),
)
var didCallAbortedHandler bool
v0 := Var(g, "hello")
m0 := Map(g, v0, ident)
m1 := Map(g, m0, ident)
m1.Node().OnAborted(func(_ context.Context, err error) {
didCallAbortedHandler = true
})
f0 := Func(g, func(ctx context.Context) (string, error) {
return "", fmt.Errorf("this is only a test")
})
_ = MustObserve(g, f0)
_ = MustObserve(g, m1)
testutil.Equal(t, true, g.recomputeHeap.has(m1))
testutil.Equal(t, true, g.recomputeHeap.has(f0))
err := g.ParallelStabilize(ctx)
testutil.NotNil(t, err)
testutil.Equal(t, false, g.recomputeHeap.has(m1), "we should clear the recompute heap on error")
testutil.Equal(t, false, g.recomputeHeap.has(f0))
testutil.Equal(t, true, didCallAbortedHandler)
}
func Test_ParallelStabilize_Always(t *testing.T) {
ctx := testContext()
g := New()
v := Var(g, "foo")
m0 := Map(g, v, ident)
a := Always(g, m0)
m1 := Map(g, a, ident)
var updates int
m1.Node().OnUpdate(func(_ context.Context) {
updates++
})
o := MustObserve(g, m1)
_ = g.ParallelStabilize(ctx)
testutil.Equal(t, "foo", o.Value())
testutil.Equal(t, 1, updates)
_ = g.ParallelStabilize(ctx)
testutil.Equal(t, "foo", o.Value())
testutil.Equal(t, 2, updates)
v.Set("bar")
_ = g.ParallelStabilize(ctx)
testutil.Equal(t, "bar", o.Value())
testutil.Equal(t, 3, updates)
}
func Test_ParallelStabilize_always_cutoff(t *testing.T) {
ctx := testContext()
g := New()
filename := Var(g, "test")
filenameAlways := Always(g, filename)
modtime := 1
statfile := Map(g, filenameAlways, func(s string) int { return modtime })
statfileCutoff := Cutoff(g, statfile, func(ov, nv int) bool {
return ov == nv
})
readFile := Map2(g, filename, statfileCutoff, func(p string, mt int) string {
return fmt.Sprintf("%s-%d", p, mt)
})
o := MustObserve(g, readFile)
err := g.ParallelStabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, "test-1", o.Value())
err = g.ParallelStabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, "test-1", o.Value())
modtime = 2
err = g.ParallelStabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, "test-2", o.Value())
err = g.ParallelStabilize(ctx)
testutil.Nil(t, err)
testutil.Equal(t, "test-2", o.Value())
}
func Test_ParallelStabilize_always_cutoff_error(t *testing.T) {
ctx := testContext()
g := New()
filename := Var(g, "test")
filenameAlways := Always(g, filename)
modtime := 1
statfile := Map(g, filenameAlways, func(s string) int { return modtime })
statfileCutoff := CutoffContext(g, statfile, func(_ context.Context, ov, nv int) (bool, error) {
return false, fmt.Errorf("this is only a test")
})
readFile := Map2(g, filename, statfileCutoff, func(p string, mt int) string {
return fmt.Sprintf("%s-%d", p, mt)
})
o := MustObserve(g, readFile)
err := g.ParallelStabilize(ctx)
testutil.NotNil(t, err)
testutil.Equal(t, "", o.Value())
testutil.Equal(t, 2, g.recomputeHeap.len(), "we should clear the recompute heap on error")
}
func Test_ParallelStabilize_printsErrors(t *testing.T) {
ctx := context.Background()
g := New()
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)
ctx = WithTracingOutputs(ctx, outBuf, errBuf)
v0 := Var(g, "hello")
gonnaPanic := MapContext(g, v0, func(_ context.Context, _ string) (string, error) {
return "", fmt.Errorf("this is only a test")
})
_ = MustObserve(g, gonnaPanic)
err := g.ParallelStabilize(ctx)
testutil.NotNil(t, err)
testutil.NotEqual(t, 0, len(outBuf.String()))
testutil.NotEqual(t, 0, len(errBuf.String()))
testutil.Equal(t, true, strings.Contains(errBuf.String(), "this is only a test"))
}
func Test_ParallelStabilize_preRequisite_heightsAreParallel(t *testing.T) {
g := New()
v0 := Var(g, "0")
v1 := Var(g, "1")
v2 := Var(g, "2")
v3 := Var(g, "3")
v4 := Var(g, "4")
v5 := Var(g, "5")
v6 := Var(g, "6")
v7 := Var(g, "7")
m00 := Map2(g, v0, v1, concat)
m01 := Map2(g, v2, v3, concat)
m02 := Map2(g, v4, v5, concat)
m03 := Map2(g, v6, v7, concat)
m10 := Map2(g, m00, m01, concat)
m11 := Map2(g, m02, m03, concat)
m20 := Map2(g, m10, m11, concat)
o, err := Observe(g, m20)
testutil.NoError(t, err)
testutil.Equal(t, 0, v0.Node().height)
testutil.Equal(t, HeightUnset, v0.Node().heightInRecomputeHeap)
testutil.Equal(t, 0, v1.Node().height)
testutil.Equal(t, HeightUnset, v1.Node().heightInRecomputeHeap)
testutil.Equal(t, 0, v2.Node().height)
testutil.Equal(t, HeightUnset, v2.Node().heightInRecomputeHeap)
testutil.Equal(t, 0, v3.Node().height)
testutil.Equal(t, HeightUnset, v3.Node().heightInRecomputeHeap)
testutil.Equal(t, 0, v4.Node().height)
testutil.Equal(t, HeightUnset, v4.Node().heightInRecomputeHeap)
testutil.Equal(t, 0, v5.Node().height)
testutil.Equal(t, HeightUnset, v5.Node().heightInRecomputeHeap)
testutil.Equal(t, 0, v6.Node().height)
testutil.Equal(t, HeightUnset, v6.Node().heightInRecomputeHeap)
testutil.Equal(t, 0, v7.Node().height)
testutil.Equal(t, HeightUnset, v7.Node().heightInRecomputeHeap)
testutil.Equal(t, 1, m00.Node().height)
testutil.Equal(t, 1, m00.Node().heightInRecomputeHeap)
testutil.Equal(t, 1, m01.Node().height)
testutil.Equal(t, 1, m01.Node().heightInRecomputeHeap)
testutil.Equal(t, 1, m02.Node().height)
testutil.Equal(t, 1, m02.Node().heightInRecomputeHeap)
testutil.Equal(t, 1, m03.Node().height)
testutil.Equal(t, 1, m03.Node().heightInRecomputeHeap)
testutil.Equal(t, 2, m10.Node().height)
testutil.Equal(t, 2, m10.Node().heightInRecomputeHeap)
testutil.Equal(t, 2, m11.Node().height)
testutil.Equal(t, 2, m11.Node().heightInRecomputeHeap)
testutil.Equal(t, 3, m20.Node().height)
testutil.Equal(t, 3, m20.Node().heightInRecomputeHeap)
_ = g.ParallelStabilize(testContext())
testutil.NotEqual(t, "", o.Value())
}
func Test_ParallelStabilize_alwaysInRecomputeHeapOnError(t *testing.T) {
g := New()
v0 := Var(g, "foo")
coa := cutoffAlways(g, v0,
func(_ context.Context, _ string) (bool, error) {
return false, fmt.Errorf("this is only a test")
},
func(_ context.Context, i string) (string, error) {
return i + "-bar", nil
},
)
_, _ = Observe(g, coa)
err := g.ParallelStabilize(testContext())
testutil.Error(t, err)
testutil.Equal(t, "this is only a test", err.Error())
}