Convert monitor to http server
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -8,29 +10,62 @@ import (
|
||||
"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)
|
||||
const testmport = ":13152"
|
||||
|
||||
func Test_MonitorListen(t *testing.T) {
|
||||
m, err := New(testmport)
|
||||
if err != nil {
|
||||
t.Fatalf("cannot create new monitor: %v", err)
|
||||
}
|
||||
if err := m.Start(); err != nil {
|
||||
t.Fatalf("cannot start monitor: %v", err)
|
||||
}
|
||||
defer m.Stop()
|
||||
|
||||
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):
|
||||
if resp, err := http.Get("http://localhost" + testmport + "/mia"); err != nil {
|
||||
t.Fatalf("GET error: %v", err)
|
||||
} else if resp.StatusCode != http.StatusNotFound {
|
||||
t.Errorf("GET /mia didn't 404: got %v", resp.StatusCode)
|
||||
}
|
||||
|
||||
if resp, err := http.Get("http://localhost" + testmport + "/newfeed"); err != nil {
|
||||
t.Fatalf("GET error: %v", err)
|
||||
} else if resp.StatusCode != http.StatusNotFound {
|
||||
t.Errorf("GET /newfeed didn't 404: got %v", resp.StatusCode)
|
||||
}
|
||||
|
||||
if resp, err := http.Post("http://localhost"+testmport+"/newfeed", "application/json", bytes.NewBuffer([]byte(""))); err != nil {
|
||||
t.Fatalf("POST error: %v", err)
|
||||
} else if resp.StatusCode != http.StatusBadRequest {
|
||||
t.Errorf("POST /newfeed didn't 200: got %v", resp.StatusCode)
|
||||
}
|
||||
|
||||
if resp, err := http.Post("http://localhost"+testmport+"/newfeed", "application/json", bytes.NewBuffer([]byte(`{"URL":"hello", "Interval":"5m"}`))); err != nil {
|
||||
t.Fatalf("POST error: %v", err)
|
||||
} else if resp.StatusCode != http.StatusOK {
|
||||
t.Errorf("POST /newfeed didn't 200: got %v", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Monitor(t *testing.T) {
|
||||
m, err := New(testmport)
|
||||
if err != nil {
|
||||
t.Fatalf("cannot create new monitor: %v", err)
|
||||
}
|
||||
itemsNew := make(chan Item, 1)
|
||||
itemsDone := make(chan Item, 1)
|
||||
m.newItems = itemsNew
|
||||
m.triggeredItems = itemsDone
|
||||
|
||||
if err := m.Start(); err != nil {
|
||||
t.Fatalf("cannot start monitor: %v", err)
|
||||
}
|
||||
|
||||
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):
|
||||
case <-time.After(time.Second * 5):
|
||||
t.Fatalf("could not add new item in time limit")
|
||||
}
|
||||
}
|
||||
@@ -38,10 +73,10 @@ func Test_Monitor(t *testing.T) {
|
||||
for i := 0; i < 2; i++ {
|
||||
select {
|
||||
case triggered := <-itemsDone:
|
||||
if triggered.url != "item"+strconv.Itoa(i) {
|
||||
if triggered.URL != "item"+strconv.Itoa(i) {
|
||||
t.Fatalf("wrong item done order: %d was %v", i, triggered)
|
||||
}
|
||||
case <-time.After(time.Second * 3):
|
||||
case <-time.After(time.Second * 5):
|
||||
t.Fatalf("could not get done item in time limit")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user