cel-rs-rb 0.3.1 → 0.4.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.
data/spec/cel_spec.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "spec_helper"
4
4
  require "time"
5
5
  require "open3"
6
+ require "timeout"
6
7
 
7
8
  RSpec.describe CEL do
8
9
  describe ".compile" do
@@ -20,7 +21,11 @@ RSpec.describe CEL do
20
21
  strings: ["'TacoCat'.lowerAscii()", "tacocat"],
21
22
  lists: ["[{'n': 8}, {'n': 2}, {'n': 5}].sortBy(x, x.n).map(x, x.n)", [2, 5, 8]],
22
23
  math: ["math.greatest(2, 9, -3)", 9],
23
- encoders: ["base64.encode(b'hi')", "aGk="]
24
+ encoders: ["base64.encode(b'hi')", "aGk="],
25
+ sets: ["[sets.contains([1, 2], [2]), sets.contains([1], [2]), " \
26
+ "sets.equivalent([1, 1, 2], [2.0, 1u]), sets.equivalent([1], [2]), " \
27
+ "sets.intersects([1, 2], [2, 3]), sets.intersects([1, 2], [3])]",
28
+ [true, false, true, false, true, false]]
24
29
  }
25
30
 
26
31
  tests.each do |extension, (expression, expected)|
@@ -48,6 +53,9 @@ RSpec.describe CEL do
48
53
  expect(enabled.execute(context)).to eq("abc")
49
54
  expect { CEL.compile("'ABC'.lowerAscii()").execute(context) }.to raise_error(CEL::ExecutionError)
50
55
  expect { enabled.execute(CEL::Context.new(true)) }.to raise_error(CEL::ExecutionError)
56
+ expect { CEL.compile("sets.contains([1], [1])").execute(context) }.to raise_error(CEL::ExecutionError)
57
+ expect { CEL.compile("sets.contains([1], [1])", extensions: [:sets]).execute(CEL::Context.new(true)) }
58
+ .to raise_error(CEL::ExecutionError)
51
59
 
52
60
  context.define_function("lowerAscii") { |value| value }
53
61
  expect { enabled.execute(context) }.to raise_error(CEL::ExecutionError, /lowerAscii/)
@@ -166,6 +174,147 @@ RSpec.describe CEL do
166
174
  expect(CEL.compile("adjust(3)").execute(context)).to eq(10)
167
175
  end
168
176
 
