Class: SeeingIsBelieving::EventStream::Producer

Inherits:
Object
  • Object
show all
Defined in:
lib/seeing_is_believing/event_stream/producer.rb

Defined Under Namespace

Modules: NullQueue

Constant Summary collapse

StackErrors =
[SystemStackError]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resultstream) ⇒ Producer

Returns a new instance of Producer.



18
19
20
21
22
23
24
25
26
27
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 18

def initialize(resultstream)
  resultstream           = Safe::Stream[resultstream]
  self.filename          = nil
  self.max_line_captures = Float::INFINITY
  self.recorded_results  = []
  self.queue             = Safe::Queue[Queue.new]
  self.producer_thread   = Safe::Thread[
    build_producer_thread(resultstream)
  ]
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



16
17
18
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 16

def filename
  @filename
end

#max_line_capturesObject

Returns the value of attribute max_line_captures.



16
17
18
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 16

def max_line_captures
  @max_line_captures
end

#versionObject (readonly) Also known as: ver

Returns the value of attribute version.



29
30
31
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 29

def version
  @version
end

Instance Method Details

#finish!Object



110
111
112
113
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 110

def finish!
  queue << :break # note that consumer will continue reading until stream is closed
  producer_thread.join
end

#record_exception(line_number, exception) ⇒ Object

records the exception, returns the exitstatus for that exception



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 75

def record_exception(line_number, exception)
  return exception.status if SystemExit === exception
  exception = Safe::Exception[exception]
  if !line_number && filename
    begin line_number = exception.backtrace.grep(/#{filename}/).first[/:\d+/][1..-1].to_i
    rescue NoMethodError
    end
  end
  line_number ||= -1
  queue << Safe::Array[[
    "exception",
    Safe::Fixnum[line_number],
    to_string_token(exception.class.name),
    to_string_token(exception.message),
    Safe::Fixnum[
      Safe::Array[exception.backtrace].size
    ],
    *Safe::Array[exception.backtrace].map { |line| to_string_token line }
  ]].join(" ")
  1 # exit status
end

#record_exec(args) ⇒ Object



102
103
104
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 102

def record_exec(args)
  queue << "exec #{to_string_token args.inspect}"
end

#record_filename(filename) ⇒ Object



97
98
99
100
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 97

def record_filename(filename)
  self.filename = filename
  queue << "filename #{to_string_token filename}"
end

#record_max_line_captures(max_line_captures) ⇒ Object



40
41
42
43
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 40

def record_max_line_captures(max_line_captures)
  self.max_line_captures = max_line_captures
  queue << "max_line_captures #{max_line_captures}"
end

#record_num_lines(num_lines) ⇒ Object



106
107
108
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 106

def record_num_lines(num_lines)
  queue << "num_lines #{num_lines}"
end

#record_result(type, line_number, value) ⇒ Object



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
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 47

def record_result(type, line_number, value)
  counts = recorded_results[line_number] ||= Safe::Hash.new(0)
  count  = counts[type]
  recorded_results[line_number][type] = count.next
  if count < max_line_captures
    begin
      if block_given?
        inspected = yield(value).to_str
      else
        inspected = value.inspect.to_str
      end
    rescue *StackErrors
      # this is necessary because SystemStackError won't show the backtrace of the method we tried to call
      # which means there won't be anything showing the user where this came from
      # so we need to re-raise the error to get a backtrace that shows where we came from
      # otherwise it looks like the bug is in SiB and not the user's program, see https://github.com/JoshCheek/seeing_is_believing/issues/37
      raise SystemStackError, "Calling inspect blew the stack (is it recursive w/o a base case?)"
    rescue Exception
      inspected = "#<no inspect available>"
    end
    queue << "result #{line_number} #{type} #{to_string_token inspected}"
  elsif count == max_line_captures
    queue << "maxed_result #{line_number} #{type}"
  end
  value
end

#record_ruby_version(ruby_version) ⇒ Object



36
37
38
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 36

def record_ruby_version(ruby_version)
  queue << "ruby_version #{to_string_token ruby_version}"
end

#record_sib_version(sib_version) ⇒ Object



31
32
33
34
# File 'lib/seeing_is_believing/event_stream/producer.rb', line 31

def record_sib_version(sib_version)
  @version = sib_version
  queue << "sib_version #{to_string_token sib_version}"
end