1package capture
2
3import (
4 "context"
5 "image"
6 "testing"
7 "time"
8
9 "github.com/bnema/neferwl/internal/ports"
10 "github.com/stretchr/testify/require"
11)
12
13func fenceScene(scale float64, w, h int, marks ...ports.CaptureIndicator) ports.Scene {
14 return ports.Scene{Scale: scale, OutputWidth: w, OutputHeight: h, CaptureIndicators: marks}
15}
16
17func fenceRequest(id uint64, region image.Rectangle, since time.Time) ports.CaptureRequest {
18 return ports.CaptureRequest{ID: id, Region: region, Indicate: true, Since: since}
19}
20
21// A request is served only from a scene that shows its mark: a border that
22// contains the target on this output, or a pill for a workspace rendered for
23// capture only. Requests that ask for none need none.
24func TestIndicatorShown(t *testing.T) {
25 whole := image.Rect(0, 0, 200, 100)
26 border := func(r ports.Rect) ports.CaptureIndicator { return ports.CaptureIndicator{Rect: r} }
27 pill := ports.CaptureIndicator{Pill: true, Rect: ports.Rect{X: 180, Y: 8, W: 12, H: 12}}
28 for _, tc := range []struct {
29 name string
30 scene ports.Scene
31 req ports.CaptureRequest
32 want bool
33 }{
34 {"no indicator", fenceScene(1, 200, 100), fenceRequest(1, whole, time.Time{}), false},
35 {"request without a mark to show", fenceScene(1, 200, 100), ports.CaptureRequest{ID: 1, Region: whole}, true},
36 {"whole output border", fenceScene(1, 200, 100, border(ports.Rect{W: 200, H: 100})), fenceRequest(1, whole, time.Time{}), true},
37 {"pill is not a border", fenceScene(1, 200, 100, pill), fenceRequest(1, whole, time.Time{}), false},
38 {"region border", fenceScene(1, 200, 100, border(ports.Rect{X: 10, Y: 10, W: 20, H: 20})), fenceRequest(1, image.Rect(10, 10, 30, 30), time.Time{}), true},
39 {"border of another region", fenceScene(1, 200, 100, border(ports.Rect{X: 50, Y: 10, W: 20, H: 20})), fenceRequest(1, image.Rect(10, 10, 30, 30), time.Time{}), false},
40 {"smaller border than the region", fenceScene(1, 200, 100, border(ports.Rect{X: 10, Y: 10, W: 10, H: 10})), fenceRequest(1, image.Rect(10, 10, 30, 30), time.Time{}), false},
41 {"larger border contains the region", fenceScene(1, 200, 100, border(ports.Rect{W: 200, H: 100})), fenceRequest(1, image.Rect(10, 10, 30, 30), time.Time{}), true},
42 {"scaled region rounds outwards", fenceScene(1.5, 200, 100, border(ports.Rect{X: 1, Y: 1, W: 3, H: 3})), fenceRequest(1, image.Rect(1, 1, 6, 6), time.Time{}), true},
43 {"scaled region outside", fenceScene(1.5, 200, 100, border(ports.Rect{X: 1, Y: 1, W: 3, H: 3})), fenceRequest(1, image.Rect(1, 1, 7, 6), time.Time{}), false},
44 {"edge at the output edge reaches it", fenceScene(1.5, 201, 100, border(ports.Rect{X: 0, Y: 0, W: 201, H: 100})), fenceRequest(1, image.Rect(0, 0, 302, 150), time.Time{}), true},
45 {"undrawable thin border shows nothing", fenceScene(1, 200, 100, border(ports.Rect{X: 10, Y: 10, W: 1, H: 20})), fenceRequest(1, image.Rect(10, 10, 11, 30), time.Time{}), false},
46 {"undrawable flat border shows nothing", fenceScene(1, 200, 100, border(ports.Rect{X: 10, Y: 10, W: 20, H: 1})), fenceRequest(1, image.Rect(10, 10, 30, 11), time.Time{}), false},
47 {"smallest drawable border", fenceScene(1, 200, 100, border(ports.Rect{X: 10, Y: 10, W: 2, H: 2})), fenceRequest(1, image.Rect(10, 10, 12, 12), time.Time{}), true},
48 {"pill cut by a short output shows nothing", fenceScene(1, 200, 8, ports.CaptureIndicator{Pill: true, Rect: ports.Rect{X: 180, Y: 8, W: 12, H: 12}}), ports.CaptureRequest{ID: 1, Indicate: true, OffScreen: true, Workspace: 3, Region: whole}, false},
49 {"pill partly outside shows nothing", fenceScene(1, 200, 100, ports.CaptureIndicator{Pill: true, Rect: ports.Rect{X: 195, Y: 8, W: 12, H: 12}}), ports.CaptureRequest{ID: 1, Indicate: true, OffScreen: true, Workspace: 3, Region: whole}, false},
50 {"whole-output pill of a tiny output", fenceScene(1, 200, 8, ports.CaptureIndicator{Pill: true, Rect: ports.Rect{W: 200, H: 8}}), ports.CaptureRequest{ID: 1, Indicate: true, OffScreen: true, Workspace: 3, Region: whole}, true},
51 {"empty pill shows nothing", fenceScene(1, 200, 100, ports.CaptureIndicator{Pill: true}), ports.CaptureRequest{ID: 1, Indicate: true, OffScreen: true, Workspace: 3, Region: whole}, false},
52 {"off-screen needs the pill", fenceScene(1, 200, 100, pill), ports.CaptureRequest{ID: 1, Indicate: true, OffScreen: true, Workspace: 3, Region: whole}, true},
53 {"off-screen border is not its mark", fenceScene(1, 200, 100, border(ports.Rect{W: 200, H: 100})), ports.CaptureRequest{ID: 1, Indicate: true, OffScreen: true, Workspace: 3, Region: whole}, false},
54 } {
55 t.Run(tc.name, func(t *testing.T) {
56 require.Equal(t, tc.want, IndicatorShown(tc.scene, tc.req))
57 })
58 }
59}
60
61// A request waits HoldFor for its indicator, then fails closed; one whose
62// indicator shows is never failed. No allocation.
63func TestExpireFailsOnlyTheRequestsWhoseIndicatorNeverCame(t *testing.T) {
64 replies := make(chan ports.CaptureDone, 4)
65 p := NewPipeline(context.Background(), replies)
66 defer p.Close(nil)
67 now := time.Unix(100, 0)
68 s := fenceScene(1, 200, 100, ports.CaptureIndicator{Rect: ports.Rect{W: 200, H: 100}})
69 shown := fenceRequest(1, image.Rect(0, 0, 200, 100), now.Add(-time.Hour))
70 late := fenceRequest(2, image.Rect(0, 0, 200, 100), now.Add(-time.Hour))
71 late.OffScreen = true
72 young := fenceRequest(3, image.Rect(0, 0, 200, 100), now.Add(-HoldFor/2))
73 young.OffScreen = true
74 kept, wait := p.Expire(s, []ports.CaptureRequest{shown, late, young}, now)
75 require.Equal(t, []uint64{1, 3}, ids(kept))
76 require.Equal(t, HoldFor/2, wait)
77 d := <-replies
78 require.Equal(t, uint64(2), d.ID)
79 require.ErrorIs(t, d.Err, ErrIndicatorMissing)
80 // Nothing waits: no timer needed.
81 _, wait = p.Expire(s, []ports.CaptureRequest{shown}, now)
82 require.Zero(t, wait)
83
84 reqs := []ports.CaptureRequest{young, shown}
85 if n := testing.AllocsPerRun(100, func() { _, _ = p.Expire(s, reqs, now) }); n != 0 {
86 t.Fatalf("%v allocations", n)
87 }
88}
89
90// Hold hands over the requests whose indicator the scene shows and keeps the
91// others, in place.
92func TestHoldSplitsByIndicator(t *testing.T) {
93 p := NewPipeline(context.Background(), make(chan ports.CaptureDone, 4))
94 defer p.Close(nil)
95 now := time.Unix(100, 0)
96 none := fenceScene(1, 200, 100)
97 with := fenceScene(1, 200, 100, ports.CaptureIndicator{Rect: ports.Rect{W: 200, H: 100}})
98 a := fenceRequest(1, image.Rect(0, 0, 200, 100), now)
99 b := fenceRequest(2, image.Rect(0, 0, 200, 100), now)
100 reqs := []ports.CaptureRequest{a, b}
101 ready, held := p.Hold(none, reqs, now)
102 require.Empty(t, ready)
103 require.Equal(t, []uint64{1, 2}, ids(held))
104 ready, held = p.Hold(with, reqs, now)
105 require.Equal(t, []uint64{1, 2}, ids(ready))
106 require.Empty(t, held)
107 if n := testing.AllocsPerRun(100, func() { _, _ = p.Hold(with, reqs, now) }); n != 0 {
108 t.Fatalf("%v allocations", n)
109 }
110}
111
112// Waiting keeps what was neither handed over nor shown.
113func TestWaitingKeepsOnlyHeldRequests(t *testing.T) {
114 now := time.Unix(100, 0)
115 with := fenceScene(1, 200, 100, ports.CaptureIndicator{Rect: ports.Rect{W: 200, H: 100}})
116 held := fenceRequest(3, image.Rect(0, 0, 200, 100), now)
117 held.OffScreen = true
118 reqs := []ports.CaptureRequest{{}, held, fenceRequest(4, image.Rect(0, 0, 200, 100), now)}
119 got := Waiting(reqs, with)
120 require.Equal(t, []uint64{3}, ids(got))
121 require.Equal(t, 3, cap(reqs[:cap(got)]), "the owner's storage is kept")
122 require.True(t, Handed(reqs[1]) && Handed(reqs[2]), "entries past the kept ones are zeroed")
123}
124
125// A scale or mode change while a request is held: the request keeps its
126// physical region, the fence judges it against the scene that will draw it.
127// Whatever the new scene draws, the region is served only when the drawn mark
128// covers it in that scene's pixels; otherwise it stays held and fails after
129// HoldFor. Nothing is served unindicated.
130func TestHeldRequestAcrossScaleAndModeChange(t *testing.T) {
131 replies := make(chan ports.CaptureDone, 4)
132 p := NewPipeline(context.Background(), replies)
133 defer p.Close(nil)
134 now := time.Unix(100, 0)
135 region := image.Rect(10, 10, 30, 30) // physical, asked at scale 1
136 q := fenceRequest(1, region, now)
137 border := func(r ports.Rect) ports.CaptureIndicator { return ports.CaptureIndicator{Rect: r} }
138 old := fenceScene(1, 200, 100, border(ports.Rect{X: 10, Y: 10, W: 20, H: 20}))
139 require.True(t, IndicatorShown(old, q))
140
141 for _, tc := range []struct {
142 name string
143 scene ports.Scene
144 shown bool
145 }{
146 {"scale doubled, mark of the old logical region", fenceScene(2, 200, 100, border(ports.Rect{X: 10, Y: 10, W: 20, H: 20})), false},
147 {"scale doubled, mark re-derived for the region", fenceScene(2, 200, 100, border(ports.Rect{X: 5, Y: 5, W: 10, H: 10})), true},
148 {"scale halved, old mark too small in pixels", fenceScene(0.5, 200, 100, border(ports.Rect{X: 10, Y: 10, W: 20, H: 20})), false},
149 {"mode change, mark of the new whole output", fenceScene(1, 400, 200, border(ports.Rect{W: 400, H: 200})), true},
150 {"mode change, no mark yet", fenceScene(1, 400, 200), false},
151 } {
152 t.Run(tc.name, func(t *testing.T) {
153 require.Equal(t, tc.shown, IndicatorShown(tc.scene, q))
154 reqs := []ports.CaptureRequest{q}
155 ready, held := p.Hold(tc.scene, reqs, now)
156 if tc.shown {
157 require.Equal(t, []uint64{1}, ids(ready))
158 return
159 }
160 require.Empty(t, ready)
161 require.Equal(t, []uint64{1}, ids(held))
162 // Still held before HoldFor; failed after, never served.
163 kept, wait := p.Expire(tc.scene, held, now.Add(HoldFor/2))
164 require.Len(t, kept, 1)
165 require.Positive(t, wait)
166 kept, _ = p.Expire(tc.scene, kept, now.Add(HoldFor))
167 require.Empty(t, kept)
168 d := <-replies
169 require.ErrorIs(t, d.Err, ErrIndicatorMissing)
170 })
171 }
172}