54 lines
1.2 KiB
Go
Executable File
54 lines
1.2 KiB
Go
Executable File
package monitor
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang-collections/go-datastructures/queue"
|
|
)
|
|
|
|
func Test_Monitor(t *testing.T) {
|
|
numItems := 2
|
|
completed := make(chan struct{}, numItems)
|
|
m, err := New(func(string) { completed <- struct{}{} })
|
|
if err != nil {
|
|
t.Fatalf("cannot create new monitor: %v", err)
|
|
}
|
|
|
|
if err := m.Start(); err != nil {
|
|
t.Fatalf("cannot start monitor: %v", err)
|
|
}
|
|
|
|
for i := 0; i < numItems; i++ {
|
|
if err := m.Submit("item"+strconv.Itoa(i), time.Second+time.Second*time.Duration(i)); err != nil {
|
|
t.Errorf("failed to submit item %d: %v", i, err)
|
|
}
|
|
}
|
|
|
|
for i := 0; i < numItems; i++ {
|
|
select {
|
|
case <-completed:
|
|
case <-time.After(time.Second * 5):
|
|
t.Errorf("did not complete item %d", i)
|
|
}
|
|
}
|
|
|
|
if err := m.Stop(); err != nil {
|
|
t.Fatalf("could not stop monitor: %v", err)
|
|
}
|
|
}
|
|
|
|
func Test_NextEvent(t *testing.T) {
|
|
q := queue.NewPriorityQueue(1)
|
|
a := NewItem("a", time.Second*5)
|
|
b := NewItem("b", time.Second*500)
|
|
q.Put(a)
|
|
q.Put(b)
|
|
if time, err := nextEventTime(q); err != nil {
|
|
t.Fatalf("could not get next event time: %v", err)
|
|
} else if time != a.next {
|
|
t.Fatalf("got wrong next event time: %v, expected %v", time, a.next)
|
|
}
|
|
}
|