Module: Skylight::Core::Test::Mocking

Defined in:
lib/skylight/core/test.rb

Instance Method Summary collapse

Instance Method Details

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



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
# File 'lib/skylight/core/test.rb', line 5

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

  unless respond_to?(:__original_instrumenter_class)
    class_eval do
      class << self
        alias_method :__original_instrumenter_class, :instrumenter_class

        def instrumenter_class
          @instrumenter_class ||= Class.new(__original_instrumenter_class) do
            def self.name
              "Mocked Instrumenter"
            end

            def self.native_new(*)
              allocate
            end

            def self.trace_class
              @trace_class ||= Class.new(super) do
                def self.native_new(start, _uuid, endpoint, meta)
                  inst = allocate
                  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 native_get_started_at
                  @start
                end

                def native_set_endpoint(endpoint)
                  @endpoint = endpoint
                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
              end
            end

            def native_start
              true
            end

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

            def native_stop; end

            def limited_description(description)
              description
            end
          end
        end
      end
    end
  end

  start!(config)
end