177
+ it "releases replaced callbacks and their captures while keeping the current callback alive" do
178
+ output, status = Open3.capture2e(
179
+ RbConfig.ruby, "-I", File.expand_path("../lib", __dir__), "-rcel", "-rweakref", "-e", <<~RUBY
180
+ context, callbacks, captures = Thread.new do
181
+ ctx = CEL::Context.new
182
+ callbacks = []
183
+ captures = []
184
+ 20.times do |i|
185
+ capture = +"capture:\#{i}"
186
+ callback = proc { capture }
187
+ ctx.add_function("current", callback)
188
+ callbacks << WeakRef.new(callback)
189
+ captures << WeakRef.new(capture)
190
+ end
191
+ [ctx, callbacks, captures]
192
+ end.value
193
+
194
+ GC.start
195
+ GC.verify_compaction_references(expand_heap: true, toward: :empty)
196
+ alive = [callbacks, captures].map { |refs| refs.count(&:weakref_alive?) }
197
+ raise "expected 1 callback and capture, got \#{alive.inspect}" unless alive == [1, 1]
198
+ raise "current callback was collected" unless callbacks.last.weakref_alive? && captures.last.weakref_alive?
199
+ result = CEL.compile("current()").execute(context)
200
+ raise "incorrect replacement result: \#{result.inspect}" unless result == "capture:19"
201
+ RUBY
202
+ )
203
+
204
+ expect(status.success?).to eq(true), output
205
+ end
206
+
207
+ it "allows garbage collection while registering execution roots" do
208
+ Open3.popen2e(
209
+ RbConfig.ruby, "-I", File.expand_path("../lib", __dir__), "-rcel", "-e", <<~RUBY
210
+ context = CEL::Context.new
211
+ context.define_function("answer") { 42 }
212
+ program = CEL.compile("answer()")
213
+ GC.stress = true
214
+ 3.times do
215
+ raise "incorrect callback result" unless program.execute(context) == 42
216
+ end
217
+ GC.stress = false
218
+ RUBY
219
+ ) do |stdin, output, process|
220
+ stdin.close
221
+ begin
222
+ result = Timeout.timeout(30) { output.read }
223
+ expect(process.value.success?).to eq(true), result
224
+ rescue Timeout::Error
225
+ Process.kill("KILL", process.pid)
226
+ raise
227
+ end
228
+ end
229
+ end
230
+
231
+ it "releases execution roots after a thread is interrupted or killed" do
232
+ output, status = Open3.capture2e(
233
+ RbConfig.ruby, "-I", File.expand_path("../lib", __dir__), "-rcel", "-rweakref", "-e", <<~RUBY
234
+ [:raise, :kill].each do |interrupt|
235
+ ready = Queue.new
236
+ runner = Thread.new do
237
+ context = CEL::Context.new
238
+ capture = +"cancelled execution"
239
+ callback = proc { capture }
240
+ context.define_function("unused", &callback)
241
+ context.add_variable("items", Array.new(2_000, 1))
242
+ program = CEL.compile("items.map(x, items.map(y, x + y).size()).size()")
243
+ ready << [WeakRef.new(context), WeakRef.new(callback), WeakRef.new(capture)]
244
+ program.execute(context)
245
+ :completed
246
+ rescue Interrupt
247
+ :interrupted
248
+ end
249
+ references = ready.pop
250
+ until runner.status == "sleep"
251
+ raise "execution finished before interruption" unless runner.alive?
252
+ Thread.pass
253
+ end
254
+ if interrupt == :raise
255
+ runner.raise(Interrupt)
256
+ raise "interrupt was not propagated" unless runner.value == :interrupted
257
+ else
258
+ runner.kill
259
+ raise "thread was not killed" unless runner.value.nil?
260
+ end
261
+
262
+ GC.start
263
+ GC.verify_compaction_references(expand_heap: true, toward: :empty)
264
+ raise "cancelled execution retained callback state" if references.any?(&:weakref_alive?)
265
+ end
266
+ RUBY
267
+ )
268
+
269
+ expect(status.success?).to eq(true), output
270
+ end
271
+
272
+ it "pins replaced callbacks until in-flight executions finish, including on errors" do
273
+ output, status = Open3.capture2e(
274
+ RbConfig.ruby, "-I", File.expand_path("../lib", __dir__), "-rcel", "-rweakref", "-e", <<~RUBY
275
+ [false, true].each do |fail_execution|
276
+ ready = Queue.new
277
+ resume = Queue.new
278
+ context, callback, capture = Thread.new do
279
+ ctx = CEL::Context.new
280
+ value = +"original"
281
+ original = proc { value + ":called" }
282
+ ctx.define_function("current", &original)
283
+ [ctx, WeakRef.new(original), WeakRef.new(value)]
284
+ end.value
285
+ context.define_function("pause") { ready << true; resume.pop; 0 }
286
+
287
+ source = fail_execution ? "[pause(), current(), 1 / 0]" : "[pause(), current()]"
288
+ runner = Thread.new do
289
+ CEL.compile(source).execute(context)
290
+ rescue => error
291
+ raise unless fail_execution && error.message.include?("Division by zero")
292
+
293
+ :execution_error
294
+ end
295
+ ready.pop
296
+ Thread.new { context.define_function("current") { "replacement" } }.join
297
+
298
+ GC.start
299
+ GC.verify_compaction_references(expand_heap: true, toward: :empty)
300
+ raise "in-flight callback was collected" unless callback.weakref_alive? && capture.weakref_alive?
301
+ resume << true
302
+ result = runner.value
303
+ expected = fail_execution ? :execution_error : [0, "original:called"]
304
+ raise "incorrect snapshot result: \#{result.inspect}" unless result == expected
305
+
306
+ GC.start
307
+ GC.verify_compaction_references(expand_heap: true, toward: :empty)
308
+ raise "finished execution retained old callback" if callback.weakref_alive? || capture.weakref_alive?
309
+ result = CEL.compile("current()").execute(context)
310
+ raise "incorrect current callback result: \#{result.inspect}" unless result == "replacement"
311
+ end
312
+ RUBY
313
+ )
314
+
315
+ expect(status.success?).to eq(true), output
316
+ end
317
+
169
318
  it "raises clear type error for unsupported variable types" do
