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.

58 lines
1.6 KiB

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