|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2025, by Samuel Williams. |
| 5 | + |
| 6 | +require "kernel/barrier" |
| 7 | + |
| 8 | +describe Kernel do |
| 9 | + with "#Barrier" do |
| 10 | + it "should create a barrier, yield it, wait, and stop automatically" do |
| 11 | + finished = 0 |
| 12 | + results = [] |
| 13 | + |
| 14 | + Barrier do |barrier| |
| 15 | + 3.times do |i| |
| 16 | + barrier.async do |task| |
| 17 | + sleep(0.01 * i) # Different completion times |
| 18 | + results << "task_#{i}" |
| 19 | + finished += 1 |
| 20 | + end |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + expect(finished).to be == 3 |
| 25 | + expect(results.size).to be == 3 |
| 26 | + end |
| 27 | + |
| 28 | + it "should handle exceptions and still clean up properly" do |
| 29 | + exception_raised = false |
| 30 | + |
| 31 | + expect do |
| 32 | + Barrier do |barrier| |
| 33 | + barrier.async do |task| |
| 34 | + raise "Test exception" |
| 35 | + end |
| 36 | + |
| 37 | + barrier.async do |task| |
| 38 | + sleep(0.1) # This should be stopped |
| 39 | + end |
| 40 | + end |
| 41 | + end.to raise_exception(RuntimeError, message: be =~ /Test exception/) |
| 42 | + |
| 43 | + # The barrier should have been cleaned up despite the exception. |
| 44 | + end |
| 45 | + |
| 46 | + it "should support parent parameter" do |
| 47 | + parent_task = nil |
| 48 | + child_task = nil |
| 49 | + |
| 50 | + Sync do |task| |
| 51 | + parent_task = task |
| 52 | + |
| 53 | + Barrier(parent: task) do |barrier| |
| 54 | + barrier.async do |async_task| |
| 55 | + child_task = async_task |
| 56 | + # While the child task is running, parent should be set: |
| 57 | + expect(child_task.parent).to be == parent_task |
| 58 | + sleep(0.01) |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + expect(child_task).not.to be_nil |
| 64 | + end |
| 65 | + |
| 66 | + it "should wait for all tasks to complete before returning" do |
| 67 | + completion_order = [] |
| 68 | + |
| 69 | + Barrier do |barrier| |
| 70 | + 3.times do |i| |
| 71 | + barrier.async do |task| |
| 72 | + sleep(0.01 * (3 - i)) # Reverse order completion |
| 73 | + completion_order << i |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + # All tasks should have completed |
| 79 | + expect(completion_order.size).to be == 3 |
| 80 | + expect(completion_order.sort).to be == [0, 1, 2] |
| 81 | + end |
| 82 | + |
| 83 | + it "should stop remaining tasks when block exits early" do |
| 84 | + tasks = [] |
| 85 | + |
| 86 | + Sync do |parent| |
| 87 | + begin |
| 88 | + Barrier do |barrier| |
| 89 | + 3.times do |i| |
| 90 | + tasks << barrier.async do |task| |
| 91 | + sleep(1) # Long running task |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + # Simulate early exit due to some condition |
| 96 | + raise "Early exit" |
| 97 | + end |
| 98 | + rescue => e |
| 99 | + # Expected exception |
| 100 | + end |
| 101 | + |
| 102 | + # Wait for tasks to finish/stopped deterministically |
| 103 | + tasks.each do |t| |
| 104 | + begin |
| 105 | + t.wait |
| 106 | + rescue => e |
| 107 | + # ignore errors from waiting on stopped tasks |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + # All three tasks should have been stopped |
| 113 | + expect(tasks.map(&:stopped?).all?).to be == true |
| 114 | + end |
| 115 | + |
| 116 | + it "should handle empty barriers gracefully" do |
| 117 | + result = nil |
| 118 | + |
| 119 | + expect do |
| 120 | + result = Async::Barrier() do |barrier| |
| 121 | + # No tasks added |
| 122 | + end |
| 123 | + end.not.to raise_exception |
| 124 | + |
| 125 | + # Should complete successfully |
| 126 | + end |
| 127 | + |
| 128 | + it "should create a barrier, yield it, wait, and stop automatically" do |
| 129 | + finished = 0 |
| 130 | + results = [] |
| 131 | + |
| 132 | + Barrier do |barrier| |
| 133 | + 3.times do |i| |
| 134 | + barrier.async do |task| |
| 135 | + sleep(0.01 * i) |
| 136 | + results << "task_#{i}" |
| 137 | + finished += 1 |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + expect(finished).to be == 3 |
| 143 | + expect(results.size).to be == 3 |
| 144 | + end |
| 145 | + |
| 146 | + it "should handle exceptions and still clean up properly" do |
| 147 | + expect do |
| 148 | + Barrier do |barrier| |
| 149 | + barrier.async do |task| |
| 150 | + raise "Kernel helper exception" |
| 151 | + end |
| 152 | + barrier.async do |task| |
| 153 | + sleep(0.1) |
| 154 | + end |
| 155 | + end |
| 156 | + end.to raise_exception(RuntimeError, message: be =~ /Kernel helper exception/) |
| 157 | + end |
| 158 | + |
| 159 | + it "should support parent parameter" do |
| 160 | + parent_task = nil |
| 161 | + child_task = nil |
| 162 | + |
| 163 | + Sync do |task| |
| 164 | + parent_task = task |
| 165 | + |
| 166 | + Barrier(parent: task) do |barrier| |
| 167 | + barrier.async do |async_task| |
| 168 | + child_task = async_task |
| 169 | + # While the child task is running, parent should be set: |
| 170 | + expect(child_task.parent).to be == parent_task |
| 171 | + sleep(0.01) |
| 172 | + end |
| 173 | + end |
| 174 | + end |
| 175 | + |
| 176 | + expect(child_task).not.to be_nil |
| 177 | + end |
| 178 | + end |
| 179 | +end |
0 commit comments