66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package monitor
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang-collections/go-datastructures/queue"
|
|
)
|
|
|
|
func Test_Monitor(t *testing.T) {
|
|
itemsNew := make(chan Item, 1)
|
|
itemsDone := make(chan Item, 1)
|
|
m, err := New(itemsNew, itemsDone)
|
|
if err != nil {
|
|
t.Fatalf("cannot create new monitor: %v", err)
|
|
}
|
|
|
|
errs := make(chan error)
|
|
go func() {
|
|
errs <- m.Start()
|
|
}()
|
|
select {
|
|
case err := <-errs:
|
|
t.Fatalf("monitor stopped early: %v", err)
|
|
case <-time.After(time.Second * 1):
|
|
}
|
|
|
|
for i := 0; i < 2; i++ {
|
|
item := NewItem("item"+strconv.Itoa(i), time.Second+time.Second*time.Duration(i)*10)
|
|
select {
|
|
case itemsNew <- *item:
|
|
case <-time.After(time.Second * 1):
|
|
t.Fatalf("could not add new item in time limit")
|
|
}
|
|
}
|
|
|
|
for i := 0; i < 2; i++ {
|
|
select {
|
|
case triggered := <-itemsDone:
|
|
if triggered.url != "item"+strconv.Itoa(i) {
|
|
t.Fatalf("wrong item done order: %d was %v", i, triggered)
|
|
}
|
|
case <-time.After(time.Second * 3):
|
|
t.Fatalf("could not get done item in time limit")
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|