attempt 0.6.3 → 0.8.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.
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative 'lib/attempt'
4
+
5
+ puts "=== Testing Improved Attempt Library ==="
6
+
7
+ # Test 1: Basic functionality
8
+ puts "\n1. Basic retry functionality:"
9
+ begin
10
+ counter = 0
11
+ result = attempt(tries: 3, interval: 0.1) do
12
+ counter += 1
13
+ puts " Attempt #{counter}"
14
+ raise "Simulated error" if counter < 3
15
+ "Success!"
16
+ end
17
+ puts " Result: #{result}"
18
+ rescue => e
19
+ puts " Final error: #{e.message}"
20
+ end
21
+
22
+ # Test 2: Parameter validation (our improvement)
23
+ puts "\n2. Parameter validation:"
24
+ begin
25
+ Attempt.new(tries: -1)
26
+ rescue ArgumentError => e
27
+ puts " ✓ Caught invalid tries: #{e.message}"
28
+ end
29
+
30
+ begin
31
+ Attempt.new(interval: -5)
32
+ rescue ArgumentError => e
33
+ puts " ✓ Caught invalid interval: #{e.message}"
34
+ end
35
+
36
+ # Test 3: Configuration inspection (our improvement)
37
+ puts "\n3. Configuration inspection:"
38
+ attempt_obj = Attempt.new(tries: 5, interval: 2, increment: 1)
39
+ puts " Configuration: #{attempt_obj.configuration}"
40
+ puts " Timeout enabled: #{attempt_obj.timeout_enabled?}"
41
+
42
+ # Test 4: Better error messaging (our improvement)
43
+ puts "\n4. Improved error logging:"
44
+ begin
45
+ attempt(tries: 2, interval: 0.1, warnings: false) do
46
+ raise StandardError, "This is a test error"
47
+ end
48
+ rescue => e
49
+ puts " Final error class: #{e.class}"
50
+ puts " Final error message: #{e.message}"
51
+ end
52
+
53
+ # Test 5: Timeout with numeric value (our improvement)
54
+ puts "\n5. Numeric timeout:"
55
+ begin
56
+ result = attempt(tries: 1, timeout: 0.1) do
57
+ sleep 0.05 # This should succeed
58
+ "Completed within timeout"
59
+ end
60
+ puts " ✓ #{result}"
61
+ rescue Timeout::Error
62
+ puts " ✗ Timed out unexpectedly"
63
+ end
64
+
65
+ puts "\n=== All tests completed ==="
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative 'lib/attempt'
4
+ require 'benchmark'
5
+
6
+ puts "=== Testing Different Timeout Strategies ==="
7
+
8
+ def blocking_operation(duration)
9
+ start_time = Time.now
10
+ while Time.now - start_time < duration
11
+ # Simulate CPU-intensive work
12
+ 1000.times { Math.sqrt(rand) }
13
+ end
14
+ "Completed after #{duration}s"
15
+ end
16
+
17
+ def io_operation(duration)
18
+ sleep duration
19
+ "IO completed after #{duration}s"
20
+ end
21
+
22
+ strategies = [:auto, :custom, :thread, :process, :fiber, :ruby_timeout]
23
+
24
+ strategies.each do |strategy|
25
+ puts "\n--- Testing #{strategy.to_s.upcase} strategy ---"
26
+
27
+ # Test 1: Operation that completes within timeout
28
+ begin
29
+ result = attempt(tries: 1, timeout: 2, timeout_strategy: strategy) do
30
+ io_operation(0.1)
31
+ end
32
+ puts "✓ Fast operation: #{result}"
33
+ rescue => e
34
+ puts "✗ Fast operation failed: #{e.message}"
35
+ end
36
+
37
+ # Test 2: Operation that times out
38
+ begin
39
+ time = Benchmark.realtime do
40
+ attempt(tries: 1, timeout: 0.5, timeout_strategy: strategy) do
41
+ io_operation(2) # This should timeout
42
+ end
43
+ end
44
+ puts "✗ Timeout test failed - should have timed out"
45
+ rescue Timeout::Error => e
46
+ puts "✓ Timeout worked: #{e.message}"
47
+ rescue => e
48
+ puts "✗ Unexpected error: #{e.class}: #{e.message}"
49
+ end
50
+ end
51
+
52
+ # Test configuration inspection
53
+ puts "\n--- Configuration Test ---"
54
+ attempt_obj = Attempt.new(tries: 3, timeout: 5, timeout_strategy: :process)
55
+ puts "Configuration: #{attempt_obj.configuration}"
56
+
57
+ puts "\n=== All timeout strategy tests completed ==="