Skip to content

Commit a424ea8

Browse files
authored
Expose all properties of a GDTClock, better -hash and -isEqual impls (#2632)
* Expose all properties of a GDTClock, add -hash and -isEqual implementations for GDTStoredEvent * Style, remove unused define * Change GDTStoredEvent -isEqual implementation * Remove unintentionally committed files
1 parent 2438383 commit a424ea8

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

GoogleDataTransport.podspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ Shared library for iOS SDK data transport needs.
4646
s.test_spec 'Tests-Integration' do |test_spec|
4747
test_spec.requires_app_host = false
4848
test_spec.source_files = ['GoogleDataTransport/Tests/Integration/**/*.{h,m}'] + common_test_sources
49-
test_spec.compiler_flags = '-DGDT_LOG_TRACE_ENABLED=1'
5049
test_spec.dependency 'GCDWebServer'
5150
end
5251
end

GoogleDataTransport/GoogleDataTransport/Classes/GDTClock.m

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,7 @@ static int64_t UptimeInNanoseconds() {
7474
}
7575

7676
// TODO: Consider adding a 'trustedTime' property that can be populated by the response from a BE.
77-
@implementation GDTClock {
78-
/** The kernel boot time when this clock was created. */
79-
int64_t _kernelBootTime;
80-
81-
/** The device uptime when this clock was created. */
82-
int64_t _uptime;
83-
}
77+
@implementation GDTClock
8478

8579
- (instancetype)init {
8680
self = [super init];
@@ -118,7 +112,8 @@ - (BOOL)isAfter:(GDTClock *)otherClock {
118112
}
119113

120114
- (NSUInteger)hash {
121-
return [@(_kernelBootTime) hash] ^ [@(_uptime) hash] ^ [@(_timeMillis) hash];
115+
return [@(_kernelBootTime) hash] ^ [@(_uptime) hash] ^ [@(_timeMillis) hash] ^
116+
[@(_timezoneOffsetSeconds) hash];
122117
}
123118

124119
- (BOOL)isEqual:(id)object {

GoogleDataTransport/GoogleDataTransport/Classes/GDTStoredEvent.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,13 @@ - (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder {
8181
return self;
8282
}
8383

84+
- (BOOL)isEqual:(GDTStoredEvent *)other {
85+
return [self hash] == [other hash];
86+
}
87+
88+
- (NSUInteger)hash {
89+
return
90+
[_eventFileURL hash] ^ [_mappingID hash] ^ [_target hash] ^ [_clockSnapshot hash] ^ _qosTier;
91+
}
92+
8493
@end

GoogleDataTransport/GoogleDataTransport/Classes/Public/GDTClock.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ NS_ASSUME_NONNULL_BEGIN
2727
/** The offset from UTC in seconds. */
2828
@property(nonatomic, readonly) int64_t timezoneOffsetSeconds;
2929

30+
/** The kernel boot time when this clock was created. */
31+
@property(nonatomic, readonly) int64_t kernelBootTime;
32+
33+
/** The device uptime when this clock was created. */
34+
@property(nonatomic, readonly) int64_t uptime;
35+
3036
/** Creates a GDTClock object using the current time and offsets.
3137
*
3238
* @return A new GDTClock object representing the current time state.

GoogleDataTransport/Tests/Unit/GDTStoredEventTest.m

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,39 @@ - (void)testNSSecureCoding {
5050
XCTAssertNil(storedEvent.customPrioritizationParams);
5151
XCTAssertNotNil(storedEvent.eventFileURL);
5252
}
53+
54+
/** Tests equality between GDTStoredEvents. */
55+
- (void)testIsEqualAndHash {
56+
GDTEvent *event1 = [[GDTEvent alloc] initWithMappingID:@"1018" target:1];
57+
event1.clockSnapshot = [GDTClock snapshot];
58+
[event1.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
59+
[event1.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
60+
[event1.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"];
61+
[event1.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"];
62+
event1.qosTier = GDTEventQosDefault;
63+
event1.customPrioritizationParams = @{@"customParam1" : @"aValue1"};
64+
GDTStoredEvent *storedEvent1 =
65+
[event1 storedEventWithFileURL:[NSURL fileURLWithPath:@"/tmp/fake.txt"]];
66+
67+
GDTEvent *event2 = [[GDTEvent alloc] initWithMappingID:@"1018" target:1];
68+
event2.clockSnapshot = [GDTClock snapshot];
69+
[event2.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
70+
[event2.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
71+
[event2.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"];
72+
[event2.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"];
73+
event2.qosTier = GDTEventQosDefault;
74+
event2.customPrioritizationParams = @{@"customParam1" : @"aValue1"};
75+
GDTStoredEvent *storedEvent2 =
76+
[event2 storedEventWithFileURL:[NSURL fileURLWithPath:@"/tmp/fake.txt"]];
77+
78+
XCTAssertEqual([storedEvent1 hash], [storedEvent2 hash]);
79+
XCTAssertEqualObjects(storedEvent1, storedEvent2);
80+
81+
// This only really tests that changing the timezoneOffsetSeconds value causes a change in hash.
82+
[storedEvent2.clockSnapshot setValue:@(-25201) forKeyPath:@"timezoneOffsetSeconds"];
83+
84+
XCTAssertNotEqual([storedEvent1 hash], [storedEvent2 hash]);
85+
XCTAssertNotEqualObjects(storedEvent1, storedEvent2);
86+
}
87+
5388
@end

0 commit comments

Comments
 (0)