polyrun 3.2.0-java

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.
Files changed (225) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +234 -0
  3. data/CODE_OF_CONDUCT.md +26 -0
  4. data/CONTRIBUTING.md +23 -0
  5. data/LICENSE +21 -0
  6. data/README.md +251 -0
  7. data/SECURITY.md +25 -0
  8. data/bin/polyrun +6 -0
  9. data/docs/QUALITY.md +82 -0
  10. data/docs/QUALITY_CONFIGURATION.md +95 -0
  11. data/docs/SETUP_PROFILE.md +139 -0
  12. data/lib/polyrun/benchmark/pressure/cooker.rb +191 -0
  13. data/lib/polyrun/benchmark/pressure/process_sample.rb +127 -0
  14. data/lib/polyrun/benchmark/pressure.rb +144 -0
  15. data/lib/polyrun/benchmark/profile.rb +191 -0
  16. data/lib/polyrun/benchmark/report.rb +87 -0
  17. data/lib/polyrun/cli/benchmark_commands.rb +66 -0
  18. data/lib/polyrun/cli/ci_shard_hooks.rb +129 -0
  19. data/lib/polyrun/cli/ci_shard_run_command.rb +177 -0
  20. data/lib/polyrun/cli/ci_shard_run_parse.rb +68 -0
  21. data/lib/polyrun/cli/config_command.rb +42 -0
  22. data/lib/polyrun/cli/coverage_commands.rb +150 -0
  23. data/lib/polyrun/cli/coverage_merge_io.rb +150 -0
  24. data/lib/polyrun/cli/database_commands.rb +163 -0
  25. data/lib/polyrun/cli/default_run.rb +144 -0
  26. data/lib/polyrun/cli/doctor_command.rb +94 -0
  27. data/lib/polyrun/cli/env_commands.rb +43 -0
  28. data/lib/polyrun/cli/failure_commands.rb +99 -0
  29. data/lib/polyrun/cli/graph_command.rb +68 -0
  30. data/lib/polyrun/cli/help.rb +83 -0
  31. data/lib/polyrun/cli/helpers.rb +162 -0
  32. data/lib/polyrun/cli/hooks_command.rb +97 -0
  33. data/lib/polyrun/cli/init_command.rb +108 -0
  34. data/lib/polyrun/cli/mutation_command.rb +67 -0
  35. data/lib/polyrun/cli/partition_diagnostics.rb +22 -0
  36. data/lib/polyrun/cli/plan_command.rb +184 -0
  37. data/lib/polyrun/cli/prepare_command.rb +70 -0
  38. data/lib/polyrun/cli/prepare_recipe.rb +82 -0
  39. data/lib/polyrun/cli/quality_commands.rb +65 -0
  40. data/lib/polyrun/cli/queue_command.rb +161 -0
  41. data/lib/polyrun/cli/quick_command.rb +13 -0
  42. data/lib/polyrun/cli/report_commands.rb +108 -0
  43. data/lib/polyrun/cli/run_queue_command.rb +127 -0
  44. data/lib/polyrun/cli/run_shards_command.rb +164 -0
  45. data/lib/polyrun/cli/run_shards_parallel_children.rb +112 -0
  46. data/lib/polyrun/cli/run_shards_parallel_wait.rb +252 -0
  47. data/lib/polyrun/cli/run_shards_plan_boot_phases.rb +207 -0
  48. data/lib/polyrun/cli/run_shards_plan_options.rb +74 -0
  49. data/lib/polyrun/cli/run_shards_planning.rb +123 -0
  50. data/lib/polyrun/cli/run_shards_run.rb +171 -0
  51. data/lib/polyrun/cli/run_shards_worker_interrupt.rb +101 -0
  52. data/lib/polyrun/cli/skip_env.rb +34 -0
  53. data/lib/polyrun/cli/spec_quality_commands.rb +149 -0
  54. data/lib/polyrun/cli/start_bootstrap.rb +126 -0
  55. data/lib/polyrun/cli/timing_command.rb +31 -0
  56. data/lib/polyrun/cli/tool_options.rb +48 -0
  57. data/lib/polyrun/cli/validate_command.rb +26 -0
  58. data/lib/polyrun/cli.rb +272 -0
  59. data/lib/polyrun/config/dotted_path.rb +21 -0
  60. data/lib/polyrun/config/effective.rb +82 -0
  61. data/lib/polyrun/config/resolver.rb +114 -0
  62. data/lib/polyrun/config/schema.rb +39 -0
  63. data/lib/polyrun/config/suite.rb +109 -0
  64. data/lib/polyrun/config/tool_defaults.rb +42 -0
  65. data/lib/polyrun/config/tool_validation.rb +79 -0
  66. data/lib/polyrun/config/tools.rb +120 -0
  67. data/lib/polyrun/config.rb +97 -0
  68. data/lib/polyrun/coverage/cobertura_zero_lines.rb +32 -0
  69. data/lib/polyrun/coverage/collector.rb +169 -0
  70. data/lib/polyrun/coverage/collector_blob.rb +53 -0
  71. data/lib/polyrun/coverage/collector_finish.rb +98 -0
  72. data/lib/polyrun/coverage/collector_fragment_meta.rb +57 -0
  73. data/lib/polyrun/coverage/config_resolve.rb +131 -0
  74. data/lib/polyrun/coverage/example_diff.rb +184 -0
  75. data/lib/polyrun/coverage/example_diff_snapshot.rb +44 -0
  76. data/lib/polyrun/coverage/example_diff_track_filter.rb +51 -0
  77. data/lib/polyrun/coverage/file_stats_report.rb +36 -0
  78. data/lib/polyrun/coverage/filter.rb +22 -0
  79. data/lib/polyrun/coverage/formatter.rb +137 -0
  80. data/lib/polyrun/coverage/merge/formatters.rb +188 -0
  81. data/lib/polyrun/coverage/merge/formatters_html.rb +221 -0
  82. data/lib/polyrun/coverage/merge/html/_file_list.html.erb +21 -0
  83. data/lib/polyrun/coverage/merge/html/_file_section.html.erb +26 -0
  84. data/lib/polyrun/coverage/merge/html/_groups_table.html.erb +18 -0
  85. data/lib/polyrun/coverage/merge/html/_overview.html.erb +47 -0
  86. data/lib/polyrun/coverage/merge/html/report.css +147 -0
  87. data/lib/polyrun/coverage/merge/html/report.js +48 -0
  88. data/lib/polyrun/coverage/merge/html/template.html.erb +30 -0
  89. data/lib/polyrun/coverage/merge.rb +160 -0
  90. data/lib/polyrun/coverage/merge_fragment_meta.rb +58 -0
  91. data/lib/polyrun/coverage/merge_merge_two.rb +168 -0
  92. data/lib/polyrun/coverage/merge_native.rb +39 -0
  93. data/lib/polyrun/coverage/rails.rb +126 -0
  94. data/lib/polyrun/coverage/reporting.rb +41 -0
  95. data/lib/polyrun/coverage/result.rb +18 -0
  96. data/lib/polyrun/coverage/track_files.rb +150 -0
  97. data/lib/polyrun/data/cached_fixtures.rb +122 -0
  98. data/lib/polyrun/data/factory_counts.rb +48 -0
  99. data/lib/polyrun/data/factory_instrumentation.rb +50 -0
  100. data/lib/polyrun/data/fixtures.rb +68 -0
  101. data/lib/polyrun/data/parallel_provisioning.rb +93 -0
  102. data/lib/polyrun/data/snapshot.rb +84 -0
  103. data/lib/polyrun/database/clone_shards.rb +106 -0
  104. data/lib/polyrun/database/provision.rb +83 -0
  105. data/lib/polyrun/database/shard.rb +64 -0
  106. data/lib/polyrun/database/url_builder/connection/infer.rb +53 -0
  107. data/lib/polyrun/database/url_builder/connection/url_builders.rb +43 -0
  108. data/lib/polyrun/database/url_builder/connection.rb +191 -0
  109. data/lib/polyrun/database/url_builder/template_prepare.rb +21 -0
  110. data/lib/polyrun/database/url_builder.rb +188 -0
  111. data/lib/polyrun/debug.rb +81 -0
  112. data/lib/polyrun/env/ci.rb +65 -0
  113. data/lib/polyrun/error.rb +3 -0
  114. data/lib/polyrun/export/csv.rb +25 -0
  115. data/lib/polyrun/export/markdown.rb +47 -0
  116. data/lib/polyrun/graph/client.rb +64 -0
  117. data/lib/polyrun/graph/error.rb +13 -0
  118. data/lib/polyrun/graph/index.rb +75 -0
  119. data/lib/polyrun/graph/inspect.rb +58 -0
  120. data/lib/polyrun/graph/plan.rb +134 -0
  121. data/lib/polyrun/graph/planner.rb +117 -0
  122. data/lib/polyrun/graph/syntax.rb +114 -0
  123. data/lib/polyrun/graph/transaction.rb +81 -0
  124. data/lib/polyrun/graph/worker.rb +55 -0
  125. data/lib/polyrun/graph/workspace.rb +117 -0
  126. data/lib/polyrun/hooks/dsl.rb +128 -0
  127. data/lib/polyrun/hooks/shell_runner.rb +33 -0
  128. data/lib/polyrun/hooks/worker_runner.rb +27 -0
  129. data/lib/polyrun/hooks/worker_shell.rb +50 -0
  130. data/lib/polyrun/hooks.rb +190 -0
  131. data/lib/polyrun/log.rb +86 -0
  132. data/lib/polyrun/minitest_support.rb +60 -0
  133. data/lib/polyrun/mutation/candidates.rb +91 -0
  134. data/lib/polyrun/mutation/operators.rb +51 -0
  135. data/lib/polyrun/mutation/runner.rb +133 -0
  136. data/lib/polyrun/mutation/subjects.rb +88 -0
  137. data/lib/polyrun/partition/constraints.rb +80 -0
  138. data/lib/polyrun/partition/hrw.rb +70 -0
  139. data/lib/polyrun/partition/min_heap.rb +64 -0
  140. data/lib/polyrun/partition/paths.rb +175 -0
  141. data/lib/polyrun/partition/paths_build.rb +141 -0
  142. data/lib/polyrun/partition/plan.rb +250 -0
  143. data/lib/polyrun/partition/plan_lpt.rb +91 -0
  144. data/lib/polyrun/partition/plan_sharding.rb +56 -0
  145. data/lib/polyrun/partition/reports.rb +139 -0
  146. data/lib/polyrun/partition/stable_shuffle.rb +18 -0
  147. data/lib/polyrun/partition/suite.rb +120 -0
  148. data/lib/polyrun/partition/timing_diagnostics.rb +138 -0
  149. data/lib/polyrun/partition/timing_keys.rb +86 -0
  150. data/lib/polyrun/prepare/artifacts.rb +40 -0
  151. data/lib/polyrun/prepare/assets.rb +64 -0
  152. data/lib/polyrun/process_stdio.rb +96 -0
  153. data/lib/polyrun/quality/command.rb +84 -0
  154. data/lib/polyrun/quality/measure.rb +38 -0
  155. data/lib/polyrun/quality/observe.rb +53 -0
  156. data/lib/polyrun/quality/observe_hook.rb +103 -0
  157. data/lib/polyrun/quality/sandbox.rb +70 -0
  158. data/lib/polyrun/queue/duration.rb +30 -0
  159. data/lib/polyrun/queue/file_store.rb +413 -0
  160. data/lib/polyrun/queue/file_store_intent.rb +188 -0
  161. data/lib/polyrun/queue/file_store_pending.rb +48 -0
  162. data/lib/polyrun/queue/worker_loop.rb +73 -0
  163. data/lib/polyrun/quick/assertions.rb +32 -0
  164. data/lib/polyrun/quick/config.rb +56 -0
  165. data/lib/polyrun/quick/errors.rb +6 -0
  166. data/lib/polyrun/quick/example_group.rb +111 -0
  167. data/lib/polyrun/quick/example_runner.rb +129 -0
  168. data/lib/polyrun/quick/matchers.rb +159 -0
  169. data/lib/polyrun/quick/matchers_predicates.rb +122 -0
  170. data/lib/polyrun/quick/reporter.rb +76 -0
  171. data/lib/polyrun/quick/runner.rb +224 -0
  172. data/lib/polyrun/quick.rb +1 -0
  173. data/lib/polyrun/railtie.rb +7 -0
  174. data/lib/polyrun/reporting/failure_merge.rb +171 -0
  175. data/lib/polyrun/reporting/junit.rb +126 -0
  176. data/lib/polyrun/reporting/junit_emit.rb +58 -0
  177. data/lib/polyrun/reporting/junit_report.rb +87 -0
  178. data/lib/polyrun/reporting/rspec_failure_fragment_formatter.rb +95 -0
  179. data/lib/polyrun/reporting/rspec_junit.rb +39 -0
  180. data/lib/polyrun/rspec_support/active_record_timeout_recovery.rb +51 -0
  181. data/lib/polyrun/rspec_support/example_debug.rb +167 -0
  182. data/lib/polyrun/rspec_support/example_debug_instrumentation.rb +73 -0
  183. data/lib/polyrun/rspec_support/sharded_formatter_compat.rb +57 -0
  184. data/lib/polyrun/rspec_support.rb +110 -0
  185. data/lib/polyrun/spec_quality/config.rb +139 -0
  186. data/lib/polyrun/spec_quality/fragment.rb +39 -0
  187. data/lib/polyrun/spec_quality/merge.rb +78 -0
  188. data/lib/polyrun/spec_quality/minitest_hook.rb +42 -0
  189. data/lib/polyrun/spec_quality/plan_loader.rb +47 -0
  190. data/lib/polyrun/spec_quality/profile.rb +102 -0
  191. data/lib/polyrun/spec_quality/report.rb +363 -0
  192. data/lib/polyrun/spec_quality/rspec_hook.rb +55 -0
  193. data/lib/polyrun/spec_quality/sql_counter.rb +34 -0
  194. data/lib/polyrun/spec_quality.rb +212 -0
  195. data/lib/polyrun/sus_support.rb +55 -0
  196. data/lib/polyrun/templates/POLYRUN.md +51 -0
  197. data/lib/polyrun/templates/ci_matrix.polyrun.yml +22 -0
  198. data/lib/polyrun/templates/minimal_gem.polyrun.yml +13 -0
  199. data/lib/polyrun/templates/polyrun_hooks_spec_quality.rb +12 -0
  200. data/lib/polyrun/templates/polyrun_spec_quality.yml +20 -0
  201. data/lib/polyrun/templates/rails_prepare.polyrun.yml +51 -0
  202. data/lib/polyrun/templates/rails_prepare_task.rake +16 -0
  203. data/lib/polyrun/timing/merge.rb +36 -0
  204. data/lib/polyrun/timing/rspec_example_formatter.rb +60 -0
  205. data/lib/polyrun/timing/stats.rb +76 -0
  206. data/lib/polyrun/timing/summary.rb +78 -0
  207. data/lib/polyrun/timing/variance_report.rb +51 -0
  208. data/lib/polyrun/version.rb +3 -0
  209. data/lib/polyrun/worker_output.rb +181 -0
  210. data/lib/polyrun/worker_output_forwarders.rb +94 -0
  211. data/lib/polyrun/worker_ping.rb +77 -0
  212. data/lib/polyrun.rb +59 -0
  213. data/polyrun.gemspec +49 -0
  214. data/sig/polyrun/cli.rbs +6 -0
  215. data/sig/polyrun/config.rbs +20 -0
  216. data/sig/polyrun/debug.rbs +12 -0
  217. data/sig/polyrun/log.rbs +12 -0
  218. data/sig/polyrun/minitest_support.rbs +7 -0
  219. data/sig/polyrun/quick.rbs +19 -0
  220. data/sig/polyrun/rspec_support.rbs +21 -0
  221. data/sig/polyrun/sus_support.rbs +7 -0
  222. data/sig/polyrun/worker_output.rbs +11 -0
  223. data/sig/polyrun/worker_ping.rbs +10 -0
  224. data/sig/polyrun.rbs +11 -0
  225. metadata +463 -0
