fix: prevent data race with cursedRenderer.onMouse (#1691)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- closes #1690 - adds `-race`, `-count`, and `-cpu` - make all viable tests parallel
authored by Liam Stanley · committed by GitHub · parent 640d879 · Browse files · 10 files·+137 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Liam Stanley <liam@liam.sh>
Diff stats
10 files+137 added−10 removed
- other:+137 added−10 removed
Provenance
- Commit
c60f0c5- Tree
2a94d0e- Author
Liam Stanley <liam@liam.sh>- Committer
GitHub <noreply@github.com>- Parent
640d879
10 files changed+137 added−10 removed
modified:examples/go.mod+1 added-1 removed
21 unmodified lines22 require (23 github.com/alecthomas/chroma/v2 v2.14.0 // indirect24 github.com/atotto/clipboard v0.1.4 // indirect25 github.com/aymerick/douceur v0.2.0 // indirect26- github.com/charmbracelet/ultraviolet v0.0.0-20260413133134-73592393e1ad // indirect26+ github.com/charmbracelet/ultraviolet v0.0.0-20260416155717-489999b90468 // indirect27 github.com/charmbracelet/x/exp/slice v0.0.0-20250327172914-2fdc97757edf // indirect28 github.com/charmbracelet/x/termios v0.1.1 // indirect29 github.com/charmbracelet/x/windows v0.2.2 // indirect30 github.com/clipperhouse/displaywidth v0.11.0 // indirect17 unmodified linesmodified:examples/go.sum+2 added-2 removed
20 unmodified lines21 github.com/charmbracelet/colorprofile v0.4.3 h1:QPa1IWkYI+AOB+fE+mg/5/4HRMZcaXex9t5KX76i20Q=22 github.com/charmbracelet/colorprofile v0.4.3/go.mod h1:/zT4BhpD5aGFpqQQqw7a+VtHCzu+zrQtt1zhMt9mR4Q=23 github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=24 github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=25-github.com/charmbracelet/ultraviolet v0.0.0-20260413133134-73592393e1ad h1:kxhuAdAdSGSiAadbEjP7v06nCb17jXjjqF2QOX+0QiE=26-github.com/charmbracelet/ultraviolet v0.0.0-20260413133134-73592393e1ad/go.mod h1:bAAz7dh/FTYfC+oiHavL4mX1tOIBZ0ZwYjSi3qE6ivM=25+github.com/charmbracelet/ultraviolet v0.0.0-20260416155717-489999b90468 h1:Q9fO0y1Zo5KB/5Vu8JZoLGm1N3RzF9bNj3Ao3xoR+Ac=26+github.com/charmbracelet/ultraviolet v0.0.0-20260416155717-489999b90468/go.mod h1:bAAz7dh/FTYfC+oiHavL4mX1tOIBZ0ZwYjSi3qE6ivM=27 github.com/charmbracelet/x/ansi v0.11.7 h1:kzv1kJvjg2S3r9KHo8hDdHFQLEqn4RBCb39dAYC84jI=28 github.com/charmbracelet/x/ansi v0.11.7/go.mod h1:9qGpnAVYz+8ACONkZBUWPtL7lulP9No6p1epAihUZwQ=29 github.com/charmbracelet/x/exp/charmtone v0.0.0-20250602192518-9e722df69bbb h1:oTM8tZxV7FY0ehvYjFuICouuhzE08UZYNqUIp/lDQdY=30 github.com/charmbracelet/x/exp/charmtone v0.0.0-20250602192518-9e722df69bbb/go.mod h1:T9jr8CzFpjhFVHjNjKwbAD7KwBNyFnj2pntAO7F2zw0=57 unmodified linesmodified:commands_test.go+8 added
4 unmodified lines5 "time"6 )7 8 func TestEvery(t *testing.T) {9+ t.Parallel()10 expected := "every ms"11 msg := Every(time.Millisecond, func(t time.Time) Msg {12 return expected13 })()2 unmodified lines16 }17 }18 19 func TestTick(t *testing.T) {20+ t.Parallel()21 expected := "tick"22 msg := Tick(time.Millisecond, func(t time.Time) Msg {23 return expected24 })()2 unmodified lines27 }28 }29 30 func TestBatch(t *testing.T) {31+ t.Parallel()32 testMultipleCommands[BatchMsg](t, Batch)33 }34 35 func TestSequence(t *testing.T) {36+ t.Parallel()37 testMultipleCommands[sequenceMsg](t, Sequence)38 }39 40 func testMultipleCommands[T ~[]Cmd](t *testing.T, createFn func(cmd ...Cmd) Cmd) {41 t.Run("nil cmd", func(t *testing.T) {42+ t.Parallel()43 if b := createFn(nil); b != nil {44 t.Fatalf("expected nil, got %+v", b)45 }46 })47 t.Run("empty cmd", func(t *testing.T) {48+ t.Parallel()49 if b := createFn(); b != nil {50 t.Fatalf("expected nil, got %+v", b)51 }52 })53 t.Run("single cmd", func(t *testing.T) {54+ t.Parallel()55 b := createFn(Quit)()56 if _, ok := b.(QuitMsg); !ok {57 t.Fatalf("expected a QuitMsg, got %T", b)58 }59 })60 t.Run("mixed nil cmds", func(t *testing.T) {61+ t.Parallel()62 b := createFn(nil, Quit, nil, Quit, nil, nil)()63 if l := len(b.(T)); l != 2 {64 t.Fatalf("expected a []Cmd with len 2, got %d", l)65 }2 unmodified linesadded:cursed_renderer_test.go+80 added
1+package tea2+3+import (4+ "fmt"5+ "io"6+ "testing"7+ "time"8+)9+10+type mouseRaceModel struct {11+ i int12+}13+14+func (m *mouseRaceModel) Init() Cmd { return nil }15+16+func (m *mouseRaceModel) Update(msg Msg) (Model, Cmd) {17+ switch msg.(type) {18+ case MouseClickMsg, MouseMotionMsg, MouseWheelMsg:19+ m.i++20+ }21+ return m, nil22+}23+24+func (m *mouseRaceModel) View() View {25+ return View{26+ Content: fmt.Sprintf("tick-%d\n", m.i),27+ MouseMode: MouseModeCellMotion,28+ }29+}30+31+// Fixes: https://github.com/charmbracelet/bubbletea/issues/169032+func TestCursedRenderer_mouseVsFlush(t *testing.T) {33+ t.Parallel()34+35+ pr, pw := io.Pipe()36+ defer func() { _ = pw.Close() }()37+38+ m := &mouseRaceModel{}39+ p := NewProgram(40+ m,41+ WithContext(t.Context()),42+ WithInput(pr),43+ WithOutput(io.Discard),44+ WithEnvironment([]string{45+ "TERM=xterm-256color",46+ "TERM_PROGRAM=Apple_Terminal",47+ }),48+ WithoutSignals(),49+ WithWindowSize(80, 24),50+ )51+52+ runDone := make(chan struct{})53+ go func() {54+ defer close(runDone)55+ _, _ = p.Run()56+ }()57+58+ time.Sleep(150 * time.Millisecond)59+60+ const iterations = 10061+ for i := range iterations {62+ switch i % 4 {63+ case 0:64+ p.Send(MouseClickMsg{X: i % 80, Y: i % 24, Button: MouseLeft})65+ case 1:66+ p.Send(MouseMotionMsg{X: i % 80, Y: i % 24})67+ case 2:68+ p.Send(MouseWheelMsg{X: 0, Y: 0, Button: MouseWheelUp})69+ default:70+ p.Send(MouseReleaseMsg{X: i % 80, Y: i % 24, Button: MouseLeft})71+ }72+ }73+74+ p.Quit()75+ select {76+ case <-runDone:77+ case <-time.After(5 * time.Second):78+ t.Fatal("program did not exit after Quit")79+ }80+}modified:cursed_renderer.go+8 added-2 removed
762 unmodified lines763 }764 765 // onMouse implements renderer.766 func (s *cursedRenderer) onMouse(m MouseMsg) Cmd {767- if s.lastView != nil && s.lastView.OnMouse != nil {768- return s.lastView.OnMouse(m)767+ var onMouse func(MouseMsg) Cmd768+ s.mu.Lock()769+ if s.lastView != nil {770+ onMouse = s.lastView.OnMouse771 }772+ s.mu.Unlock()773+ if onMouse != nil {774+ return onMouse(m)775+ }776 return nil777 }778 779 func setProgressBar(s *cursedRenderer, pb *ProgressBar) {81 unmodified linesmodified:exec_test.go+2 added
88 unmodified lines89 }90 91 for _, test := range tests {92 t.Run(test.name, func(t *testing.T) {93+ t.Parallel()94 var buf bytes.Buffer95 var in bytes.Buffer96 97 m := &testExecModel{cmd: test.cmd}20 unmodified lines118 }119 }120 121 func TestTeaExecWithNilInput(t *testing.T) {122+ t.Parallel()123 var buf bytes.Buffer124 125 m := &testExecNoInputModel{}126 p := NewProgram(m,11 unmodified linesmodified:options_test.go+9 added
8 unmodified lines9 )10 11 func TestOptions(t *testing.T) {12 t.Run("output", func(t *testing.T) {13+ t.Parallel()14 var b bytes.Buffer15 p := NewProgram(nil, WithOutput(&b))16 if f, ok := p.output.(*os.File); ok {17 t.Errorf("expected output to custom, got %v", f.Fd())18 }19 })20 21 t.Run("renderer", func(t *testing.T) {22+ t.Parallel()23 p := NewProgram(nil, WithoutRenderer())24 if !p.disableRenderer {25 t.Errorf("expected renderer to be a nilRenderer, got %v", p.renderer)26 }27 })28 29 t.Run("without signals", func(t *testing.T) {30+ t.Parallel()31 p := NewProgram(nil, WithoutSignals())32 if atomic.LoadUint32(&p.ignoreSignals) == 0 {33 t.Errorf("ignore signals should have been set")34 }35 })36 37 t.Run("filter", func(t *testing.T) {38+ t.Parallel()39 p := NewProgram(nil, WithFilter(func(_ Model, msg Msg) Msg { return msg }))40 if p.filter == nil {41 t.Errorf("expected filter to be set")42 }43 })44 45 t.Run("external context", func(t *testing.T) {46+ t.Parallel()47 extCtx, extCancel := context.WithCancel(context.Background())48 defer extCancel()49 50 p := NewProgram(nil, WithContext(extCtx))8 unmodified lines59 fn(p)60 }61 62 t.Run("nil input", func(t *testing.T) {63+ t.Parallel()64 exercise(t, WithInput(nil), func(p *Program) {65 if !p.disableInput || p.input != nil {66 t.Errorf("expected input to be disabled, got %v", p.input)67 }68 })69 })70 71 t.Run("custom input", func(t *testing.T) {72+ t.Parallel()73 var b bytes.Buffer74 exercise(t, WithInput(&b), func(p *Program) {75 if p.input != &b {76 t.Errorf("expected input to be custom, got %v", p.input)8 unmodified lines85 fn(p)86 }87 88 t.Run("without catch panics", func(t *testing.T) {89+ t.Parallel()90 exercise(t, WithoutCatchPanics(), func(p *Program) {91 if !p.disableCatchPanics {92 t.Errorf("expected catch panics to be disabled")93 }94 })95 })96 97 t.Run("without signal handler", func(t *testing.T) {98+ t.Parallel()99 exercise(t, WithoutSignalHandler(), func(p *Program) {100 if !p.disableSignalHandler {101 t.Errorf("expected signal handler to be disabled")102 }4 unmodified linesmodified:screen_test.go+2 added
130 unmodified lines131 }132 133 for _, test := range tests {134 t.Run(test.name, func(t *testing.T) {135+ t.Parallel()136 var buf bytes.Buffer137 var in bytes.Buffer138 139 m := &testViewModel{testModel: &testModel{}}39 unmodified lines179 }180 181 for _, test := range tests {182 t.Run(test.name, func(t *testing.T) {183+ t.Parallel()184 var buf bytes.Buffer185 var in bytes.Buffer186 187 m := &testModel{}20 unmodified linesmodified:Taskfile.yaml+2 added-2 removed
1 # https://taskfile.dev2 3-version: '3'3+version: "3"4 5 tasks:6 lint:7 desc: Run lint2 unmodified lines10 11 test:12 desc: Run tests13 cmds:14- - go test ./... {{.CLI_ARGS}}14+ - go test -race -count 4 -cpu 1,4 ./... {{.CLI_ARGS}}modified:tea_test.go+23 added-3 removed
2 unmodified lines3 import (4 "bytes"5 "context"6 "errors"7+ "fmt"8 "io"9 "strings"10 "sync"11 "sync/atomic"54 unmodified lines66 return NewView("success")67 }68 69 func TestTeaModel(t *testing.T) {70+ t.Parallel()71 var buf bytes.Buffer72 var in bytes.Buffer73 in.Write([]byte("q"))74 14 unmodified lines89 }90 }91 92 func TestTeaQuit(t *testing.T) {93+ t.Parallel()94 var buf bytes.Buffer95 var in bytes.Buffer96 97 m := &testModel{}16 unmodified lines114 }115 }116 117 func TestTeaWaitQuit(t *testing.T) {118+ t.Parallel()119 var buf bytes.Buffer120 var in bytes.Buffer121 122 progStarted := make(chan struct{})45 unmodified lines168 }169 }170 171 func TestTeaWaitKill(t *testing.T) {172+ t.Parallel()173 var buf bytes.Buffer174 var in bytes.Buffer175 176 progStarted := make(chan struct{})45 unmodified lines222 }223 }224 225 func TestTeaWithFilter(t *testing.T) {221- testTeaWithFilter(t, 0)222- testTeaWithFilter(t, 1)223- testTeaWithFilter(t, 2)226+ for _, preventCount := range []uint32{0, 1, 2} {227+ t.Run(fmt.Sprintf("prevent_%d", preventCount), func(t *testing.T) {228+ t.Parallel()229+ testTeaWithFilter(t, preventCount)230+ })231+ }232 }233 234 func testTeaWithFilter(t *testing.T, preventCount uint32) {235 var buf bytes.Buffer31 unmodified lines267 }268 }269 270 func TestTeaKill(t *testing.T) {271+ t.Parallel()272 var buf bytes.Buffer273 var in bytes.Buffer274 275 m := &testModel{}24 unmodified lines300 }301 }302 303 func TestTeaContext(t *testing.T) {304+ t.Parallel()305 ctx, cancel := context.WithCancel(t.Context())306 var buf bytes.Buffer307 var in bytes.Buffer308 25 unmodified lines334 }335 }336 337 func TestTeaContextImplodeDeadlock(t *testing.T) {338+ t.Parallel()339 ctx, cancel := context.WithCancel(t.Context())340 var buf bytes.Buffer341 var in bytes.Buffer342 18 unmodified lines361 }362 }363 364 func TestTeaContextBatchDeadlock(t *testing.T) {365+ t.Parallel()366 ctx, cancel := context.WithCancel(t.Context())367 var buf bytes.Buffer368 var in bytes.Buffer369 27 unmodified lines397 }398 }399 400 func TestTeaBatchMsg(t *testing.T) {401+ t.Parallel()402 var buf bytes.Buffer403 var in bytes.Buffer404 405 inc := func() Msg {27 unmodified lines433 }434 }435 436 func TestTeaSequenceMsg(t *testing.T) {437+ t.Parallel()438 var buf bytes.Buffer439 var in bytes.Buffer440 441 inc := func() Msg {16 unmodified lines458 }459 }460 461 func TestTeaSequenceMsgWithBatchMsg(t *testing.T) {462+ t.Parallel()463 var buf bytes.Buffer464 var in bytes.Buffer465 466 inc := func() Msg {19 unmodified lines486 }487 }488 489 func TestTeaNestedSequenceMsg(t *testing.T) {490+ t.Parallel()491 var buf bytes.Buffer492 var in bytes.Buffer493 494 inc := func() Msg {16 unmodified lines511 }512 }513 514 func TestTeaSend(t *testing.T) {515+ t.Parallel()516 var buf bytes.Buffer517 var in bytes.Buffer518 519 m := &testModel{}13 unmodified lines533 p.Send(Quit())534 }535 536 func TestTeaNoRun(t *testing.T) {537+ t.Parallel()538 var buf bytes.Buffer539 var in bytes.Buffer540 541 m := &testModel{}3 unmodified lines545 )546 }547 548 func TestTeaPanic(t *testing.T) {549+ t.Parallel()550 var buf bytes.Buffer551 var in bytes.Buffer552 553 m := &testModel{}22 unmodified lines576 }577 }578 579 func TestTeaGoroutinePanic(t *testing.T) {580+ t.Parallel()581 var buf bytes.Buffer582 var in bytes.Buffer583 584 m := &testModel{}87 unmodified lines