The event queue can now report if it is empty.

This commit is contained in:
Jakub Sobon 2018-06-18 20:02:17 +01:00
parent fd186956f6
commit 0e620cf3ca
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
2 changed files with 19 additions and 4 deletions

View File

@ -75,6 +75,13 @@ func (u *Unbound) wake() {
}
}
// Empty determines if the queue is empty.
func (u *Unbound) Empty() bool {
u.mu.Lock()
defer u.mu.Unlock()
return u.empty()
}
// empty determines if the queue is empty.
func (u *Unbound) empty() bool {
return u.first == nil

View File

@ -25,12 +25,14 @@ import (
func TestQueue(t *testing.T) {
tests := []struct {
desc string
pushes []terminalapi.Event
wantPops []terminalapi.Event
desc string
pushes []terminalapi.Event
wantEmpty bool // Checked after pushes and before pops.
wantPops []terminalapi.Event
}{
{
desc: "empty queue returns nil",
desc: "empty queue returns nil",
wantEmpty: true,
wantPops: []terminalapi.Event{
nil,
},
@ -42,6 +44,7 @@ func TestQueue(t *testing.T) {
terminalapi.NewError("error2"),
terminalapi.NewError("error3"),
},
wantEmpty: false,
wantPops: []terminalapi.Event{
terminalapi.NewError("error1"),
terminalapi.NewError("error2"),
@ -59,6 +62,11 @@ func TestQueue(t *testing.T) {
q.Push(ev)
}
gotEmpty := q.Empty()
if gotEmpty != tc.wantEmpty {
t.Errorf("Empty => got %v, want %v", gotEmpty, tc.wantEmpty)
}
for i, want := range tc.wantPops {
got := q.Pop()
if diff := pretty.Compare(want, got); diff != "" {