111 lines
1.8 KiB
Go
111 lines
1.8 KiB
Go
package monitor
|
|
|
|
import (
|
|
"local/rssmon3/config"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func initMonitor() {
|
|
os.Args = []string{"a", "-db", "map"}
|
|
if err := config.New(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func TestMonitorNew(t *testing.T) {
|
|
initMonitor()
|
|
|
|
New()
|
|
}
|
|
|
|
func TestMonitorTrigger(t *testing.T) {
|
|
initMonitor()
|
|
|
|
m := New()
|
|
i, _ := NewItem("MonitorTrigger", time.Second)
|
|
i.setLast(never)
|
|
if time.Now().After(i.Last()) {
|
|
t.Error(i.Last())
|
|
}
|
|
|
|
m.Outgoing = make(chan *Item, 1)
|
|
m.queue.Push(i)
|
|
m.trigger()
|
|
|
|
j := <-m.Outgoing
|
|
if i.Key != j.Key {
|
|
t.Errorf("popped %v != %v", j, i)
|
|
}
|
|
if time.Since(j.Last()) > time.Second {
|
|
t.Error(j.Last())
|
|
}
|
|
}
|
|
|
|
func TestMonitorTriggered(t *testing.T) {
|
|
initMonitor()
|
|
|
|
m := New()
|
|
i, _ := NewItem("MonitorTriggered", time.Second)
|
|
i.setLast(time.Now().Add(time.Hour * -1))
|
|
m.queue.Push(i)
|
|
|
|
start := time.Now()
|
|
select {
|
|
case <-m.triggered():
|
|
case <-time.After(time.Second):
|
|
}
|
|
if time.Since(start) > time.Second {
|
|
t.Errorf("failed to triggered")
|
|
}
|
|
}
|
|
|
|
func TestMonitorEnqueued(t *testing.T) {
|
|
initMonitor()
|
|
|
|
m := New()
|
|
m.Incoming = make(chan *Item, 1)
|
|
m.Incoming <- &Item{Key: "MonitorEnqueued"}
|
|
|
|
select {
|
|
case i := <-m.enqueued():
|
|
if i.Key != "MonitorEnqueued" {
|
|
t.Error(i)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Errorf("enqueued didnt pop")
|
|
}
|
|
}
|
|
|
|
func TestMonitorEnqueue(t *testing.T) {
|
|
initMonitor()
|
|
|
|
m := New()
|
|
m.enqueue(nil)
|
|
|
|
if m.queue.Len() != 1 {
|
|
t.Error(m.queue.Len())
|
|
}
|
|
}
|
|
|
|
func TestMonitorRun(t *testing.T) {
|
|
initMonitor()
|
|
|
|
m := New()
|
|
m.Outgoing = make(chan *Item, 1)
|
|
go m.Run()
|
|
defer config.Values().Can()
|
|
|
|
i, _ := NewItem("MonitorRun", time.Second*5)
|
|
m.Incoming <- i
|
|
time.Sleep(time.Millisecond * 250)
|
|
for j := 0; j < 5; j++ {
|
|
if m.queue.Len() == 1 {
|
|
return
|
|
}
|
|
time.Sleep(time.Millisecond * 10)
|
|
}
|
|
t.Errorf("incoming item didnt enter/reenter queue")
|
|
}
|