@@ -0,0 +1,188 @@
1
+ # rubocop:disable Metrics/ModuleLength -- claim/ack/reclaim intent recovery beside FileStore
2
+ module Polyrun
3
+ module Queue
4
+ class FileStore
5
+ INTENT_BASENAME = "intent.json"
6
+
7
+ private
8
+
9
+ def intent_path
10
+ File.join(@root, INTENT_BASENAME)
11
+ end
12
+
13
+ def write_intent!(payload)
14
+ atomic_write(intent_path, JSON.generate(payload), fsync: true)
15
+ end
16
+
17
+ def clear_intent!
18
+ File.unlink(intent_path)
19
+ rescue Errno::ENOENT
20
+ nil
21
+ end
22
+
23
+ def recover_intent!
24
+ return unless File.file?(intent_path)
25
+
26
+ intent = JSON.parse(File.read(intent_path))
27
+ case intent["op"].to_s
28
+ when "claim"
29
+ recover_claim_intent!(intent)
30
+ when "ack", "ack_paths"
31
+ recover_ack_intent!(intent)
32
+ when "reclaim", "reclaim_lease"
33
+ recover_reclaim_intent!(intent)
34
+ else
35
+ Polyrun::Log.warn "polyrun queue: dropping unknown intent op=#{intent["op"].inspect}"
36
+ end
37
+ clear_intent!
38
+ rescue JSON::ParserError, TypeError
39
+ Polyrun::Log.warn "polyrun queue: dropping corrupt intent at #{intent_path}"
40
+ clear_intent!
41
+ end
42
+
43
+ def recover_claim_intent!(intent)
44
+ lease_id = intent["lease_id"].to_s
45
+ leases = read_leases
46
+ return if leases.key?(lease_id)
47
+
48
+ paths = Array(intent["paths"])
49
+ meta = load_meta!
50
+ return_paths_to_pending!(meta, paths)
51
+ write_meta!(meta)
52
+ append_ledger(
53
+ "RECOVER_CLAIM" => lease_id,
54
+ "worker_id" => intent["worker_id"],
55
+ "paths" => paths
56
+ )
57
+ end
58
+
59
+ def recover_ack_intent!(intent)
60
+ lease_id = intent["lease_id"].to_s
61
+ leases = read_leases
62
+ lease = leases[lease_id]
63
+ return unless lease
64
+
65
+ paths = Array(intent["paths"])
66
+ paths = Array(lease["paths"]) if paths.empty?
67
+ apply_ack_side_effects!(leases, lease_id, lease, paths, intent["worker_id"])
68
+ end
69
+
70
+ def recover_reclaim_intent!(intent)
71
+ lease_id = intent["lease_id"].to_s
72
+ leases = read_leases
73
+ lease = leases[lease_id]
74
+ return unless lease
75
+
76
+ paths = Array(intent["paths"])
77
+ paths = Array(lease["paths"]) if paths.empty?
78
+ bumped = lease.merge("attempts" => intent["attempts"] || lease["attempts"])
79
+ leases.delete(lease_id)
80
+ write_leases!(leases)
81
+ meta = load_meta!
82
+ quarantine_or_return_paths!(meta, paths, bumped)
83
+ write_meta!(meta)
84
+ append_ledger(
85
+ "RECOVER_RECLAIM" => lease_id,
86
+ "worker_id" => lease["worker_id"],
87
+ "paths" => paths,
88
+ "attempts" => bumped["attempts"]
89
+ )
90
+ end
91
+
92
+ def apply_ack_side_effects!(leases, lease_id, lease, paths, worker_id)
93
+ append_done_lines!(paths)
94
+ clear_path_attempts!(paths)
95
+ meta = load_meta!
96
+ meta["done_count"] = Integer(meta["done_count"]) + paths.size
97
+ write_meta!(meta)
98
+ remaining = Array(lease["paths"]).map(&:to_s) - paths.map(&:to_s)
99
+ if remaining.empty?
100
+ leases.delete(lease_id)
101
+ else
102
+ lease["paths"] = remaining
103
+ leases[lease_id] = lease
104
+ end
105
+ write_leases!(leases)
106
+ append_ledger("ACK" => lease_id, "worker_id" => worker_id.to_s, "paths" => paths)
107
+ end
108
+
109
+ def quarantine_or_return_paths!(meta, paths, lease)
110
+ attempts_h = read_path_attempts
111
+ to_pending = []
112
+ poisoned = []
113
+ max_attempts = queue_max_attempts
114
+ Array(paths).each do |path|
115
+ key = path.to_s
116
+ n = Integer(attempts_h[key] || 0) + 1
117
+ attempts_h[key] = n
118
+ if max_attempts.positive? && n >= max_attempts
119
+ poisoned << [key, n]
120
+ else
121
+ to_pending << key
122
+ end
123
+ end
124
+ write_path_attempts!(attempts_h)
125
+ poisoned.each do |key, n|
126
+ append_failed_lines!(
127
+ [key],
128
+ "worker_id" => lease["worker_id"],
129
+ "attempts" => n
130
+ )
131
+ append_ledger("POISON" => lease["worker_id"], "attempts" => n, "paths" => [key])
132
+ end
133
+ return_paths_to_pending!(meta, to_pending)
134
+ end
135
+
136
+ def clear_path_attempts!(paths)
137
+ attempts_h = read_path_attempts
138
+ Array(paths).each { |p| attempts_h.delete(p.to_s) }
139
+ write_path_attempts!(attempts_h)
140
+ end
141
+
142
+ def queue_max_attempts
143
+ raw = ENV["POLYRUN_QUEUE_MAX_ATTEMPTS"].to_s.strip
144
+ return 3 if raw.empty?
145
+
146
+ n = Integer(raw, exception: false)
147
+ (n && n >= 1) ? n : 3
148
+ end
149
+
150
+ def path_attempts_path
151
+ File.join(@root, "path_attempts.json")
152
+ end
153
+
154
+ def read_path_attempts
155
+ return {} unless File.file?(path_attempts_path)
156
+
157
+ data = JSON.parse(File.read(path_attempts_path))
158
+ data.is_a?(Hash) ? data : {}
159
+ rescue JSON::ParserError
160
+ {}
161
+ end
162
+
163
+ def write_path_attempts!(hash)
164
+ atomic_write(path_attempts_path, JSON.generate(hash))
165
+ end
166
+
167
+ def failed_path
168
+ File.join(@root, "failed.jsonl")
169
+ end
170
+
171
+ def append_failed_lines!(paths, lease)
172
+ return if paths.empty?
173
+
174
+ File.open(failed_path, "a") do |io|
175
+ paths.each do |p|
176
+ io.puts(JSON.generate(
177
+ "path" => p.to_s,
178
+ "worker_id" => lease["worker_id"],
179
+ "attempts" => Integer(lease["attempts"] || 0),
180
+ "at" => Time.now.utc.iso8601
181
+ ))
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
188
+ # rubocop:enable Metrics/ModuleLength
@@ -0,0 +1,48 @@
1
+ module Polyrun
2
+ module Queue
3
+ class FileStore
4
+ private
5
+
6
+ def sorted_chunk_files
7
+ Dir.glob(File.join(pending_dir, "[0-9][0-9][0-9][0-9][0-9][0-9].json")).sort
8
+ end
9
+
10
+ def take_pending_batch!(meta, batch_size)
11
+ remaining = Integer(meta["pending_count"])
12
+ return [] if remaining <= 0 || batch_size <= 0
13
+
14
+ batch = []
15
+ files = sorted_chunk_files
16
+ while batch.size < batch_size
17
+ break if files.empty?
18
+
19
+ head = files.first
20
+ append_from_next_chunk!(batch, batch_size, head)
21
+ files.shift unless File.file?(head)
22
+ end
23
+
24
+ meta["pending_count"] = [remaining - batch.size, 0].max
25
+ if meta["pending_count"].positive? && sorted_chunk_files.empty?
26
+ raise Polyrun::Error,
27
+ "queue corrupt: pending_count=#{meta["pending_count"]} but no pending chunk files under #{pending_dir}"
28
+ end
29
+
30
+ batch
31
+ end
32
+
33
+ def append_from_next_chunk!(batch, batch_size, path)
34
+ chunk = JSON.parse(File.read(path))
35
+ raise Polyrun::Error, "corrupt queue chunk: #{path}" unless chunk.is_a?(Array)
36
+
37
+ need = batch_size - batch.size
38
+ taken = chunk.shift(need)
39
+ batch.concat(taken)
40
+ if chunk.empty?
41
+ FileUtils.rm_f(path)
42
+ else
43
+ atomic_write(path, JSON.generate(chunk))
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,73 @@
1
+ module Polyrun
2
+ module Queue
3
+ # Claim / run / ack loop for +polyrun run-queue+ workers (forked or direct).
4
+ #
5
+ # Successful batches stay one command invocation. On failure, paths are re-run one at a time so
6
+ # already-passing paths can be acked without reclaiming them with a poison neighbor.
7
+ module WorkerLoop
8
+ module_function
9
+
10
+ def run(store:, worker_id:, batch:, cmd:, on_failure:)
11
+ batches_ok = 0
12
+ batches_fail = 0
13
+ loop do
14
+ claim = store.claim!(worker_id: worker_id, batch_size: batch)
15
+ paths = claim["paths"] || []
16
+ break if paths.empty?
17
+
18
+ store.heartbeat!(lease_id: claim["lease_id"], worker_id: worker_id)
19
+ code = run_batch(cmd, paths)
20
+ if code == 0
21
+ store.ack!(lease_id: claim["lease_id"], worker_id: worker_id)
22
+ batches_ok += 1
23
+ next
24
+ end
25
+
26
+ outcome = isolate_failed_batch!(
27
+ store: store,
28
+ worker_id: worker_id,
29
+ lease_id: claim["lease_id"],
30
+ paths: paths,
31
+ cmd: cmd,
32
+ on_failure: on_failure
33
+ )
34
+ batches_ok += outcome[:ok]
35
+ batches_fail += outcome[:fail]
36
+ return {ok: batches_ok, fail: batches_fail, exit_code: outcome[:exit_code]} if outcome[:stop]
37
+ end
38
+ {ok: batches_ok, fail: batches_fail, exit_code: 0}
39
+ rescue Polyrun::Error => e
40
+ Polyrun::Log.warn "polyrun run-queue worker #{worker_id}: #{e.message}"
41
+ {ok: batches_ok, fail: batches_fail, exit_code: 2}
42
+ end
43
+
44
+ def run_batch(cmd, paths)
45
+ system(*cmd, *paths) ? 0 : ($?.exitstatus || 1)
46
+ end
47
+
48
+ def isolate_failed_batch!(store:, worker_id:, lease_id:, paths:, cmd:, on_failure:)
49
+ ok = 0
50
+ paths.each do |path|
51
+ store.heartbeat!(lease_id: lease_id, worker_id: worker_id)
52
+ code = run_batch(cmd, [path])
53
+ if code == 0
54
+ store.ack_paths!(lease_id: lease_id, worker_id: worker_id, paths: [path])
55
+ ok += 1
56
+ next
57
+ end
58
+
59
+ if on_failure.to_s == "requeue"
60
+ store.reclaim_lease!(lease_id)
61
+ return {ok: ok, fail: 1, exit_code: 1, stop: true}
62
+ end
63
+
64
+ return {ok: ok, fail: 1, exit_code: code.zero? ? 1 : code, stop: true}
65
+ end
66
+
67
+ # Every path passed on the isolating re-run; lease should already be empty.
68
+ {ok: ok.positive? ? 1 : 0, fail: 0, exit_code: 0, stop: false}
69
+ end
70
+ private_class_method :isolate_failed_batch!
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,32 @@
1
+ require_relative "errors"
2
+
3
+ module Polyrun
4
+ module Quick
5
+ module Assertions
6
+ def assert(condition, message = "assertion failed")
7
+ raise AssertionFailed, message unless condition
8
+ end
9
+
10
+ def assert_equal(expected, actual, message = nil)
11
+ return if expected == actual
12
+
13
+ raise AssertionFailed,
14
+ message || "expected #{expected.inspect}, got #{actual.inspect}"
15
+ end
16
+
17
+ def assert_nil(obj, message = nil)
18
+ return if obj.nil?
19
+
20
+ raise AssertionFailed, message || "expected nil, got #{obj.inspect}"
21
+ end
22
+
23
+ def assert_raises(exception_class = StandardError)
24
+ yield
25
+ rescue exception_class
26
+ nil
27
+ else
28
+ raise AssertionFailed, "expected #{exception_class} to be raised"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,56 @@
1
+ module Polyrun
2
+ module Quick
3
+ # Optional host file +config/polyrun_quick.rb+ evaluated before Quick examples run.
4
+ class Config
5
+ PATH = "config/polyrun_quick.rb"
6
+
7
+ attr_reader :before_suite_hooks, :after_suite_hooks, :load_paths
8
+
9
+ def self.load!(root: Dir.pwd)
10
+ cfg = new
11
+ path = File.join(File.expand_path(root), PATH)
12
+ return cfg unless File.file?(path)
13
+
14
+ cfg.instance_eval(File.read(path), path)
15
+ cfg
16
+ end
17
+
18
+ def initialize
19
+ @before_suite_hooks = []
20
+ @after_suite_hooks = []
21
+ @load_paths = []
22
+ end
23
+
24
+ def before_suite(&block)
25
+ @before_suite_hooks << block if block
26
+ end
27
+
28
+ def after_suite(&block)
29
+ @after_suite_hooks << block if block
30
+ end
31
+
32
+ def add_load_path(path)
33
+ @load_paths << path.to_s
34
+ end
35
+
36
+ def apply_load_paths!(root:)
37
+ base = File.expand_path(root)
38
+ @load_paths.each do |rel|
39
+ abs = File.expand_path(rel, base)
40
+ next unless File.directory?(abs)
41
+ next if $LOAD_PATH.include?(abs)
42
+
43
+ $LOAD_PATH.unshift(abs)
44
+ end
45
+ end
46
+
47
+ def run_before_suite!
48
+ @before_suite_hooks.each(&:call)
49
+ end
50
+
51
+ def run_after_suite!(status)
52
+ @after_suite_hooks.each { |h| h.call(status) }
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,6 @@
1
+ module Polyrun
2
+ module Quick
3
+ class Error < Polyrun::Error; end
4
+ class AssertionFailed < Error; end
5
+ end
6
+ end
@@ -0,0 +1,111 @@
1
+ module Polyrun
2
+ module Quick
3
+ # One +describe+ / +with+ block (possibly nested). Holds examples and hooks.
4
+ class ExampleGroup
5
+ attr_reader :name, :parent, :children, :examples, :before_hooks, :after_hooks, :around_hooks,
6
+ :lets, :let_bang_order, :described_target
7
+
8
+ def initialize(name, parent: nil, described_target: nil)
9
+ @name = name_label(name)
10
+ @described_target = described_target.nil? ? infer_described_target(name) : described_target
11
+ @parent = parent
12
+ @children = []
13
+ @examples = []
14
+ @before_hooks = []
15
+ @after_hooks = []
16
+ @around_hooks = []
17
+ @lets = {}
18
+ @let_bang_order = []
19
+ maybe_default_subject!
20
+ end
21
+
22
+ def full_name
23
+ return @name if parent.nil?
24
+
25
+ "#{parent.full_name} #{@name}".strip
26
+ end
27
+
28
+ def describe(name, &block)
29
+ child = ExampleGroup.new(name, parent: self)
30
+ @children << child
31
+ child.instance_eval(&block) if block
32
+ child
33
+ end
34
+
35
+ alias_method :with, :describe
36
+
37
+ def it(description, &block)
38
+ @examples << [description.to_s, block]
39
+ end
40
+
41
+ alias_method :test, :it
42
+
43
+ def before(&block)
44
+ @before_hooks << block
45
+ end
46
+
47
+ def after(&block)
48
+ @after_hooks << block
49
+ end
50
+
51
+ def around(&block)
52
+ @around_hooks << block
53
+ end
54
+
55
+ def let(name, &block)
56
+ @lets[name.to_sym] = block
57
+ end
58
+
59
+ def let!(name, &block)
60
+ sym = name.to_sym
61
+ @lets[sym] = block
62
+ @let_bang_order << sym
63
+ end
64
+
65
+ def subject(&block)
66
+ if block
67
+ let(:subject, &block)
68
+ else
69
+ # Used from examples via ExampleRunner method; group-level call without block is invalid.
70
+ raise ArgumentError, "subject requires a block at describe time"
71
+ end
72
+ end
73
+
74
+ def each_example_with_ancestors(ancestors = [], &visitor)
75
+ chain = ancestors + [self]
76
+ @examples.each do |desc, block|
77
+ visitor.call(chain, desc, block)
78
+ end
79
+ @children.each do |child|
80
+ child.each_example_with_ancestors(chain, &visitor)
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def name_label(name)
87
+ return name.to_s unless name.is_a?(Module)
88
+
89
+ name.name || name.to_s
90
+ end
91
+
92
+ def infer_described_target(name)
93
+ name.is_a?(Module) ? name : nil
94
+ end
95
+
96
+ def maybe_default_subject!
97
+ return unless @described_target
98
+ return if @lets.key?(:subject)
99
+
100
+ target = @described_target
101
+ @lets[:subject] = -> {
102
+ if target.respond_to?(:new)
103
+ target.new
104
+ else
105
+ target
106
+ end
107
+ }
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,129 @@
1
+ require_relative "../worker_ping"
2
+ require_relative "assertions"
3
+ require_relative "errors"
4
+ require_relative "matchers"
5
+
6
+ Polyrun::WorkerPing.ensure_interval_ping_thread!
7
+
8
+ module Polyrun
9
+ module Quick
10
+ # Per-example execution: merged lets, hooks, assertions, optional Capybara::DSL.
11
+ class ExampleRunner
12
+ include Assertions
13
+ include Matchers
14
+
15
+ def initialize(reporter)
16
+ @reporter = reporter
17
+ @_let_cache = {}
18
+ end
19
+
20
+ def run(group_name:, description:, ancestor_chain:, block:)
21
+ @_let_cache = {}
22
+ merge_lets_from_chain(ancestor_chain)
23
+ define_let_methods!
24
+ run_let_bangs_from_chain
25
+ extend_capybara_if_enabled!
26
+ qloc = quick_example_location(block)
27
+ Polyrun::WorkerPing.ping!(location: qloc)
28
+ Polyrun::SpecQuality.start_example!(location: qloc) if Polyrun::SpecQuality.started?
29
+ error = nil
30
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
31
+ begin
32
+ run_before_hooks_from_chain(ancestor_chain)
33
+ invoke_arounds(ancestor_chain.flat_map(&:around_hooks), 0) { instance_eval(&block) }
34
+ @reporter.pass(group_name, description, duration_seconds: elapsed_since(started))
35
+ rescue AssertionFailed => e
36
+ error = e
37
+ @reporter.fail(group_name, description, e, duration_seconds: elapsed_since(started))
38
+ rescue => e
39
+ error = e
40
+ @reporter.error(group_name, description, e, duration_seconds: elapsed_since(started))
41
+ ensure
42
+ Polyrun::SpecQuality.finish_example!(location: qloc) if Polyrun::SpecQuality.started?
43
+ run_after_hooks_from_chain(ancestor_chain, error: error)
44
+ reset_capybara_if_enabled!
45
+ @_let_cache = {}
46
+ Polyrun::WorkerPing.ping!(location: qloc)
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def elapsed_since(started)
53
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
54
+ end
55
+
56
+ def quick_example_location(block)
57
+ loc = block&.source_location
58
+ loc ? "#{loc[0]}:#{loc[1]}" : nil
59
+ end
60
+
61
+ def merge_lets_from_chain(ancestor_chain)
62
+ @merged_lets = {}
63
+ ancestor_chain.each do |g|
64
+ @merged_lets.merge!(g.lets)
65
+ end
66
+ @let_bang_order = []
67
+ ancestor_chain.each do |g|
68
+ @let_bang_order.concat(g.let_bang_order)
69
+ end
70
+ end
71
+
72
+ def define_let_methods!
73
+ @merged_lets.each do |sym, proc|
74
+ define_singleton_method(sym) do
75
+ @_let_cache[sym] ||= instance_eval(&proc) # rubocop:disable ThreadSafety/ClassInstanceVariable -- per-example runner memo
76
+ end
77
+ end
78
+ end
79
+
80
+ def run_let_bangs_from_chain
81
+ @let_bang_order.each { |sym| public_send(sym) }
82
+ end
83
+
84
+ def run_before_hooks_from_chain(ancestor_chain)
85
+ ancestor_chain.each do |g|
86
+ g.before_hooks.each { |h| instance_eval(&h) }
87
+ end
88
+ end
89
+
90
+ def run_after_hooks_from_chain(ancestor_chain, error: nil)
91
+ ancestor_chain.reverse_each do |g|
92
+ g.after_hooks.reverse_each { |h| instance_exec(error, &h) }
93
+ end
94
+ end
95
+
96
+ def invoke_arounds(arounds, index, &body)
97
+ if index >= arounds.size
98
+ body.call
99
+ return
100
+ end
101
+
102
+ called = false
103
+ run = lambda {
104
+ called = true
105
+ invoke_arounds(arounds, index + 1, &body)
106
+ }
107
+ instance_exec(run, &arounds[index])
108
+ raise AssertionFailed, "around hook did not run the example" unless called
109
+ end
110
+
111
+ def extend_capybara_if_enabled!
112
+ return unless Quick.capybara?
113
+ return unless defined?(::Capybara)
114
+ return unless defined?(::Capybara::DSL)
115
+
116
+ extend ::Capybara::DSL
117
+ end
118
+
119
+ def reset_capybara_if_enabled!
120
+ return unless Quick.capybara?
121
+ return unless defined?(::Capybara)
122
+
123
+ ::Capybara.reset_sessions!
124
+ rescue
125
+ # Driver/session may be absent in non-Capybara runs
126
+ end
127
+ end
128
+ end
129
+ end