2017-05-22 20:07:40 +00:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-05-22 20:07:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Notify struct {
|
2017-07-12 14:01:42 +00:00
|
|
|
updated chan int
|
2017-05-22 20:07:40 +00:00
|
|
|
|
|
|
|
// A guard to protect an access to the internal attributes
|
|
|
|
// of the notification mock in order to prevent panics
|
|
|
|
// raised by the race conditions detector.
|
|
|
|
sync.RWMutex
|
2020-06-04 12:50:52 +00:00
|
|
|
state map[structs.CheckID]string
|
|
|
|
updates map[structs.CheckID]int
|
|
|
|
output map[structs.CheckID]string
|
|
|
|
serviceIDs map[structs.ServiceID]bool
|
2017-05-22 20:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNotify() *Notify {
|
|
|
|
return &Notify{
|
2020-06-04 12:50:52 +00:00
|
|
|
state: make(map[structs.CheckID]string),
|
|
|
|
updates: make(map[structs.CheckID]int),
|
|
|
|
output: make(map[structs.CheckID]string),
|
|
|
|
serviceIDs: make(map[structs.ServiceID]bool),
|
2017-05-22 20:07:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 12:50:52 +00:00
|
|
|
// ServiceExists mock
|
|
|
|
func (c *Notify) ServiceExists(serviceID structs.ServiceID) bool {
|
|
|
|
return c.serviceIDs[serviceID]
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddServiceID will mock a service being present locally
|
|
|
|
func (c *Notify) AddServiceID(serviceID structs.ServiceID) {
|
|
|
|
c.serviceIDs[serviceID] = true
|
|
|
|
}
|
|
|
|
|
2017-07-12 14:01:42 +00:00
|
|
|
func NewNotifyChan() (*Notify, chan int) {
|
|
|
|
n := &Notify{
|
|
|
|
updated: make(chan int),
|
2019-12-10 02:26:41 +00:00
|
|
|
state: make(map[structs.CheckID]string),
|
|
|
|
updates: make(map[structs.CheckID]int),
|
|
|
|
output: make(map[structs.CheckID]string),
|
2017-07-12 14:01:42 +00:00
|
|
|
}
|
|
|
|
return n, n.updated
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:07:40 +00:00
|
|
|
func (m *Notify) sprintf(v interface{}) string {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
return fmt.Sprintf("%v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Notify) StateMap() string { return m.sprintf(m.state) }
|
|
|
|
func (m *Notify) UpdatesMap() string { return m.sprintf(m.updates) }
|
|
|
|
func (m *Notify) OutputMap() string { return m.sprintf(m.output) }
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
func (m *Notify) UpdateCheck(id structs.CheckID, status, output string) {
|
2017-05-22 20:07:40 +00:00
|
|
|
m.Lock()
|
|
|
|
m.state[id] = status
|
|
|
|
old := m.updates[id]
|
|
|
|
m.updates[id] = old + 1
|
|
|
|
m.output[id] = output
|
2017-07-12 14:01:42 +00:00
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
if m.updated != nil {
|
|
|
|
m.updated <- 1
|
|
|
|
}
|
2017-05-22 20:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// State returns the state of the specified health-check.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (m *Notify) State(id structs.CheckID) string {
|
2017-05-22 20:07:40 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
return m.state[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates returns the count of updates of the specified health-check.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (m *Notify) Updates(id structs.CheckID) int {
|
2017-05-22 20:07:40 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
return m.updates[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output returns an output string of the specified health-check.
|
2019-12-10 02:26:41 +00:00
|
|
|
func (m *Notify) Output(id structs.CheckID) string {
|
2017-05-22 20:07:40 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
return m.output[id]
|
|
|
|
}
|