Skip to content

Commit

Permalink
Change the "sleep" support to not block a thread.
Browse files Browse the repository at this point in the history
This should help with some things that do multiple fetches at a time as we were
stalling all connections.
  • Loading branch information
thomasvl committed Nov 8, 2024
1 parent e18fe7c commit 5db7351
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions UnitTests/GTMHTTPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ enum {
@property(nonatomic, readonly) int statusCode;
@property(nonatomic, copy) NSData *body;

// If non zero, the response will be queued tobe sent after this amount of time.
@property(nonatomic) double delaySeconds;

+ (instancetype)responseWithString:(NSString *)plainText;
+ (instancetype)responseWithHTMLString:(NSString *)htmlString;
+ (instancetype)responseWithBody:(NSData *)body
Expand Down
17 changes: 16 additions & 1 deletion UnitTests/GTMHTTPServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,20 @@ - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
NSOutputStream *outputStream = connDict[kOutputStream];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[outputStream open];
if (response.delaySeconds) {
dispatch_time_t delayTime =
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(response.delaySeconds * NSEC_PER_SEC));
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
@try {
[outputStream open];
} @catch (NSException *e) {
NSLog(@"Exception while doing a delayed response open: %@", e);
} // COV_NF_LINE - radar 5851992 only reachable w/ an uncaught exception,
// un-testable
});
} else {
[outputStream open];
}
}
}
} @catch (NSException *e) { // COV_NF_START
Expand Down Expand Up @@ -651,6 +664,8 @@ @implementation GTMHTTPResponseMessage {
CFHTTPMessageRef _message;
}

@synthesize delaySeconds = _delaySeconds;

- (instancetype)init {
return [self initWithBody:nil contentType:nil statusCode:0];
}
Expand Down
6 changes: 3 additions & 3 deletions UnitTests/GTMSessionFetcherTestServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,9 @@ - (GTMHTTPResponseMessage *)httpServer:(GTMHTTPServer *)server

NSString *sleepStr = [[self class] valueForParameter:@"sleep" query:query];
if (sleepStr) {
NSTimeInterval interval = [sleepStr doubleValue];
[NSThread sleepForTimeInterval:interval];
return sendResponse(408, nil, nil); // request timeout
GTMHTTPResponseMessage *response = sendResponse(408, nil, nil); // request timeout
response.delaySeconds = [sleepStr doubleValue];
return response;
}
if (ifMatch != nil && ![ifMatch isEqual:kEtag]) {
// there is no match, hence this is an inconsistent PUT or DELETE
Expand Down

0 comments on commit 5db7351

Please sign in to comment.