monitor a good
Former-commit-id: 5a37bfdfe208d3d1a71e6b4924209d0c8e6f53a0
This commit is contained in:
110
monitor/monitor_test.go
Normal file
110
monitor/monitor_test.go
Normal file
@@ -0,0 +1,110 @@
|
||||
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")
|
||||
}
|
||||
Reference in New Issue
Block a user