Ejemplo n.º 1
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)
	}
}
Ejemplo n.º 2
0
func TestMarhsalJSONUnhealthy(t *testing.T) {
	testcases := []health.Status{
		health.StatusUnhealthy("", nil),
		health.StatusUnhealthy("service is down", nil),
		health.StatusUnhealthy("", errors.New("network unreachable")),
		health.StatusUnhealthy("service is down", errors.New("network unreachable")),
		health.StatusUnhealthyWithProperties(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)
		if err != nil {
			println(err.Error())
		}
		assert.EqualValues(t, s, *decoded)
	}
}