170
319
  context = CEL::Context.new
171
320
  expect { context.add_variable("bad", Object.new) }.to raise_error(CEL::TypeError)
@@ -180,6 +329,183 @@ RSpec.describe CEL do
180
329
  expect(refs["functions"]).to include("size")
181
330
  end
182
331
 
332
+ it "reports external references without comprehension bindings" do
333
+ program = CEL.compile("items.map(x, x + offset)")
334
+ expect(program.references["variables"]).to contain_exactly("items", "offset")
335
+ refs = CEL.compile("[x].map(x, .f(x + .offset))").references
336
+ expect(refs["variables"]).to contain_exactly("x", "offset")
337
+ expect(refs["functions"]).to include("f")
338
+ expect(refs["functions"]).not_to include(".f")
339
+ expect(CEL.compile("input.?field.optMap(x, x + 1)").references["variables"])
340
+ .to contain_exactly("input")
341
+ end
342
+
343
+ it "returns false for unordered numeric comparisons and unusable map membership keys" do
344
+ context = CEL::Context.build(nan: Float::NAN)
345
+ %w[< <= > >=].each do |operator|
346
+ ["nan #{operator} 1", "1u #{operator} nan", "nan #{operator} nan"].each do |expression|
347
+ expect(CEL.compile(expression).execute(context)).to eq(false)
348
+ end
349
+ end
350
+ ["[] in {}", "null in {'a': 1}", "1.5 in {1: 'a'}"].each do |expression|
351
+ expect(CEL.compile(expression).execute).to eq(false)
352
+ end
353
+ expect(CEL.compile("1.0 in {1: 'a'}").execute).to eq(true)
354
+ expect { CEL.compile("nan < 'a'").execute(context) }.to raise_error(CEL::ExecutionError)
355
+ end
356
+
357
+ it "only suppresses missing entries in optional access and rejects list plus map" do
358
+ ["{}[?'a'].hasValue()", "[1][?2].hasValue()", "{}.?a.hasValue()"].each do |expression|
359
+ expect(CEL.compile(expression).execute).to eq(false)
360
+ end
361
+ ["{}[?[]]", "[1][?'a']", "1.?a", "[1] + {'a': 2}"].each do |expression|
362
+ expect { CEL.compile(expression).execute }.to raise_error(CEL::ExecutionError)
363
+ end
364
+ expect(CEL.compile("[1] + [2]").execute).to eq([1, 2])
365
+ end
366
+
367
+ it "exposes exact inclusive duration limits in nanoseconds" do
368
+ boundaries = [
369
+ [CEL::Duration::MAX_NANOSECONDS, "315576000000.999999999s"],
370
+ [CEL::Duration::MIN_NANOSECONDS, "-315576000000.999999999s"]
371
+ ]
372
+
373
+ boundaries.each do |nanoseconds, formatted|
374
+ expect(nanoseconds).to be_an(Integer)
375
+ expect(CEL.compile("string(duration('#{nanoseconds}ns'))").execute).to eq(formatted)
376
+ outside = nanoseconds + (nanoseconds.negative? ? -1 : 1)
377
+ expect { CEL.compile("duration('#{outside}ns')").execute }.to raise_error(CEL::ExecutionError)
378
+ end
379
+ end
380
+
381
+ it "preserves the widened duration range and exact decimal parsing" do
382
+ [-315_576_000_000, -10_000_000_000, 10_000_000_000, 315_576_000_000].each do |seconds|
383
+ duration = CEL.compile("duration('#{seconds}s')").execute
384
+ expect(duration.total_seconds).to eq(seconds.to_f)
385
+ expect(duration.to_s).to eq("#{seconds}s")
386
+ expect(duration).to eq(CEL::Duration.new(seconds))
387
+ context = CEL::Context.build(delay: duration)
388
+ expect(CEL.compile("delay == duration('#{seconds}s')").execute(context)).to eq(true)
389
+ end
390
+ expect(CEL.compile("string(duration('4.02us'))").execute).to eq("0.00000402s")
391
+ expect(CEL.compile("string(duration('315576000000.999999999s'))").execute)
392
+ .to eq("315576000000.999999999s")
393
+ expect { CEL.compile("duration('315576000001s')").execute }.to raise_error(CEL::ExecutionError)
394
+ [315_576_000_001, -315_576_000_001, Float::NAN].each do |seconds|
395
+ expect { CEL::Duration.new(seconds) }.to raise_error(CEL::TypeError)
396
+ end
397
+ end
398
+
399
+ it "raises documented CEL error classes on secondary Ruby threads" do
400
+ errors = Thread.new do
401
+ [
402
+ -> { CEL.compile("1 +") },
403
+ -> { CEL.compile("1 / 0").execute },
404
+ -> { CEL.compile("optional.none()").execute }
405
+ ].map do |operation|
406
+ operation.call
407
+ rescue => e
408
+ e.class
409
+ end
410
+ end.value
411
+ expect(errors).to eq([CEL::ParseError, CEL::ExecutionError, CEL::TypeError])
412
+ end
413
+
414
+ it "bounds total nested iterations with a fresh budget for every execution" do
415
+ expression = "[1, 2].map(x, [3, 4].filter(y, y > x))"
416
+ program = CEL.compile(expression, max_iterations: 6)
417
+ [nil, CEL::Context.new, CEL::Context.new(true)].each do |context|
418
+ 2.times { expect(program.execute(context)).to eq([[3, 4], [3, 4]]) }
419
+ expect { CEL.compile(expression, max_iterations: 5).execute(context) }
420
+ .to raise_error(CEL::ExecutionError, /iteration.*5/i)
421
+ end
422
+ expect(CEL.compile(expression).execute).to eq([[3, 4], [3, 4]])
423
+ end
424
+
425
+ it "does not let logical or optional expressions absorb budget failures" do
426
+ [
427
+ "[1, 2].all(x, x > 0) || true",
428
+ "[1, 2].all(x, x > 0) && false",
429
+ "[[1, 2].all(x, x > 0)][?0].hasValue()"
430
+ ].each do |expression|
431
+ expect { CEL.compile(expression, max_iterations: 1).execute }
432
+ .to raise_error(CEL::ExecutionError, /iteration/i)
433
+ end
434
+ end
435
+
436
+ it "rejects invalid iteration budgets and timeouts" do
437
+ [-1, 1.5, nil, "3"].each do |budget|
438
+ expect { CEL.compile("1", max_iterations: budget) }.to raise_error(ArgumentError)
439
+ end
440
+ expect { CEL.compile("1", max_iterations: 2**64) }.to raise_error(RangeError)
441
+ program = CEL.compile("1")
442
+ [-1, Float::INFINITY, Float::NAN, 1e100].each do |timeout|
443
+ expect { program.execute(timeout: timeout) }.to raise_error(ArgumentError, /timeout/)
444
+ end
445
+ expect { program.execute(interrupt: Object.new) }.to raise_error(TypeError)
446
+ end
447
+
448
+ it "applies cooperative timeouts per call without poisoning later executions" do
449
+ program = CEL.compile("['ABC'].map(x, x.lowerAscii())", extensions: [:strings])
450
+ context = CEL::Context.new
451
+ expect { program.execute(context, timeout: 0) }.to raise_error(CEL::ExecutionError, /interrupt/i)
452
+ expect(program.execute(context, timeout: 1)).to eq(["abc"])
453
+ expect(program.call(timeout: 1)).to eq(["abc"])
454
+ expect(program.execute_with_context(context, timeout: 1)).to eq(["abc"])
455
+ expect(program.execute(context)).to eq(["abc"])
456
+ expect { program.execute(timeout: 0, interrupt: CEL::CancellationToken.new) }
457
+ .to raise_error(CEL::ExecutionError, /interrupt/i)
458
+ expect { program.execute(CEL::Context.new(true), timeout: 0) }
459
+ .to raise_error(CEL::ExecutionError, /interrupt/i)
460
+ expect(CEL.compile("1 + 1").execute(timeout: 0)).to eq(2)
461
+ end
462
+
463
+ it "observes an elapsed deadline after a Ruby callback returns" do
464
+ returned = false
465
+ context = CEL::Context.build do |ctx|
466
+ ctx.define_function("delay") do
467
+ sleep(0.02)
468
+ returned = true
469
+ end
470
+ end
471
+ program = CEL.compile("delay() && [1, 2].all(x, x > 0)")
472
+ expect { program.execute(context, timeout: 0.001) }
473
+ .to raise_error(CEL::ExecutionError, /interrupt/i)
474
+ expect(returned).to eq(true)
475
+ end
476
+
477
+ it "cancels an evaluation from another Ruby thread and keeps other runs independent" do
478
+ token = CEL::CancellationToken.new
479
+ expect(token.cancelled?).to eq(false)
480
+ started = Queue.new
481
+ resume = Queue.new
482
+ context = CEL::Context.build(items: [1, 2]) do |ctx|
483
+ ctx.define_function("started") do
484
+ started << true
485
+ resume.pop
486
+ true
487
+ end
488
+ end
489
+ program = CEL.compile("started() && items.all(x, items.exists(y, y > x)) || true")
490
+ runner = Thread.new do
491
+ program.execute(context, interrupt: token)
492
+ rescue CEL::ExecutionError => e
493
+ e
494
+ end
495
+ started.pop
496
+ token.cancel!
497
+ resume << true
498
+ expect(token.cancelled?).to eq(true)
499
+ expect(runner.value).to be_a(CEL::ExecutionError).and(have_attributes(message: /interrupt/i))
500
+
501
+ control = CEL::Context.build(items: [1, 2]) do |ctx|
502
+ ctx.define_function("started") { true }
503
+ end
504
+ expect(program.execute(control)).to eq(true)
505
+ expect(program.execute(control, interrupt: CEL::CancellationToken.new)).to eq(true)
506
+ expect { program.execute(control, interrupt: token) }.to raise_error(CEL::ExecutionError, /interrupt/i)
507
+ end
508
+
183
509
  it "registers max and min by default" do
