pkg/edit: Fix race conditions.

This commit is contained in:
Qi Xiao 2020-07-14 20:43:11 +01:00
parent b18b833acb
commit c2c6254316
3 changed files with 27 additions and 5 deletions

View File

@ -66,7 +66,7 @@ func Start(app cli.App, cfg Config) {
if cfg.Binding == nil {
cfg.Binding = cli.DummyHandler{}
}
cursor := histutil.NewDedupCursor(cfg.Store.Cursor(cfg.Prefix))
cursor := cfg.Store.Cursor(cfg.Prefix)
cursor.Prev()
_, err := cursor.Get()
if err != nil {

View File

@ -21,8 +21,8 @@ func TestHistWalk(t *testing.T) {
getCfg := func() Config {
store := histutil.NewMemStore(
// 0 1 2 3 4 5
"echo", "ls -l", "echo a", "ls -a", "echo a", "ls -a")
// 0 1 2 3 4 5
"echo", "ls -l", "echo a", "echo", "echo a", "ls -a")
return Config{
Store: store,
Prefix: "ls",
@ -45,7 +45,6 @@ func TestHistWalk(t *testing.T) {
f.TTY.TestBuffer(t, buf5)
f.TTY.Inject(term.K(ui.Up))
// Skips item #3 as it is a duplicate.
buf1 := f.MakeBuffer(
"ls -l", Styles,
" ___", term.DotHere, "\n",

View File

@ -35,7 +35,7 @@ func (s *histStore) AllCmds() ([]store.Cmd, error) {
func (s *histStore) Cursor(prefix string) histutil.Cursor {
s.m.Lock()
defer s.m.Unlock()
return s.hs.Cursor(prefix)
return cursor{&s.m, histutil.NewDedupCursor(s.hs.Cursor(prefix))}
}
func (s *histStore) FastForward() error {
@ -45,3 +45,26 @@ func (s *histStore) FastForward() error {
s.hs = hs
return err
}
type cursor struct {
m *sync.Mutex
c histutil.Cursor
}
func (c cursor) Prev() {
c.m.Lock()
defer c.m.Unlock()
c.c.Prev()
}
func (c cursor) Next() {
c.m.Lock()
defer c.m.Unlock()
c.c.Next()
}
func (c cursor) Get() (store.Cmd, error) {
c.m.Lock()
defer c.m.Unlock()
return c.c.Get()
}