Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data race in keyboard.Listen() and unit tests, enable race detection #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: go build -v .

- name: Test
run: go test -coverprofile="coverage.txt" -covermode=atomic -v -p 1 .
run: go test -coverprofile="coverage.txt" -covermode=atomic -v -p 1 . -race

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
16 changes: 15 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
module atomicgo.dev/keyboard

go 1.15
go 1.19

require (
github.com/MarvinJWendt/testza v0.4.2
github.com/containerd/console v1.0.3
)

require (
github.com/atomicgo/cursor v0.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gookit/color v1.5.0 // indirect
github.com/klauspost/cpuid/v2 v2.0.12 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/pterm/pterm v0.12.40 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
)
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02
github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE=
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand Down Expand Up @@ -60,7 +58,6 @@ golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuX
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
10 changes: 6 additions & 4 deletions keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keyboard
import (
"fmt"
"os"
"sync/atomic"

"github.com/containerd/console"

Expand Down Expand Up @@ -68,7 +69,7 @@ func stopListener() error {
// })
func Listen(onKeyPress func(key keys.Key) (stop bool, err error)) error {
cancel := make(chan bool)
stopRoutine := false
stopRoutine := atomic.Bool{}

go func() {
for {
Expand All @@ -78,11 +79,12 @@ func Listen(onKeyPress func(key keys.Key) (stop bool, err error)) error {
return
}
case keyInfo := <-mockChannel:
stopRoutine, _ = onKeyPress(keyInfo)
if stopRoutine {
shouldStop, _ := onKeyPress(keyInfo)
if shouldStop {
closeInput()
inputTTY.Close()
}
stopRoutine.Store(shouldStop)
}
}
}()
Expand All @@ -94,7 +96,7 @@ func Listen(onKeyPress func(key keys.Key) (stop bool, err error)) error {
}
}

for !stopRoutine {
for !stopRoutine.Load() {
key, err := getKeyPress()
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions keyboard_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keyboard_test

import (
"sync"
"testing"

"atomicgo.dev/keyboard"
Expand All @@ -18,9 +19,13 @@ func TestMocking(t *testing.T) {

var aPressed, bPressed, cPressed, enterPressed bool
var keyList []keys.Key
var lock = sync.Mutex{}

err := keyboard.Listen(func(key keys.Key) (stop bool, err error) {
lock.Lock()
keyList = append(keyList, key)
lock.Unlock()

switch key.Code {
case keys.RuneKey:
switch key.String() {
Expand Down