Switching termdash test to common spinlock utility.

And delaying redraws to allow fast widgets to process the input event.
This commit is contained in:
Jakub Sobon 2019-02-20 23:35:26 -05:00
parent 37d557d30f
commit d100f6fc24
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
3 changed files with 54 additions and 24 deletions

View File

@ -0,0 +1,32 @@
// Package testevent provides utilities for tests that deal with concurrent
// events.
package testevent
import (
"context"
"fmt"
"time"
)
// WaitFor waits until the provided function returns a nil error or the timeout.
// If the function doesn't return a nil error before the timeout expires,
// returns the last returned error.
func WaitFor(timeout time.Duration, fn func() error) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
var err error
for {
tick := time.NewTimer(5 * time.Millisecond)
select {
case <-tick.C:
if err = fn(); err != nil {
continue
}
return nil
case <-ctx.Done():
return fmt.Errorf("timeout expired, error: %v", err)
}
}
}

View File

@ -277,6 +277,12 @@ func (td *termdash) redraw() error {
func (td *termdash) evRedraw() error { func (td *termdash) evRedraw() error {
td.mu.Lock() td.mu.Lock()
defer td.mu.Unlock() defer td.mu.Unlock()
// Don't redraw immediately, give widgets that are performing enough time
// to update.
// We don't want to actually synchronize until all widgets update, we are
// purposefully leaving slow widgets behind.
time.Sleep(25 * time.Millisecond)
return td.redraw() return td.redraw()
} }

View File

@ -16,6 +16,7 @@ package termdash
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"image" "image"
"sync" "sync"
@ -26,6 +27,7 @@ import (
"github.com/mum4k/termdash/canvas/testcanvas" "github.com/mum4k/termdash/canvas/testcanvas"
"github.com/mum4k/termdash/container" "github.com/mum4k/termdash/container"
"github.com/mum4k/termdash/event/eventqueue" "github.com/mum4k/termdash/event/eventqueue"
"github.com/mum4k/termdash/event/testevent"
"github.com/mum4k/termdash/keyboard" "github.com/mum4k/termdash/keyboard"
"github.com/mum4k/termdash/mouse" "github.com/mum4k/termdash/mouse"
"github.com/mum4k/termdash/terminal/faketerm" "github.com/mum4k/termdash/terminal/faketerm"
@ -388,8 +390,13 @@ func TestRun(t *testing.T) {
return return
} }
if err := untilEmpty(5*time.Second, eq); err != nil { if err := testevent.WaitFor(5*time.Second, func() error {
t.Fatalf("untilEmpty => %v", err) if !eq.Empty() {
return errors.New("event queue not empty")
}
return nil
}); err != nil {
t.Fatalf("testevent.WaitFor => %v", err)
} }
if tc.after != nil { if tc.after != nil {
@ -569,8 +576,13 @@ func TestController(t *testing.T) {
tc.apiEvents(mi) tc.apiEvents(mi)
} }
if err := untilEmpty(5*time.Second, eq); err != nil { if err := testevent.WaitFor(5*time.Second, func() error {
t.Fatalf("untilEmpty => %v", err) if !eq.Empty() {
return errors.New("event queue not empty")
}
return nil
}); err != nil {
t.Fatalf("testevent.WaitFor => %v", err)
} }
if tc.controls != nil { if tc.controls != nil {
if err := tc.controls(ctrl); err != nil { if err := tc.controls(ctrl); err != nil {
@ -585,23 +597,3 @@ func TestController(t *testing.T) {
}) })
} }
} }
// untilEmpty waits until the queue empties.
// Waits at most the specified duration.
func untilEmpty(timeout time.Duration, q *eventqueue.Unbound) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for {
tick := time.NewTimer(5 * time.Millisecond)
select {
case <-tick.C:
if q.Empty() {
return nil
}
case <-ctx.Done():
return fmt.Errorf("while waiting for the event queue to empty: %v", ctx.Err())
}
}
}