crspec 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,39 +3,62 @@
3
3
  module Crspec
4
4
  module Formatters
5
5
  class ProgressFormatter
6
+ FLUSH_THRESHOLD = 64
7
+ FLUSH_INTERVAL = 0.2
8
+
6
9
  def initialize(output = $stdout, color: output.respond_to?(:tty?) && output.tty?)
7
10
  @output = output
8
11
  @color = color
9
12
  @mutex = Mutex.new
13
+ @buffer = +""
14
+ @last_flush = Process.clock_gettime(Process::CLOCK_MONOTONIC)
10
15
  end
11
16
 
12
17
  def example_passed(_example)
13
- @mutex.synchronize do
14
- @output.print @color ? "\e[32m.\e[0m" : "."
15
- @output.flush
16
- end
18
+ record(@color ? "\e[32m.\e[0m" : ".")
17
19
  end
18
20
 
19
21
  def example_failed(_example)
20
- @mutex.synchronize do
21
- @output.print @color ? "\e[31mF\e[0m" : "F"
22
- @output.flush
23
- end
22
+ record(@color ? "\e[31mF\e[0m" : "F")
24
23
  end
25
24
 
26
25
  def example_pending(_example)
27
- @mutex.synchronize do
28
- @output.print @color ? "\e[33m*\e[0m" : "*"
29
- @output.flush
30
- end
26
+ record(@color ? "\e[33m*\e[0m" : "*")
31
27
  end
32
28
 
33
- def start; end
29
+ def start
30
+ @last_flush = Process.clock_gettime(Process::CLOCK_MONOTONIC)
31
+ end
34
32
 
35
33
  def finish
34
+ chunk = @mutex.synchronize { swap_buffer }
35
+ @output.print(chunk) unless chunk.empty?
36
+ @output.puts
37
+ @output.flush
38
+ end
39
+
40
+ private
41
+
42
+ def record(token)
43
+ chunk = nil
36
44
  @mutex.synchronize do
37
- @output.puts
45
+ @buffer << token
46
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
47
+ if @buffer.size >= FLUSH_THRESHOLD || now - @last_flush >= FLUSH_INTERVAL
48
+ @last_flush = now
49
+ chunk = swap_buffer
50
+ end
38
51
  end
52
+ return unless chunk
53
+
54
+ @output.print(chunk)
55
+ @output.flush
56
+ end
57
+
58
+ def swap_buffer
59
+ chunk = @buffer
60
+ @buffer = +""
61
+ chunk
39
62
  end
40
63
  end
41
64