Пример #1
0
func TestStatusProperties(t *testing.T) {
	props := map[string]interface{}{"message": "ok", "size": 3, "sync": true}
	uh := health.StatusHealthyWithProperties(props)
	assert.Equal(t, props, uh.Properties)
	assert.EqualValues(t, 3, uh.Properties["size"])
	assert.EqualValues(t, true, uh.Properties["sync"])
}
Пример #2
0
func TestUnmarhsalJSON(t *testing.T) {
	testcases := []struct {
		encoded []byte        // encoded JSON
		status  health.Status // expected object
	}{
		{[]byte(`{"healthy":true}`), health.Healthy},
		{[]byte(`{"healthy":true,"properties":{"message":"service is up"}}`), health.StatusHealthy("service is up")},
		{[]byte(`{}`), health.StatusUnhealthy("", nil)},
		{[]byte(`{"healthy":false,"properties":{"message":"service is down"}}`), health.StatusUnhealthy("service is down", nil)},
		{[]byte(`{"healthy":false,"properties":{"cause":"network unreachable"}}`), health.StatusUnhealthy("", errors.New("network unreachable"))},
		{[]byte(`{"healthy":false,"properties":{"message":"service is down","cause":"network unreachable"}}`),
			health.StatusUnhealthy("service is down", errors.New("network unreachable"))},
		{[]byte(`{"healthy":true,"properties":{"message":"ok","size":3.0, "sync":true}}`),
			health.StatusHealthyWithProperties(map[string]interface{}{"message": "ok", "size": 3.0, "sync": true})},
		{[]byte(`{"healthy":false,"properties":{"message":"ok","size":3.0, "sync":true}}`),
			health.StatusUnhealthyWithProperties(map[string]interface{}{"message": "ok", "size": 3.0, "sync": true})},
	}

	for _, s := range testcases {
		decoded := &health.Status{}
		err := json.Unmarshal(s.encoded, decoded)
		assert.Nil(t, err)
		if err != nil {
			println(err.Error())
		}
		assert.EqualValues(t, s.status, *decoded)
	}
}
Пример #3
0
//-----------------------------------------------------------------------------
// JSON marshaling
func TestMarhsalJSONHealthy(t *testing.T) {
	testcases := []health.Status{
		health.Healthy,
		health.StatusHealthy("service is up"),
		health.StatusHealthyWithProperties(nil),
		health.StatusHealthyWithProperties(map[string]interface{}{"message": "ok", "size": 3.0, "sync": true}),
	}

	for _, s := range testcases {
		b, err := json.Marshal(s)
		assert.Nil(t, err)
		decoded := &health.Status{}
		err = json.Unmarshal(b, decoded)
		assert.Nil(t, err)
		assert.EqualValues(t, s, *decoded)
	}
}
Пример #4
0
func (hc *healthChecker) Check() health.Status {
	hc.mutex.Lock()
	defer hc.mutex.Unlock()

	props := map[string]interface{}{"size": hc.clusterSize, "threshold": hc.threshold}

	if hc.clusterSize < hc.threshold {
		subsizeDuration := time.Now().Sub(hc.subsizeTimestamp)
		message := fmt.Sprintf("Cluster size (%d) is beneath threshold (%d) for %v", hc.clusterSize, hc.threshold, subsizeDuration)
		if subsizeDuration > hc.subsizeGracePeriod {
			hc.logger.Error(message)
			props["message"] = message
			return health.StatusHealthyWithProperties(props)
		}
		hc.logger.Warning(message)
	}

	return health.StatusHealthyWithProperties(props)
}