-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache JSON generation for individual messages
- Loading branch information
1 parent
e14207d
commit 874c417
Showing
3 changed files
with
41 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'spec_helper' | ||
|
||
module Segment | ||
class Analytics | ||
describe Message do | ||
describe '#to_json' do | ||
it 'caches JSON conversions' do | ||
# Keeps track of the number of times to_json was called | ||
nested_obj = Class.new do | ||
attr_reader :to_json_call_count | ||
|
||
def initialize | ||
@to_json_call_count = 0 | ||
end | ||
|
||
def to_json(*_) | ||
@to_json_call_count += 1 | ||
'{}' | ||
end | ||
end.new | ||
|
||
message = Message.new('some_key' => nested_obj) | ||
expect(nested_obj.to_json_call_count).to eq(0) | ||
|
||
message.to_json | ||
expect(nested_obj.to_json_call_count).to eq(1) | ||
|
||
# When called a second time, the call count shouldn't increase | ||
message.to_json | ||
expect(nested_obj.to_json_call_count).to eq(1) | ||
end | ||
end | ||
end | ||
end | ||
end |