Module: Skylight::Test::Mocking

Defined in:
lib/skylight/test.rb

Instance Method Summary collapse

Instance Method Details

#mock!(config_opts = {}, &callback) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/skylight/test.rb', line 4

def mock!(config_opts = {}, &callback)
  config_opts[:mock_submission] ||= callback || proc {}

  config_class =
    Class.new(Config) do
      def validate_with_server
        true
      end
    end

  config = config_class.load(config_opts)
  config[:authentication] ||= "zomg"

  class_eval do
    unless const_defined?(:OriginalInstrumenter)
      const_set :OriginalInstrumenter, Instrumenter
      remove_const :Instrumenter
      const_set(
        :Instrumenter,
        Class.new(OriginalInstrumenter) do
          def self.name
            "Mocked Instrumenter"
          end

          def native_start
            true
          end

          def native_submit_trace(trace)
            config[:mock_submission].call(trace)
          end

          def native_stop
          end
        end
      )

      const_set :OriginalTrace, Trace
      remove_const :Trace
      const_set(
        :Trace,
        Class.new(OriginalTrace) do
          def self.native_new(start, _uuid, endpoint, meta)
            inst = super
            inst.instance_variable_set(:@start, start)
            inst.instance_variable_set(:@endpoint, endpoint)
            inst.instance_variable_set(:@starting_endpoint, endpoint)
            inst.instance_variable_set(:@meta, meta)
            inst
          end

          attr_reader :endpoint, :starting_endpoint, :meta

          def mock_spans
            @mock_spans ||= []
          end

          def filter_spans
            if block_given?
              mock_spans.select { |span| yield span }
            else
              mock_spans.reject { |span| span[:cat] == "noise.gc" }
            end
          end

          def native_get_uuid
            @uuid
          end

          def uuid=(value)
            @uuid = value
          end

          def native_get_started_at
            @start
          end

          def native_set_endpoint(endpoint)
            @endpoint = endpoint
          end

          def native_set_component(component)
            @component = component
          end

          def native_start_span(time, cat)
            span = { start: time, cat: cat }
            mock_spans << span

            # Return integer like the native method does
            mock_spans.index(span)
          end

          def native_span_set_title(span, title)
            mock_spans[span][:title] = title
          end

          def native_span_set_description(span, desc)
            mock_spans[span][:desc] = desc
          end

          def native_span_set_meta(span, meta)
            mock_spans[span][:meta] = meta
          end

          def native_span_started(span)
          end

          def native_span_set_exception(span, exception_object, exception)
            mock_spans[span][:exception_object] = exception_object
            mock_spans[span][:exception] = exception
          end

          def native_stop_span(span, time)
            span = mock_spans[span]
            span[:duration] = time - span[:start]
            nil
          end

          def native_use_pruning
            @using_native_pruning = true
          end
        end
      )
    end
  end

  start!(config)
end

#unmock!Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/skylight/test.rb', line 134

def unmock!
  if const_defined?(:OriginalInstrumenter)
    class_eval do
      remove_const :Instrumenter
      const_set :Instrumenter, OriginalInstrumenter
      remove_const :OriginalInstrumenter

      remove_const :Trace
      const_set :Trace, OriginalTrace
      remove_const :OriginalTrace
    end
  end
end