1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package probing
import (
"net/http/httptest"
"testing"
"time"
)
var (
testID = "testID"
)
func TestProbe(t *testing.T) {
s := httptest.NewServer(NewHandler())
p := NewProber(nil)
p.AddHTTP(testID, time.Millisecond, []string{s.URL})
defer p.Remove(testID)
time.Sleep(100 * time.Millisecond)
status, err := p.Status(testID)
if err != nil {
t.Fatalf("err = %v, want %v", err, nil)
}
if total := status.Total(); total < 50 || total > 150 {
t.Fatalf("total = %v, want around %v", total, 100)
}
if health := status.Health(); health != true {
t.Fatalf("health = %v, want %v", health, true)
}
// become unhealthy
s.Close()
time.Sleep(100 * time.Millisecond)
if total := status.Total(); total < 150 || total > 250 {
t.Fatalf("total = %v, want around %v", total, 200)
}
if loss := status.Loss(); loss < 50 || loss > 150 {
t.Fatalf("loss = %v, want around %v", loss, 200)
}
if health := status.Health(); health != false {
t.Fatalf("health = %v, want %v", health, false)
}
}
func TestProbeReset(t *testing.T) {
s := httptest.NewServer(NewHandler())
defer s.Close()
p := NewProber(nil)
p.AddHTTP(testID, time.Millisecond, []string{s.URL})
defer p.Remove(testID)
time.Sleep(100 * time.Millisecond)
status, err := p.Status(testID)
if err != nil {
t.Fatalf("err = %v, want %v", err, nil)
}
if total := status.Total(); total < 50 || total > 150 {
t.Fatalf("total = %v, want around %v", total, 100)
}
if health := status.Health(); health != true {
t.Fatalf("health = %v, want %v", health, true)
}
p.Reset(testID)
time.Sleep(100 * time.Millisecond)
if total := status.Total(); total < 50 || total > 150 {
t.Fatalf("total = %v, want around %v", total, 100)
}
if health := status.Health(); health != true {
t.Fatalf("health = %v, want %v", health, true)
}
}
func TestProbeRemove(t *testing.T) {
s := httptest.NewServer(NewHandler())
defer s.Close()
p := NewProber(nil)
p.AddHTTP(testID, time.Millisecond, []string{s.URL})
p.Remove(testID)
_, err := p.Status(testID)
if err != ErrNotFound {
t.Fatalf("err = %v, want %v", err, ErrNotFound)
}
}
|