1package wayland
2
3import (
4 "context"
5 "reflect"
6 "testing"
7 "time"
8
9 "github.com/bnema/neferwl/internal/logging"
10 "github.com/bnema/neferwl/internal/ports"
11)
12
13func TestDrainCommandsCollapsesMotion(t *testing.T) {
14 cmds := make(chan ports.ClientCommand, 16)
15 cmds <- ports.PointerMotionTo{ID: 1, X: 2, DX: 0.25, UnaccelDX: 0.5}
16 cmds <- ports.PointerMotionTo{ID: 1, X: 3, DX: 0.25, DY: -0.5, UnaccelDX: 0.5, UnaccelDY: -1}
17 cmds <- ports.PointerButtonTo{ID: 1, Button: 272, Pressed: true}
18 cmds <- ports.PointerMotionTo{ID: 1, X: 4}
19 cmds <- ports.PointerFocus{ID: 2}
20 cmds <- ports.PointerMotionTo{ID: 2, X: 5}
21 cmds <- ports.PointerMotionTo{ID: 2, X: 6}
22 batch, open := drainCommands(ports.PointerMotionTo{ID: 1, X: 1, DX: 0.25, UnaccelDX: 0.5}, cmds)
23 want := []ports.ClientCommand{
24 // Slow motions add up: none of the sub-pixel deltas is lost.
25 ports.PointerMotionTo{ID: 1, X: 3, DX: 0.75, DY: -0.5, UnaccelDX: 1.5, UnaccelDY: -1},
26 ports.PointerButtonTo{ID: 1, Button: 272, Pressed: true},
27 ports.PointerMotionTo{ID: 1, X: 4},
28 ports.PointerFocus{ID: 2},
29 ports.PointerMotionTo{ID: 2, X: 6},
30 }
31 if !open || !reflect.DeepEqual(batch, want) {
32 t.Fatalf("open=%v batch=%#v", open, batch)
33 }
34 close(cmds)
35 if _, open := drainCommands(ports.PointerFocus{}, cmds); open {
36 t.Fatal("closed channel reported open")
37 }
38}
39
40// Each command used to cost one display.Do round trip (up to one 5 ms
41// event-loop dispatch), capping pointer motion at ~150/s. A 1 kHz mouse
42// must be absorbed without core blocking.
43func TestCommandThroughput(t *testing.T) {
44 cmds := make(chan ports.ClientCommand, 32)
45 s, err := New(Options{RuntimeDir: t.TempDir(), Outputs: testOutputs}, Channels{Commands: cmds}, logging.For(context.Background(), "wayland"))
46 if err != nil {
47 t.Fatal(err)
48 }
49 ctx, cancel := context.WithCancel(context.Background())
50 done := make(chan error, 1)
51 go func() { done <- s.Run(ctx) }()
52 defer func() { cancel(); <-done }()
53 start := time.Now()
54 for i := range 2000 {
55 cmds <- ports.PointerMotionTo{ID: 1, X: float64(i), Y: 1}
56 }
57 if d := time.Since(start); d > time.Second {
58 t.Fatalf("2000 motion commands took %v (%.0f/s); want well above 1000/s", d, 2000/d.Seconds())
59 }
60}