|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +RSpec.describe SOF::CycleRegistry do |
| 6 | + describe "thread safety" do |
| 7 | + let(:registry) { described_class.instance } |
| 8 | + let(:original_classes) { [] } |
| 9 | + |
| 10 | + before do |
| 11 | + # Save the original classes |
| 12 | + @original_classes = registry.cycle_classes.to_a |
| 13 | + # Clear the registry before each test |
| 14 | + registry.cycle_classes.clear |
| 15 | + end |
| 16 | + |
| 17 | + after do |
| 18 | + # Restore the original classes |
| 19 | + registry.cycle_classes.clear |
| 20 | + @original_classes.each { |klass| registry.register(klass) } |
| 21 | + end |
| 22 | + |
| 23 | + it "safely registers multiple cycle classes concurrently" do |
| 24 | + # Create some test cycle classes |
| 25 | + test_classes = 10.times.map do |i| |
| 26 | + Class.new do |
| 27 | + define_singleton_method(:handles?) { |kind| kind == "test#{i}" } |
| 28 | + define_singleton_method(:name) { "TestCycle#{i}" } |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + threads = test_classes.map do |klass| |
| 33 | + Thread.new { registry.register(klass) } |
| 34 | + end |
| 35 | + |
| 36 | + threads.each(&:join) |
| 37 | + |
| 38 | + expect(registry.cycle_classes.size).to eq(10) |
| 39 | + test_classes.each do |klass| |
| 40 | + expect(registry.cycle_classes).to include(klass) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + it "safely handles concurrent reads while registering" do |
| 45 | + # Pre-register some classes |
| 46 | + 5.times do |i| |
| 47 | + klass = Class.new do |
| 48 | + define_singleton_method(:handles?) { |kind| kind == "existing#{i}" } |
| 49 | + end |
| 50 | + registry.register(klass) |
| 51 | + end |
| 52 | + |
| 53 | + read_errors = [] |
| 54 | + write_errors = [] |
| 55 | + |
| 56 | + # Create threads that read |
| 57 | + read_threads = 20.times.map do |
| 58 | + Thread.new do |
| 59 | + 100.times do |
| 60 | + registry.cycle_classes.to_a |
| 61 | + registry.cycle_classes.size |
| 62 | + rescue => e |
| 63 | + read_errors << e |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + # Create threads that write |
| 69 | + write_threads = 5.times.map do |i| |
| 70 | + Thread.new do |
| 71 | + klass = Class.new do |
| 72 | + define_singleton_method(:handles?) { |kind| kind == "new#{i}" } |
| 73 | + end |
| 74 | + begin |
| 75 | + registry.register(klass) |
| 76 | + rescue => e |
| 77 | + write_errors << e |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + (read_threads + write_threads).each(&:join) |
| 83 | + |
| 84 | + expect(read_errors).to be_empty |
| 85 | + expect(write_errors).to be_empty |
| 86 | + expect(registry.cycle_classes.size).to eq(10) # 5 existing + 5 new |
| 87 | + end |
| 88 | + |
| 89 | + it "safely finds handlers concurrently" do |
| 90 | + # Register test classes |
| 91 | + 10.times do |i| |
| 92 | + klass = Class.new do |
| 93 | + define_singleton_method(:handles?) { |kind| kind == "kind#{i}" } |
| 94 | + define_singleton_method(:name) { "Handler#{i}" } |
| 95 | + end |
| 96 | + registry.register(klass) |
| 97 | + end |
| 98 | + |
| 99 | + errors = [] |
| 100 | + results = Concurrent::Array.new |
| 101 | + |
| 102 | + threads = 100.times.map do |i| |
| 103 | + Thread.new do |
| 104 | + kind_index = i % 10 |
| 105 | + begin |
| 106 | + handler = registry.handling("kind#{kind_index}") |
| 107 | + results << handler |
| 108 | + rescue => e |
| 109 | + errors << e |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + threads.each(&:join) |
| 115 | + |
| 116 | + expect(errors).to be_empty |
| 117 | + expect(results.size).to eq(100) |
| 118 | + |
| 119 | + # Verify all lookups found the correct handler |
| 120 | + results.each_with_index do |handler, i| |
| 121 | + expected_kind = "kind#{i % 10}" |
| 122 | + expect(handler.handles?(expected_kind)).to be true |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + it "raises error for unknown kind even under concurrent access" do |
| 127 | + # Register a known handler |
| 128 | + known_class = Class.new do |
| 129 | + define_singleton_method(:handles?) { |kind| kind == "known" } |
| 130 | + end |
| 131 | + registry.register(known_class) |
| 132 | + |
| 133 | + errors = Concurrent::Array.new |
| 134 | + |
| 135 | + threads = 10.times.map do |
| 136 | + Thread.new do |
| 137 | + registry.handling("unknown") |
| 138 | + rescue SOF::Cycle::InvalidKind => e |
| 139 | + errors << e |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + threads.each(&:join) |
| 144 | + |
| 145 | + expect(errors.size).to eq(10) |
| 146 | + errors.each do |error| |
| 147 | + expect(error.message).to include("':unknown' is not a valid kind of Cycle") |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + it "maintains singleton behavior across threads" do |
| 152 | + instances = Concurrent::Array.new |
| 153 | + |
| 154 | + threads = 20.times.map do |
| 155 | + Thread.new do |
| 156 | + instances << described_class.instance |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + threads.each(&:join) |
| 161 | + |
| 162 | + expect(instances.uniq.size).to eq(1) |
| 163 | + expect(instances.first).to be(described_class.instance) |
| 164 | + end |
| 165 | + end |
| 166 | +end |
0 commit comments