Skip to content

Commit

Permalink
Ensure each instance of RubyEventStore::Client has its own metadata
Browse files Browse the repository at this point in the history
Store metadata in thread local variable that has unique name for each instance
so that you can nest `RubyEventStore::Client#with_metadata` calls of different
client instances without overwriting the metadata
  • Loading branch information
jakubkosinski committed Apr 30, 2018
1 parent 331834b commit 9ee2174
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ruby_event_store/lib/ruby_event_store/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ def enrich_event_metadata(event)
protected

def metadata
Thread.current[:ruby_event_store]
Thread.current["ruby_event_store_#{hash}"]
end

def metadata=(value)
Thread.current[:ruby_event_store] = value
Thread.current["ruby_event_store_#{hash}"] = value
end
end
end
19 changes: 19 additions & 0 deletions ruby_event_store/spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,25 @@ module RubyEventStore
expect(published.first.metadata[:timestamp]).to be_a Time
end

specify 'metadata is bound to the current instance and does not leak to others' do
client_a = RubyEventStore::Client.new(repository: InMemoryRepository.new)
client_b = RubyEventStore::Client.new(repository: InMemoryRepository.new)

client_a.with_metadata(client: 'a') do
client_b.with_metadata(client: 'b') do
client_a.publish_event(TestEvent.new)
client_b.publish_event(TestEvent.new)
end
end

published_a = client_a.read_all_streams_forward
published_b = client_b.read_all_streams_forward
expect(published_a.size).to eq(1)
expect(published_b.size).to eq(1)
expect(published_a.last.metadata[:client]).to eq('a')
expect(published_b.last.metadata[:client]).to eq('b')
end

specify 'timestamp can be overwritten by using with_metadata' do
client = RubyEventStore::Client.new(repository: InMemoryRepository.new)
event = TestEvent.new
Expand Down

0 comments on commit 9ee2174

Please sign in to comment.