184
510
  tests = {
185
511
  "max(2, 9, -3)" => 9,
@@ -278,7 +604,7 @@ RSpec.describe CEL do
278
604
  end
279
605
 
280
606
  it "executes the same program concurrently with independent contexts" do
281
- program = CEL.compile("items.map(x, x + offset).filter(x, x > cutoff).size() + id")
607
+ program = CEL.compile("items.map(x, x + offset).filter(x, x > cutoff).size() + id", max_iterations: 400)
282
608
  ready = Queue.new
283
609
  start = Queue.new
284
610
 
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe "Source gem packaging" do
6
+ it "includes the checked-in Cargo lockfile" do
7
+ spec = Dir.chdir(File.expand_path("..", __dir__)) do
8
+ Gem::Specification.load("cel-rs-rb.gemspec")
9
+ end
10
+
11
+ expect(spec.files).to include("Cargo.lock")
12
+ end
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cel-rs-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - CEL Ruby Contributors
@@ -101,6 +101,7 @@ extensions:
101
101
  extra_rdoc_files: []
102
102
  files:
103
103
  - CHANGELOG.md
104
+ - Cargo.lock
104
105
  - Cargo.toml
105
106
  - LICENSE
106
107
  - README.md
@@ -110,6 +111,7 @@ files:
110
111
  - lib/cel.rb
111
112
  - lib/cel/version.rb
112
113
  - spec/cel_spec.rb
114
+ - spec/packaging_spec.rb
113
115
  - spec/spec_helper.rb
114
116
  homepage: https://github.com/buildkite/cel-rs-rb
115
117
  licenses: