1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
require 'concurrent'
require 'concurrent/edge'
require 'securerandom'
RSpec.describe Concurrent::Edge::LockFreeLinkedSet, edge: true do
subject { described_class.new }
describe '.new' do
context 'when passed default val' do
it 'uses the val arg as data for each node' do
set = described_class.new 3, true
expect(set.all? { |val| val == true }).to be_truthy
end
end
end
describe '#add' do
it 'appends to the linked set' do
expect(subject.add 'test string1').to be true
end
context 'in a multi-threaded environment', notravis: true do
it 'adds the items to the set' do
to_insert = %w(one two three four five six)
threads = ::Array.new(16) do
in_thread do
to_insert.each do |item|
subject.add item
end
end
end
threads.each(&:join)
to_insert.each do |item|
expect(subject.contains? item).to be true
end
end
end
end
describe '#<<' do
it 'appends to the linked set and returns self' do
expect(subject << 'test string1').to be_a described_class
end
it 'returns self regardless of whether it was logically added' do
subject << 'test string'
expect(subject << 'test string').to be_a described_class
end
end
describe '#contains?' do
context 'when checking if set includes a value' do
it 'returns true if a value exists' do
subject << 'Concurrency... ooh! ahh!'
expect(subject.contains? 'Concurrency... ooh! ahh!').to eq true
end
it 'compares object using Object#hash' do
val = 'Hash me.'
subject << val
expect(subject.contains? 'Hash me.').to eq true
end
it 'returns false for values not in the set' do
subject << 'Concurrency... ooh! ahh!'
expect(subject.contains? 'Sequential... booh! nah!').to eq false
end
context 'when set is empty' do
it 'does not break' do
expect(subject.contains? 'Nothing to see here.').to eq false
end
end
context 'when set is long' do
it 'does not break' do
arr = ::Array.new(1000) { SecureRandom.hex }
arr.each { |n| subject << n }
ret = arr.all? { |n| subject.contains? n }
expect(ret).to be true
end
end
context 'in a multi-threaded environment', notravis: true do
it 'correctly check that the set contains the item' do
to_insert = %w(one two three four five six)
to_insert.each { |item| subject << item }
threads = ::Array.new(16) do
in_thread do
100.times { subject << SecureRandom.hex }
to_insert.each do |item|
expect(subject.contains? item).to be true
end
end
end
threads.each(&:join)
to_insert.each do |item|
expect(subject.contains? item).to be true
end
end
end
end
end
describe '#remove' do
context 'when item is inside of set' do
before { subject << 'one' << 'two' << 'three' }
it 'the item is no longer visible to the user' do
subject.remove 'three'
expect(subject.contains? 'three').to be false
end
it 'allows for the item to be added despite being physically present' do
subject.remove 'three'
expect(subject.add 'three').to be true
end
end
context 'in a multi-threaded environment', notravis: true do
it 'adds the items to the set' do
to_insert = %w(one two three four five six)
to_insert.each { |item| subject << item }
threads = ::Array.new(8) do
[in_thread { subject.remove 'one' },
in_thread { subject.remove 'two' },
in_thread { subject.remove 'three' }]
end
threads.flatten.each(&:join)
expect(subject.contains? 'one').to be false
expect(subject.contains? 'two').to be false
expect(subject.contains? 'three').to be false
expect(subject.contains? 'four').to be true
expect(subject.contains? 'five').to be true
expect(subject.contains? 'six').to be true
end
it 'does not recognize the existence of the item when removed' do
to_insert = %w(one two three four five six)
to_insert.each { |item| subject << item }
::Array.new(16) do
in_thread do
100.times { subject << SecureRandom.hex }
to_insert.each do |item|
subject.remove item
expect(subject.contains? item).to be false
end
end
end
end
end
context 'when item is not inside of set' do
before { subject << 'one' << 'two' << 'three' }
it 'does not remove to value' do
expect(subject.remove 'four').to be false
end
it 'the set remains intact' do
expect(subject).to receive :remove
subject.remove 'four'
present = %w(one two three).map { |n| subject.contains? n }.all?
expect(present).to be true
end
context 'when the set is empty' do
subject { described_class.new }
it 'remove does not break' do
expect(subject.remove 'test').to be false
end
end
context 'when the set is large' do
subject { described_class.new(1000) { SecureRandom.hex } }
it 'remove successfully removes the node' do
subject << 'Testing'
expect(subject.remove 'Testing').to be true
end
end
end
end
end
|