mock.go 802 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package paladin
  2. import (
  3. "context"
  4. )
  5. var _ Client = &mock{}
  6. // mock is mock config client.
  7. type mock struct {
  8. ch chan Event
  9. values *Map
  10. }
  11. // NewMock new a config mock client.
  12. func NewMock(vs map[string]string) Client {
  13. values := make(map[string]*Value, len(vs))
  14. for k, v := range vs {
  15. values[k] = &Value{val: v, raw: v}
  16. }
  17. m := new(Map)
  18. m.Store(values)
  19. return &mock{values: m, ch: make(chan Event)}
  20. }
  21. // Get return value by key.
  22. func (m *mock) Get(key string) *Value {
  23. return m.values.Get(key)
  24. }
  25. // GetAll return value map.
  26. func (m *mock) GetAll() *Map {
  27. return m.values
  28. }
  29. // WatchEvent watch multi key.
  30. func (m *mock) WatchEvent(ctx context.Context, key ...string) <-chan Event {
  31. return m.ch
  32. }
  33. // Close close watcher.
  34. func (m *mock) Close() error {
  35. close(m.ch)
  36. return nil
  37. }