Skip to content

Expose all properties of a GDTClock, better -hash and -isEqual impls #2632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion GoogleDataTransport.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Shared library for iOS SDK data transport needs.
s.test_spec 'Tests-Integration' do |test_spec|
test_spec.requires_app_host = false
test_spec.source_files = ['GoogleDataTransport/Tests/Integration/**/*.{h,m}'] + common_test_sources
test_spec.compiler_flags = '-DGDT_LOG_TRACE_ENABLED=1'
test_spec.dependency 'GCDWebServer'
end
end
11 changes: 3 additions & 8 deletions GoogleDataTransport/GoogleDataTransport/Classes/GDTClock.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,7 @@ static int64_t UptimeInNanoseconds() {
}

// TODO: Consider adding a 'trustedTime' property that can be populated by the response from a BE.
@implementation GDTClock {
/** The kernel boot time when this clock was created. */
int64_t _kernelBootTime;

/** The device uptime when this clock was created. */
int64_t _uptime;
}
@implementation GDTClock

- (instancetype)init {
self = [super init];
Expand Down Expand Up @@ -118,7 +112,8 @@ - (BOOL)isAfter:(GDTClock *)otherClock {
}

- (NSUInteger)hash {
return [@(_kernelBootTime) hash] ^ [@(_uptime) hash] ^ [@(_timeMillis) hash];
return [@(_kernelBootTime) hash] ^ [@(_uptime) hash] ^ [@(_timeMillis) hash] ^
[@(_timezoneOffsetSeconds) hash];
}

- (BOOL)isEqual:(id)object {
Expand Down
11 changes: 11 additions & 0 deletions GoogleDataTransport/GoogleDataTransport/Classes/GDTStoredEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,15 @@ - (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder {
return self;
}

- (BOOL)isEqual:(id)object {
NSAssert([object isKindOfClass:[self class]], @"You must compare like classes.");
GDTStoredEvent *other = (GDTStoredEvent *)object;
return [self hash] == [other hash];
}

- (NSUInteger)hash {
return
[_eventFileURL hash] ^ [_mappingID hash] ^ [_target hash] ^ [_clockSnapshot hash] ^ _qosTier;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ NS_ASSUME_NONNULL_BEGIN
/** The offset from UTC in seconds. */
@property(nonatomic, readonly) int64_t timezoneOffsetSeconds;

/** The kernel boot time when this clock was created. */
@property(nonatomic, readonly) int64_t kernelBootTime;

/** The device uptime when this clock was created. */
@property(nonatomic, readonly) int64_t uptime;

/** Creates a GDTClock object using the current time and offsets.
*
* @return A new GDTClock object representing the current time state.
Expand Down
35 changes: 35 additions & 0 deletions GoogleDataTransport/Tests/Unit/GDTStoredEventTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,39 @@ - (void)testNSSecureCoding {
XCTAssertNil(storedEvent.customPrioritizationParams);
XCTAssertNotNil(storedEvent.eventFileURL);
}

/** Tests equality between GDTStoredEvents. */
- (void)testIsEqualAndHash {
GDTEvent *event1 = [[GDTEvent alloc] initWithMappingID:@"1018" target:1];
event1.clockSnapshot = [GDTClock snapshot];
[event1.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
[event1.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
[event1.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"];
[event1.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"];
event1.qosTier = GDTEventQosDefault;
event1.customPrioritizationParams = @{@"customParam1" : @"aValue1"};
GDTStoredEvent *storedEvent1 =
[event1 storedEventWithFileURL:[NSURL fileURLWithPath:@"/tmp/fake.txt"]];

GDTEvent *event2 = [[GDTEvent alloc] initWithMappingID:@"1018" target:1];
event2.clockSnapshot = [GDTClock snapshot];
[event2.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
[event2.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
[event2.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"];
[event2.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"];
event2.qosTier = GDTEventQosDefault;
event2.customPrioritizationParams = @{@"customParam1" : @"aValue1"};
GDTStoredEvent *storedEvent2 =
[event2 storedEventWithFileURL:[NSURL fileURLWithPath:@"/tmp/fake.txt"]];

XCTAssertEqual([storedEvent1 hash], [storedEvent2 hash]);
XCTAssertEqualObjects(storedEvent1, storedEvent2);

// This only really tests that changing the timezoneOffsetSeconds value causes a change in hash.
[storedEvent2.clockSnapshot setValue:@(-25201) forKeyPath:@"timezoneOffsetSeconds"];

XCTAssertNotEqual([storedEvent1 hash], [storedEvent2 hash]);
XCTAssertNotEqualObjects(storedEvent1, storedEvent2);
}

@end