...

Source file src/github.com/xiang90/probing/prober_test.go

Documentation: github.com/xiang90/probing

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

View as plain text