Skip to content

Commit 0b42337

Browse files
Use set
1 parent 6af5fb2 commit 0b42337

4 files changed

Lines changed: 32 additions & 19 deletions

File tree

gems/smithy-client/lib/smithy-client/features.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ module Client
66
module Features
77
class << self
88
def track(*features, &block)
9-
Thread.current[:smithy_ruby_features] ||= []
10-
Thread.current[:smithy_ruby_features].concat(features)
9+
Thread.current[:smithy_ruby_features] ||= Set.new
10+
added = features.map { |f| Thread.current[:smithy_ruby_features].add?(f) }
1111
block.call
1212
ensure
13-
Thread.current[:smithy_ruby_features].pop(features.size)
13+
features.each_with_index { |f, i| Thread.current[:smithy_ruby_features].delete(f) if added[i] }
1414
end
1515

1616
def tracked
17-
Thread.current[:smithy_ruby_features] || []
17+
Thread.current[:smithy_ruby_features] || Set.new
1818
end
1919
end
2020
end

gems/smithy-client/spec/smithy-client/features_spec.rb

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,35 @@ module Smithy
66
module Client
77
describe Features do
88
it 'tracks and removes a feature' do
9-
Features.track('A') { expect(Features.tracked).to eq(%w[A]) }
9+
Features.track('A') { expect(Features.tracked).to eq(%w[A].to_set) }
1010
expect(Features.tracked).to be_empty
1111
end
1212

1313
it 'tracks and removes multiple features' do
14-
features = %w[A B C]
14+
features = %w[A B C].to_set
1515
Features.track(*features) { expect(Features.tracked).to eq(features) }
1616
expect(Features.tracked).to be_empty
1717
end
1818

1919
it 'tracks and removes features in stack order' do
2020
Features.track('A') do
21-
expect(Features.tracked).to eq(%w[A])
21+
expect(Features.tracked).to eq(%w[A].to_set)
2222
Features.track('B') do
23-
expect(Features.tracked).to eq(%w[A B])
23+
expect(Features.tracked).to eq(%w[A B].to_set)
2424
Features.track('C') do
25-
expect(Features.tracked).to eq(%w[A B C])
25+
expect(Features.tracked).to eq(%w[A B C].to_set)
2626
end
27-
expect(Features.tracked).to eq(%w[A B])
27+
expect(Features.tracked).to eq(%w[A B].to_set)
2828
end
29-
expect(Features.tracked).to eq(%w[A])
29+
expect(Features.tracked).to eq(%w[A].to_set)
3030
end
3131
expect(Features.tracked).to be_empty
3232
end
3333

3434
it 'ensures that features are removed' do
3535
begin
3636
Features.track('A') do
37-
expect(Features.tracked).to eq(%w[A])
37+
expect(Features.tracked).to eq(%w[A].to_set)
3838
raise StandardError
3939
end
4040
rescue StandardError
@@ -45,15 +45,30 @@ module Client
4545

4646
it 'tracks features in multiple threads' do
4747
Features.track('A') do
48-
expect(Features.tracked).to eq(%w[A])
48+
expect(Features.tracked).to eq(%w[A].to_set)
4949
Thread.new do
5050
expect(Features.tracked).to be_empty
5151
Features.track('B') do
52-
expect(Features.tracked).to eq(%w[B])
52+
expect(Features.tracked).to eq(%w[B].to_set)
5353
end
5454
expect(Features.tracked).to be_empty
5555
end.join
56-
expect(Features.tracked).to eq(%w[A])
56+
expect(Features.tracked).to eq(%w[A].to_set)
57+
end
58+
expect(Features.tracked).to be_empty
59+
end
60+
61+
it 'does not track duplicate features' do
62+
Features.track('A') do
63+
expect(Features.tracked).to eq(%w[A].to_set)
64+
Features.track('B') do
65+
expect(Features.tracked).to eq(%w[A B].to_set)
66+
Features.track('A') do
67+
expect(Features.tracked).to eq(%w[A B].to_set)
68+
end
69+
expect(Features.tracked).to eq(%w[A B].to_set)
70+
end
71+
expect(Features.tracked).to eq(%w[A].to_set)
5772
end
5873
expect(Features.tracked).to be_empty
5974
end

projections/shapes/lib/shapes/client.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ class Client < Smithy::Client::Base
161161
# @option options [Boolean] :stub_responses
162162
# When `true`, the client will return stubbed responses instead of networking requests.
163163
# By default fake responses are generated and returned. You can specify the response data
164-
# to return or errors to raise by calling {Stubs#stub_responses}.
165-
# @see Stubs
164+
# to return or errors to raise by calling {Smithy::Client::Stubs#stub_responses}.
166165
# @option options [String] :user_agent_suffix
167166
# An optional string that is appended to the User-Agent header.
168167
# The default User-Agent includes the smithy-client version,

projections/weather/lib/weather/client.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ class Client < Smithy::Client::Base
161161
# @option options [Boolean] :stub_responses
162162
# When `true`, the client will return stubbed responses instead of networking requests.
163163
# By default fake responses are generated and returned. You can specify the response data
164-
# to return or errors to raise by calling {Stubs#stub_responses}.
165-
# @see Stubs
164+
# to return or errors to raise by calling {Smithy::Client::Stubs#stub_responses}.
166165
# @option options [String] :user_agent_suffix
167166
# An optional string that is appended to the User-Agent header.
168167
# The default User-Agent includes the smithy-client version,

0 commit comments

Comments
 (0)