diff --git a/ruby_event_store/lib/ruby_event_store/client.rb b/ruby_event_store/lib/ruby_event_store/client.rb index 008fac52e4..da5890625d 100644 --- a/ruby_event_store/lib/ruby_event_store/client.rb +++ b/ruby_event_store/lib/ruby_event_store/client.rb @@ -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 diff --git a/ruby_event_store/spec/client_spec.rb b/ruby_event_store/spec/client_spec.rb index 4af1fa27af..7a97a35c43 100644 --- a/ruby_event_store/spec/client_spec.rb +++ b/ruby_event_store/spec/client_spec.rb @@ -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