You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.7 KiB

  1. package mock
  2. import (
  3. "log"
  4. "reflect"
  5. gokitlog "github.com/go-kit/kit/log"
  6. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  7. em "github.com/tendermint/tools/tm-monitor/eventmeter"
  8. )
  9. type EventMeter struct {
  10. latencyCallback em.LatencyCallbackFunc
  11. disconnectCallback em.DisconnectCallbackFunc
  12. eventCallback em.EventCallbackFunc
  13. }
  14. func (e *EventMeter) Start() error { return nil }
  15. func (e *EventMeter) Stop() {}
  16. func (e *EventMeter) SetLogger(l gokitlog.Logger) {}
  17. func (e *EventMeter) RegisterLatencyCallback(cb em.LatencyCallbackFunc) { e.latencyCallback = cb }
  18. func (e *EventMeter) RegisterDisconnectCallback(cb em.DisconnectCallbackFunc) {
  19. e.disconnectCallback = cb
  20. }
  21. func (e *EventMeter) Subscribe(eventID string, cb em.EventCallbackFunc) error {
  22. e.eventCallback = cb
  23. return nil
  24. }
  25. func (e *EventMeter) Unsubscribe(eventID string) error {
  26. e.eventCallback = nil
  27. return nil
  28. }
  29. func (e *EventMeter) Call(callback string, args ...interface{}) {
  30. switch callback {
  31. case "latencyCallback":
  32. e.latencyCallback(args[0].(float64))
  33. case "disconnectCallback":
  34. e.disconnectCallback()
  35. case "eventCallback":
  36. e.eventCallback(args[0].(*em.EventMetric), args[1])
  37. }
  38. }
  39. type RpcClient struct {
  40. Stubs map[string]ctypes.TMResult
  41. }
  42. func (c *RpcClient) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) {
  43. s, ok := c.Stubs[method]
  44. if !ok {
  45. log.Fatalf("Call to %s, but no stub is defined for it", method)
  46. }
  47. rv, rt := reflect.ValueOf(result), reflect.TypeOf(result)
  48. rv, rt = rv.Elem(), rt.Elem()
  49. rv.Set(reflect.ValueOf(s))
  50. return s, nil
  51. }