From 487c8fe88f4cfcc55850b8aef73665b1d09b8fe0 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Wed, 16 Aug 2017 03:24:23 +0900 Subject: Make Reader event notification asynchronous Instead of notifying the event coordinator (EventBox) whenever a new line is arrived, start a background goroutine that periodically does the task. Atomic.StoreInt32 is much cheaper than mutex synchronization that happens during EventBox update. --- src/reader_test.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'src/reader_test.go') diff --git a/src/reader_test.go b/src/reader_test.go index d5c218cb..82ca6b7b 100644 --- a/src/reader_test.go +++ b/src/reader_test.go @@ -2,6 +2,7 @@ package fzf import ( "testing" + "time" "github.com/junegunn/fzf/src/util" ) @@ -11,7 +12,10 @@ func TestReadFromCommand(t *testing.T) { eb := util.NewEventBox() reader := Reader{ pusher: func(s []byte) bool { strs = append(strs, string(s)); return true }, - eventBox: eb} + eventBox: eb, + event: int32(EvtReady)} + + reader.startEventPoller() // Check EventBox if eb.Peek(EvtReadNew) { @@ -19,21 +23,16 @@ func TestReadFromCommand(t *testing.T) { } // Normal command - reader.readFromCommand(`echo abc && echo def`) + reader.fin(reader.readFromCommand(`echo abc && echo def`)) if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" { t.Errorf("%s", strs) } // Check EventBox again - if !eb.Peek(EvtReadNew) { - t.Error("EvtReadNew should be set yet") - } + eb.WaitFor(EvtReadFin) // Wait should return immediately eb.Wait(func(events *util.Events) { - if _, found := (*events)[EvtReadNew]; !found { - t.Errorf("%s", events) - } events.Clear() }) @@ -42,8 +41,14 @@ func TestReadFromCommand(t *testing.T) { t.Error("EvtReadNew should not be set yet") } + // Make sure that event poller is finished + time.Sleep(readerPollIntervalMax) + + // Restart event poller + reader.startEventPoller() + // Failing command - reader.readFromCommand(`no-such-command`) + reader.fin(reader.readFromCommand(`no-such-command`)) strs = []string{} if len(strs) > 0 { t.Errorf("%s", strs) @@ -51,6 +56,9 @@ func TestReadFromCommand(t *testing.T) { // Check EventBox again if eb.Peek(EvtReadNew) { - t.Error("Command failed. EvtReadNew should be set") + t.Error("Command failed. EvtReadNew should not be set") + } + if !eb.Peek(EvtReadFin) { + t.Error("EvtReadFin should be set") } } -- cgit v1.2.3