func TestShouldTrace(t *testing.T) {
	testService := "com.hailocab.service.helloworld"
	testEndpoint := "sayhello"
	payload := &TestPayload{}

	// Without a traceID and with a 0 pcChance, we shouldn't trace
	buf := bytes.NewBufferString(`{"hailo":{"service":{"trace":{"pcChance":0.0}}}}`)
	config.Load(buf)
	req, _ := NewRequest(testService, testEndpoint, payload)
	req.SetTraceID("")
	assert.False(t, req.shouldTrace(), `shouldTrace() should return false with traceID="" and pcChance=0`)

	// With a traceID present, we should always trace
	req, _ = NewRequest(testService, testEndpoint, payload)
	req.SetTraceID("test-trace-id")
	assert.True(t, req.shouldTrace(), "shouldTrace() should return true with a traceID set")

	// Without a traceID, and with a 1 pcChance (ie. always), we should trace
	buf = bytes.NewBufferString(`{"hailo":{"service":{"trace":{"pcChance":1.0}}}}`)
	config.Load(buf)
	req, _ = NewRequest(testService, testEndpoint, payload)
	req.SetTraceID("")
	assert.True(t, req.shouldTrace(), `shouldTrace() should return true with traceId="" and pcChance=1`)
}
func (s *MemcacheHostsSuite) TestGetHostsServersInConfig() {
	buf := bytes.NewBufferString(`{
		"hailo": {
			"service": {
				"memcache":{
					"servers": ["10.0.0.1:11211"]
				}
			}
		}
	}`)
	config.Load(buf)

	hosts := getHosts()

	s.Len(hosts, 1)
	s.Equal(hosts[0], "10.0.0.1:11211")
}
func (s *ZkHostsSuite) TestGetHostsServersInConfig() {
	buf := bytes.NewBufferString(`{
		"hailo": {
			"service": {
				"zookeeper":{
					"hosts": ["10.0.0.1:2181"]
				}
			}
		}
	}`)
	config.Load(buf)

	hosts := getHosts()

	s.Len(hosts, 1)
	s.Equal(hosts[0], "10.0.0.1:2181")
}
func (s *MemcacheHostsSuite) TestGetHostsTierInConfig() {
	buf := bytes.NewBufferString(`{
		"hailo": {
			"service": {
				"memcache":{
					"tier": "my-tier"
				}
			}
		}
	}`)
	config.Load(buf)

	s.mockResolver.Register(
		"memcached-my-tier",
		[]net.IP{net.ParseIP("10.0.0.1")},
		nil,
	)
	hosts := getHosts()

	s.Len(hosts, 1)
	s.Equal(hosts[0], "10.0.0.1:11211")
}
func (s *NsqHostsSuite) TestGetHostsClusterInConfig() {
	buf := bytes.NewBufferString(`{
		"hailo": {
			"service": {
				"nsq":{
					"cluster": "my-cluster"
				}
			}
		}
	}`)
	config.Load(buf)

	s.mockResolver.Register(
		"nsq-my-cluster",
		[]net.IP{net.ParseIP("10.0.0.1")},
		nil,
	)
	hosts := getHosts(4150)

	expectedHosts := []string{"10.0.0.1:4150"}
	s.Equal(strings.Join(expectedHosts, " "), strings.Join(hosts, " "))
}
func loadConfig() {
	buf := bytes.NewBufferString(`{"hailo": {"service": {"cassandra": {"hosts": ["localhost:19160"]}}}}`)
	config.Load(buf)
}
func (s *CircuitBreakerTestSuit) TestCircuitConfig() {
	service := "com.hailocab.test.cruft"
	endpoint := "testendpoint"

	// Set default timeout to 100 ms
	config.Load(bytes.NewBuffer([]byte(`{
		"hailo": {
			"platform": {
				"circuitbreaker": {
					"initialIntervalMs": 100
				}
			}
		}
	}`)))

	// Let config propagate -- crufty :(
	time.Sleep(50 * time.Millisecond)

	// Circuit is initially closed
	s.False(Open(service, endpoint))

	// Trip circuit and check
	for i := 0; i < 100; i++ {
		Result(service, endpoint, errors.Timeout("code", "description"))
	}
	s.True(Open(service, endpoint))

	// Wait for circuit to half-open
	s.clock.Add(51 * time.Millisecond)
	s.True(Open(service, endpoint), "Circuit should be open after 51ms")
	s.clock.Add(50 * time.Millisecond)
	s.False(Open(service, endpoint), "Circuit should be closed after 101ms")

	// Set new interval
	s.NoError(config.Load(bytes.NewBuffer([]byte(`{
		"hailo": {
			"platform": {
				"circuitbreaker": {
					"initialIntervalMs": 50,
					"endpoints": {
						"com.hailocab.test.cruft": {
							"testendpoint": {
								"initialIntervalMs": 90
							}
						}
					}
				}
			}
		}
	}`))))

	// Let config propagate -- crufty :(
	time.Sleep(50 * time.Millisecond)

	// Check circuit is closed again
	s.False(Open(service, endpoint))

	// Trip circuit and check
	for i := 0; i < 100; i++ {
		Result(service, endpoint, errors.Timeout("code", "description"))
	}
	s.True(Open(service, endpoint))

	// Wait for circuit to half-open
	s.clock.Add(51 * time.Millisecond)
	s.True(Open(service, endpoint), "Circuit should be open after 51ms")
	s.clock.Add(40 * time.Millisecond)
	s.False(Open(service, endpoint), "Circuit should be closed after 91ms")
}
func (s *NsqHostsSuite) TearDownTest() {
	s.Suite.TearDownTest()
	dns.DefaultResolver = s.realResolver
	config.Load(bytes.NewBufferString("{}"))
}