ghostream/internal/monitoring/monitoring_test.go

35 lines
958 B
Go
Raw Permalink Normal View History

package monitoring
2020-10-09 21:26:26 +00:00
import (
"net/http"
"testing"
"time"
)
func TestMonitoringServe(t *testing.T) {
go Serve(&Options{Enabled: false, ListenAddress: "127.0.0.1:2112"})
// Sleep 0.5 second to ensure that the web server is running, to avoid fails because the request came too early
time.Sleep(500 * time.Millisecond)
// Test GET request, should fail
resp, err := http.Get("http://127.0.0.1:2112/metrics")
if err == nil {
t.Error("Failed to fail task")
}
go Serve(&Options{Enabled: true, ListenAddress: "127.0.0.1:2112"})
// Sleep 0.5 second to ensure that the web server is running, to avoid fails because the request came too early
time.Sleep(500 * time.Millisecond)
// Test GET request
resp, err = http.Get("http://127.0.0.1:2112/metrics")
if err != nil {
t.Error("Error while getting /metrics:", err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Metric page returned %v != %v on GET", resp.StatusCode, http.StatusOK)
}
}