Skip to content
Myr

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

10 files+137 added−10 removed

modified:examples/go.mod+1 added-1 removed
21 unmodified lines
22require (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 // indirect
17 unmodified lines
modified:examples/go.sum+2 added-2 removed
20 unmodified lines
21github.com/charmbracelet/colorprofile v0.4.3 h1:QPa1IWkYI+AOB+fE+mg/5/4HRMZcaXex9t5KX76i20Q=22github.com/charmbracelet/colorprofile v0.4.3/go.mod h1:/zT4BhpD5aGFpqQQqw7a+VtHCzu+zrQtt1zhMt9mR4Q=23github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=24github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=25github.com/charmbracelet/ultraviolet v0.0.0-20260413133134-73592393e1ad h1:kxhuAdAdSGSiAadbEjP7v06nCb17jXjjqF2QOX+0QiE=26github.com/charmbracelet/ultraviolet v0.0.0-20260413133134-73592393e1ad/go.mod h1:bAAz7dh/FTYfC+oiHavL4mX1tOIBZ0ZwYjSi3qE6ivM=25github.com/charmbracelet/ultraviolet v0.0.0-20260416155717-489999b90468 h1:Q9fO0y1Zo5KB/5Vu8JZoLGm1N3RzF9bNj3Ao3xoR+Ac=26github.com/charmbracelet/ultraviolet v0.0.0-20260416155717-489999b90468/go.mod h1:bAAz7dh/FTYfC+oiHavL4mX1tOIBZ0ZwYjSi3qE6ivM=27github.com/charmbracelet/x/ansi v0.11.7 h1:kzv1kJvjg2S3r9KHo8hDdHFQLEqn4RBCb39dAYC84jI=28github.com/charmbracelet/x/ansi v0.11.7/go.mod h1:9qGpnAVYz+8ACONkZBUWPtL7lulP9No6p1epAihUZwQ=29github.com/charmbracelet/x/exp/charmtone v0.0.0-20250602192518-9e722df69bbb h1:oTM8tZxV7FY0ehvYjFuICouuhzE08UZYNqUIp/lDQdY=30github.com/charmbracelet/x/exp/charmtone v0.0.0-20250602192518-9e722df69bbb/go.mod h1:T9jr8CzFpjhFVHjNjKwbAD7KwBNyFnj2pntAO7F2zw0=
57 unmodified lines
modified:commands_test.go+8 added
4 unmodified lines
5 "time"6)78func 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 lines
16 }17}1819func 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 lines
27 }28}2930func TestBatch(t *testing.T) {31 t.Parallel()32 testMultipleCommands[BatchMsg](t, Batch)33}3435func TestSequence(t *testing.T) {36 t.Parallel()37 testMultipleCommands[sequenceMsg](t, Sequence)38}3940func 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 lines
added:cursed_renderer_test.go+80 added
1package tea23import (4 "fmt"5 "io"6 "testing"7 "time"8)910type mouseRaceModel struct {11 i int12}1314func (m *mouseRaceModel) Init() Cmd { return nil }1516func (m *mouseRaceModel) Update(msg Msg) (Model, Cmd) {17 switch msg.(type) {18 case MouseClickMsg, MouseMotionMsg, MouseWheelMsg:19 m.i++20 }21 return m, nil22}2324func (m *mouseRaceModel) View() View {25 return View{26 Content: fmt.Sprintf("tick-%d\n", m.i),27 MouseMode: MouseModeCellMotion,28 }29}3031// Fixes: https://github.com/charmbracelet/bubbletea/issues/169032func TestCursedRenderer_mouseVsFlush(t *testing.T) {33 t.Parallel()3435 pr, pw := io.Pipe()36 defer func() { _ = pw.Close() }()3738 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 )5152 runDone := make(chan struct{})53 go func() {54 defer close(runDone)55 _, _ = p.Run()56 }()5758 time.Sleep(150 * time.Millisecond)5960 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 }7374 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 lines
763}764765// onMouse implements renderer.766func (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}778779func setProgressBar(s *cursedRenderer, pb *ProgressBar) {
81 unmodified lines
modified:exec_test.go+2 added
88 unmodified lines
89 }9091 for _, test := range tests {92 t.Run(test.name, func(t *testing.T) {93 t.Parallel()94 var buf bytes.Buffer95 var in bytes.Buffer9697 m := &testExecModel{cmd: test.cmd}
20 unmodified lines
118 }119}120121func TestTeaExecWithNilInput(t *testing.T) {122 t.Parallel()123 var buf bytes.Buffer124125 m := &testExecNoInputModel{}126 p := NewProgram(m,
11 unmodified lines
modified:options_test.go+9 added
8 unmodified lines
9)1011func 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 })2021 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 })2829 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 })3637 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 })4445 t.Run("external context", func(t *testing.T) {46 t.Parallel()47 extCtx, extCancel := context.WithCancel(context.Background())48 defer extCancel()4950 p := NewProgram(nil, WithContext(extCtx))
8 unmodified lines
59 fn(p)60 }6162 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 })7071 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 lines
85 fn(p)86 }8788 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 })9697 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 lines
modified:screen_test.go+2 added
130 unmodified lines
131 }132133 for _, test := range tests {134 t.Run(test.name, func(t *testing.T) {135 t.Parallel()136 var buf bytes.Buffer137 var in bytes.Buffer138139 m := &testViewModel{testModel: &testModel{}}
39 unmodified lines
179 }180181 for _, test := range tests {182 t.Run(test.name, func(t *testing.T) {183 t.Parallel()184 var buf bytes.Buffer185 var in bytes.Buffer186187 m := &testModel{}
20 unmodified lines
modified:Taskfile.yaml+2 added-2 removed
1# https://taskfile.dev23version: '3'3version: "3"45tasks:6 lint:7 desc: Run lint
2 unmodified lines
1011 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 lines
3import (4 "bytes"5 "context"6 "errors"7 "fmt"8 "io"9 "strings"10 "sync"11 "sync/atomic"
54 unmodified lines
66 return NewView("success")67}6869func TestTeaModel(t *testing.T) {70 t.Parallel()71 var buf bytes.Buffer72 var in bytes.Buffer73 in.Write([]byte("q"))74
14 unmodified lines
89 }90}9192func TestTeaQuit(t *testing.T) {93 t.Parallel()94 var buf bytes.Buffer95 var in bytes.Buffer9697 m := &testModel{}
16 unmodified lines
114 }115}116117func TestTeaWaitQuit(t *testing.T) {118 t.Parallel()119 var buf bytes.Buffer120 var in bytes.Buffer121122 progStarted := make(chan struct{})
45 unmodified lines
168 }169}170171func TestTeaWaitKill(t *testing.T) {172 t.Parallel()173 var buf bytes.Buffer174 var in bytes.Buffer175176 progStarted := make(chan struct{})
45 unmodified lines
222 }223}224225func 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}233234func testTeaWithFilter(t *testing.T, preventCount uint32) {235 var buf bytes.Buffer
31 unmodified lines
267 }268}269270func TestTeaKill(t *testing.T) {271 t.Parallel()272 var buf bytes.Buffer273 var in bytes.Buffer274275 m := &testModel{}
24 unmodified lines
300 }301}302303func 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 lines
334 }335}336337func 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 lines
361 }362}363364func 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 lines
397 }398}399400func TestTeaBatchMsg(t *testing.T) {401 t.Parallel()402 var buf bytes.Buffer403 var in bytes.Buffer404405 inc := func() Msg {
27 unmodified lines
433 }434}435436func TestTeaSequenceMsg(t *testing.T) {437 t.Parallel()438 var buf bytes.Buffer439 var in bytes.Buffer440441 inc := func() Msg {
16 unmodified lines
458 }459}460461func TestTeaSequenceMsgWithBatchMsg(t *testing.T) {462 t.Parallel()463 var buf bytes.Buffer464 var in bytes.Buffer465466 inc := func() Msg {
19 unmodified lines
486 }487}488489func TestTeaNestedSequenceMsg(t *testing.T) {490 t.Parallel()491 var buf bytes.Buffer492 var in bytes.Buffer493494 inc := func() Msg {
16 unmodified lines
511 }512}513514func TestTeaSend(t *testing.T) {515 t.Parallel()516 var buf bytes.Buffer517 var in bytes.Buffer518519 m := &testModel{}
13 unmodified lines
533 p.Send(Quit())534}535536func TestTeaNoRun(t *testing.T) {537 t.Parallel()538 var buf bytes.Buffer539 var in bytes.Buffer540541 m := &testModel{}
3 unmodified lines
545 )546}547548func TestTeaPanic(t *testing.T) {549 t.Parallel()550 var buf bytes.Buffer551 var in bytes.Buffer552553 m := &testModel{}
22 unmodified lines
576 }577}578579func TestTeaGoroutinePanic(t *testing.T) {580 t.Parallel()581 var buf bytes.Buffer582 var in bytes.Buffer583584 m := &testModel{}
87 unmodified lines