Persist session events immediately after creation (remove ununsed timer code)

This commit is contained in:
Christoph Wendt 2015-09-11 12:36:54 -07:00
parent ea922447df
commit bae361be8a
3 changed files with 8 additions and 45 deletions

View File

@ -70,10 +70,13 @@ FOUNDATION_EXPORT char *BITSafeJsonEventsString;
@property (nonatomic, assign) NSUInteger dataItemCount;
/**
* A timer source which is used to flush the queue after a cretain time.
* Initializes a new BITChannel instance.
*
* @param telemetryContext the context used to add context values to the metrics payload
* @param persistence the persistence used to save metrics after the queue gets flushed
*
* @return the telemetry context
*/
@property (nonatomic, strong, null_unspecified) dispatch_source_t timerSource;
- (instancetype)initWithTelemetryContext:(BITTelemetryContext *)telemetryContext persistence:(BITPersistence *) persistence;
/**
@ -110,16 +113,6 @@ void bit_appendStringToSafeJsonStream(NSString *string, char *__nonnull*__nonnul
*/
void bit_resetSafeJsonStream(char *__nonnull*__nonnull jsonStream);
/**
* Starts the timer.
*/
- (void)startTimer;
/**
* Stops the timer if currently running.
*/
- (void)invalidateTimer;
/**
* A method which indicates whether the telemetry pipeline is busy and no new data should be enqueued.
* Currently, we drop telemetry data if this returns YES.

View File

@ -16,8 +16,7 @@
static char *const BITDataItemsOperationsQueue = "net.hockeyapp.senderQueue";
char *BITSafeJsonEventsString;
NSInteger const defaultBatchInterval = 20;
NSInteger const defaultMaxBatchCount = 50;
NSInteger const defaultMaxBatchCount = 1;
static NSInteger const schemaVersion = 2;
@implementation BITChannel
@ -53,7 +52,6 @@ static NSInteger const schemaVersion = 2;
}
- (void)persistDataItemQueue {
[self invalidateTimer];
if(!BITSafeJsonEventsString || strlen(BITSafeJsonEventsString) == 0) {
return;
}
@ -87,9 +85,6 @@ static NSInteger const schemaVersion = 2;
// Max batch count has been reached, so write queue to disk and delete all items.
[strongSelf persistDataItemQueue];
} else if(strongSelf->_dataItemCount == 1) {
// It is the first item, let's start the timer
[strongSelf startTimer];
}
});
}
@ -187,30 +182,6 @@ void bit_resetSafeJsonStream(char **string) {
#pragma mark - Batching
- (void)invalidateTimer {
if(self.timerSource) {
dispatch_source_cancel(self.timerSource);
self.timerSource = nil;
}
}
- (void)startTimer {
// Reset timer, if it is already running
if(self.timerSource) {
[self invalidateTimer];
}
self.timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.dataItemsOperations);
dispatch_source_set_timer(self.timerSource, dispatch_walltime(NULL, NSEC_PER_SEC * defaultBatchInterval), 1ull * NSEC_PER_SEC, 1ull * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.timerSource, ^{
// On completion: Reset timer and persist items
[self persistDataItemQueue];
});
dispatch_resume(self.timerSource);
}
- (NSInteger)maxBatchCount {
if(_maxBatchCount <= 0){
return defaultMaxBatchCount;

View File

@ -45,6 +45,7 @@
- (void)testEnqueueEnvelopeWithOneEnvelopeAndJSONStream {
_sut = OCMPartialMock(_sut);
_sut.maxBatchCount = 3;
BITTelemetryData *testData = [BITTelemetryData new];
[_sut enqueueTelemetryItem:testData];
@ -52,7 +53,6 @@
dispatch_sync(_sut.dataItemsOperations, ^{
assertThatUnsignedInteger(_sut.dataItemCount, equalToUnsignedInteger(1));
XCTAssertTrue(strlen(BITSafeJsonEventsString) > 0);
OCMVerify([_sut startTimer]);
});
}
@ -78,7 +78,6 @@
[_sut enqueueTelemetryItem:testData];
dispatch_sync(_sut.dataItemsOperations, ^{
OCMVerify([_sut invalidateTimer]);
assertThatUnsignedInteger(_sut.dataItemCount, equalToUnsignedInteger(0));
XCTAssertTrue(strcmp(BITSafeJsonEventsString, "") == 0);
});