caddy/middleware/proxy/upstream_test.go

54 lines
1.2 KiB
Go
Raw Normal View History

package proxy
import (
"testing"
"time"
)
func TestHealthCheck(t *testing.T) {
upstream := &staticUpstream{
from: "",
Hosts: testPool(),
Policy: &Random{},
FailTimeout: 10 * time.Second,
MaxFails: 1,
}
upstream.healthCheck()
if upstream.Hosts[0].Down() {
t.Error("Expected first host in testpool to not fail healthcheck.")
}
if !upstream.Hosts[1].Down() {
t.Error("Expected second host in testpool to fail healthcheck.")
}
}
func TestSelect(t *testing.T) {
upstream := &staticUpstream{
from: "",
Hosts: testPool()[:3],
Policy: &Random{},
FailTimeout: 10 * time.Second,
MaxFails: 1,
}
upstream.Hosts[0].Unhealthy = true
upstream.Hosts[1].Unhealthy = true
upstream.Hosts[2].Unhealthy = true
if h := upstream.Select(); h != nil {
t.Error("Expected select to return nil as all host are down")
}
upstream.Hosts[2].Unhealthy = false
if h := upstream.Select(); h == nil {
t.Error("Expected select to not return nil")
}
}
2015-05-29 06:56:11 +08:00
func TestRegisterPolicy(t *testing.T) {
2015-05-29 07:53:54 +08:00
name := "custom"
2015-05-29 06:56:11 +08:00
customPolicy := &customPolicy{}
2015-05-29 07:53:54 +08:00
RegisterPolicy(name, func() Policy { return customPolicy })
if _, ok := supportedPolicies[name]; !ok {
2015-05-29 06:56:11 +08:00
t.Error("Expected supportedPolicies to have a custom policy.")
}
}