Remove monitor server and instead use callback
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -12,43 +10,10 @@ import (
|
||||
|
||||
const testmport = ":13152"
|
||||
|
||||
func Test_MonitorListen(t *testing.T) {
|
||||
m, err := New(testmport, func(string) {})
|
||||
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()
|
||||
|
||||
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, func(string) {})
|
||||
numItems := 2
|
||||
completed := make(chan struct{}, numItems)
|
||||
m, err := New(testmport, func(string) { completed <- struct{}{} })
|
||||
if err != nil {
|
||||
t.Fatalf("cannot create new monitor: %v", err)
|
||||
}
|
||||
@@ -59,8 +24,8 @@ func Test_Monitor(t *testing.T) {
|
||||
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)
|
||||
for i := 0; i < numItems; i++ {
|
||||
item := NewItem("item"+strconv.Itoa(i), time.Second+time.Second*time.Duration(i)*1)
|
||||
select {
|
||||
case itemsNew <- *item:
|
||||
case <-time.After(time.Second * 5):
|
||||
@@ -68,6 +33,14 @@ func Test_Monitor(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user