github.com/splucs/witchcraft-go-server@v1.7.0/status/reporter/reporter_test.go (about) 1 // Copyright (c) 2018 Palantir Technologies. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package reporter 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/palantir/witchcraft-go-server/conjure/witchcraft/api/health" 22 "github.com/stretchr/testify/assert" 23 ) 24 25 const testCheckType = health.CheckType("TEST") 26 27 func TestGetHealthy(t *testing.T) { 28 reporter := newHealthReporter() 29 reporter.healthComponents = map[health.CheckType]HealthComponent{ 30 testCheckType: &healthComponent{ 31 name: testCheckType, 32 state: health.HealthStateHealthy, 33 }, 34 } 35 36 status, found := reporter.getHealthCheck(testCheckType) 37 assert.True(t, found) 38 assert.Equal(t, status.State, HealthyState) 39 } 40 41 func TestGetError(t *testing.T) { 42 reporter := newHealthReporter() 43 reporter.healthComponents = map[health.CheckType]HealthComponent{ 44 testCheckType: &healthComponent{ 45 name: testCheckType, 46 state: ErrorState, 47 }, 48 } 49 50 status, found := reporter.getHealthCheck(testCheckType) 51 assert.True(t, found) 52 assert.Equal(t, status.State, ErrorState) 53 } 54 55 func TestGetNotFound(t *testing.T) { 56 reporter := NewHealthReporter() 57 status := reporter.HealthStatus(context.TODO()) 58 _, found := status.Checks[testCheckType] 59 assert.False(t, found) 60 } 61 62 func TestInitializeCollision(t *testing.T) { 63 reporter := NewHealthReporter() 64 component, err := reporter.InitializeHealthComponent(validComponent) 65 assert.NotNil(t, component) 66 assert.NoError(t, err) 67 68 componentCollision, err := reporter.InitializeHealthComponent(validComponent) 69 assert.Nil(t, componentCollision) 70 assert.Error(t, err) 71 } 72 73 func TestInitializeThenGet(t *testing.T) { 74 reporter := NewHealthReporter() 75 component, err := reporter.InitializeHealthComponent(validComponent) 76 assert.NotNil(t, component) 77 assert.NoError(t, err) 78 79 fromGet, ok := reporter.GetHealthComponent(validComponent) 80 assert.True(t, ok) 81 assert.Equal(t, fromGet, component) 82 } 83 84 func TestUnregisterThenGet(t *testing.T) { 85 reporter := newHealthReporter() 86 component, err := reporter.InitializeHealthComponent(validComponent) 87 assert.NotNil(t, component) 88 assert.NoError(t, err) 89 90 assert.True(t, reporter.UnregisterHealthComponent(validComponent)) 91 92 _, present := reporter.GetHealthComponent(validComponent) 93 assert.False(t, present) 94 } 95 96 func TestUnregisterThenGetComponentStatus(t *testing.T) { 97 reporter := newHealthReporter() 98 component, err := reporter.InitializeHealthComponent(validComponent) 99 assert.NotNil(t, component) 100 assert.NoError(t, err) 101 102 assert.True(t, reporter.UnregisterHealthComponent(validComponent)) 103 104 _, present := reporter.GetHealthComponent(validComponent) 105 assert.False(t, present) 106 107 assert.NotPanics(t, func() { component.Status() }) 108 } 109 110 func TestUnregisterOnUninitialized(t *testing.T) { 111 reporter := newHealthReporter() 112 assert.False(t, reporter.UnregisterHealthComponent(validComponent)) 113 }