async-rspec 1.8.0 → 1.9.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6b16775a1318cfc18c1d2f08ce937291e5b5a76f233a83cad775cc65e4a85444
4
- data.tar.gz: fbc4009e8b7df715903c33e0e57a863182f3000c9f7e39b44177f759da4b4713
3
+ metadata.gz: f790c28cf9e2685e31a1ee197071be8cf64a0ef31f88ee6f627536733b7a2b1f
4
+ data.tar.gz: 290e3da14a70db132cfe92d493832ea2f0eec4a7a2543666ec33781912e88fb7
5
5
  SHA512:
6
- metadata.gz: 4079b21429b84d174e922ace80aad05a126e2ea68ee47248a46f13585a3ac24e898eb767ba0bb039d80b9aacdedeb28b64bd0ea6a265de3ae457c6e45c2fa877
7
- data.tar.gz: 3b231e51b87424651f97e619a30f61726da951f5a22984eb77770b200d626a862f9ca08f6dc0161f17a32daee13ab051ebf52b3bf1adbc6dee3a76d420523d89
6
+ metadata.gz: f469938b965ea3aa5e99bab4cd903f0311a0a7975df1bffbfa8dcd757326cda2f7c75f5353e7221672d5c1a2ed6e3811de5466aa65e447521f14230c9053bb31
7
+ data.tar.gz: 3fe986d225ed92222f91d3766b1904c9989ba05b99ccf83640084e90abb6810a94fd4c7459daae8915fa60caba0a769741c316a13c62ba76d17facda5c99b54f
@@ -60,16 +60,19 @@ module Async
60
60
  yield "expected within #{limit}"
61
61
  end
62
62
  when Integer
63
- unless value <= limit
64
- yield "expected at most #{limit}"
63
+ unless value == limit
64
+ yield "expected exactly #{limit}"
65
65
  end
66
66
  end
67
67
  end
68
68
 
69
69
  def matches?(given_proc)
70
- return true unless trace = Trace.capture(&given_proc)
70
+ return true unless trace = Trace.capture(@allocations.keys, &given_proc)
71
71
 
72
- if total = trace.total
72
+ if @count or @size
73
+ # If the spec specifies a total limit, we have a limit which we can enforce which takes all allocations into account:
74
+ total = trace.total
75
+
73
76
  check(total.count, @count) do |expected|
74
77
  @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} instances"
75
78
  end if @count
@@ -77,10 +80,15 @@ module Async
77
80
  check(total.size, @size) do |expected|
78
81
  @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} bytes"
79
82
  end if @size
83
+ else
84
+ # Otherwise unspecified allocations are considered an error:
85
+ trace.ignored.each do |klass, allocation|
86
+ @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, but it was not specified"
87
+ end
80
88
  end
81
89
 
82
- @allocations.each do |klass, acceptable|
83
- next unless allocation = trace.allocated[klass]
90
+ trace.allocated.each do |klass, allocation|
91
+ next unless acceptable = @allocations[klass]
84
92
 
85
93
  check(allocation.count, acceptable[:count]) do |expected|
86
94
  @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} instances"
@@ -99,8 +107,8 @@ module Async
99
107
  end
100
108
  end
101
109
 
102
- def limit_allocations(allocations)
103
- LimitAllocations.new(allocations)
110
+ def limit_allocations(*args)
111
+ LimitAllocations.new(*args)
104
112
  end
105
113
  end
106
114
  end
@@ -24,9 +24,17 @@ module Async
24
24
  module RSpec
25
25
  module Memory
26
26
  Allocation = Struct.new(:count, :size) do
27
+ SLOT_SIZE = 40
28
+
27
29
  def << object
28
30
  self.count += 1
29
- self.size += ObjectSpace.memsize_of(object)
31
+
32
+ # We don't want to force specs to take the slot size into account.
33
+ self.size += ObjectSpace.memsize_of(object) - SLOT_SIZE
34
+ end
35
+
36
+ def self.default_hash
37
+ Hash.new{|h,k| h[k] = Allocation.new(0, 0)}
30
38
  end
31
39
  end
32
40
 
@@ -36,8 +44,8 @@ module Async
36
44
  end
37
45
 
38
46
  if supported?
39
- def self.capture(&block)
40
- self.new.tap do |trace|
47
+ def self.capture(*args, &block)
48
+ self.new(*args).tap do |trace|
41
49
  trace.capture(&block)
42
50
  end
43
51
  end
@@ -49,9 +57,13 @@ module Async
49
57
  end
50
58
  end
51
59
 
52
- def initialize
53
- @allocated = Hash.new{|h,k| h[k] = Allocation.new(0, 0)}
54
- @retained = Hash.new{|h,k| h[k] = Allocation.new(0, 0)}
60
+ def initialize(klasses)
61
+ @klasses = klasses
62
+
63
+ @allocated = Allocation.default_hash
64
+ @retained = Allocation.default_hash
65
+
66
+ @ignored = Allocation.default_hash
55
67
 
56
68
  @total = Allocation.new(0, 0)
57
69
  end
@@ -59,6 +71,8 @@ module Async
59
71
  attr :allocated
60
72
  attr :retained
61
73
 
74
+ attr :ignored
75
+
62
76
  attr :total
63
77
 
64
78
  def current_objects(generation)
@@ -73,6 +87,10 @@ module Async
73
87
  return allocations
74
88
  end
75
89
 
90
+ def find_base(object)
91
+ @klasses.find{|klass| object.is_a? klass}
92
+ end
93
+
76
94
  def capture(&block)
77
95
  GC.start
78
96
 
@@ -92,13 +110,25 @@ module Async
92
110
 
93
111
  # All allocated objects, including those freed in the last GC:
94
112
  allocated.each do |object|
95
- @allocated[object.class] << object
113
+ if klass = find_base(object)
114
+ @allocated[klass] << object
115
+ else
116
+ # If the user specified classes, but we can't pin this allocation to a specific class, we issue a warning.
117
+ if @klasses.any?
118
+ warn "Ignoring allocation of #{object.class} at #{ObjectSpace.allocation_sourcefile(object)}:#{ObjectSpace.allocation_sourceline(object)}"
119
+ end
120
+
121
+ @ignored[object.class] << object
122
+ end
123
+
96
124
  @total << object
97
125
  end
98
126
 
99
127
  # Retained objects are still alive after a final GC:
100
128
  retained.each do |object|
101
- @retained[object.class] << object
129
+ if klass = find_base(object)
130
+ @retained[klass] << object
131
+ end
102
132
  end
103
133
  end
104
134
  end
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Async
22
22
  module RSpec
23
- VERSION = "1.8.0"
23
+ VERSION = "1.9.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-05 00:00:00.000000000 Z
11
+ date: 2018-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec