StressTests.m
5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// StressTests.m
// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest
//
// Created by Ben Copsey on 30/10/2009.
// Copyright 2009 All-Seeing Interactive. All rights reserved.
//
/*
IMPORTANT
All these tests depend on you running a local webserver on port 80
These tests create 1000s of requests in a very short space of time - DO NOT RUN THESE TESTS ON A REMOTE WEBSERVER
IMPORTANT
*/
#import "StressTests.h"
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
@implementation MyDelegate;
- (void)dealloc
{
[request setDelegate:nil];
[request release];
[super dealloc];
}
@synthesize request;
@end
@implementation StressTests
// A test for a potential crasher that used to exist when requests were cancelled
// We aren't testing a specific condition here, but rather attempting to trigger a crash
- (void)testCancelQueue
{
ASINetworkQueue *queue = [ASINetworkQueue queue];
// Increase the risk of this crash
[queue setMaxConcurrentOperationCount:25];
int i;
for (i=0; i<100; i++) {
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1"]];
[queue addOperation:request];
}
[queue go];
[queue cancelAllOperations];
// Run the test again with requests running on a background thread
queue = [ASINetworkQueue queue];
[queue setMaxConcurrentOperationCount:25];
for (i=0; i<100; i++) {
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1"]];
[queue addOperation:request];
}
[queue go];
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
[queue cancelAllOperations];
}
// This test looks for thread-safety problems with cancelling requests
// It will run for 30 seconds, creating a request, then cancelling it and creating another as soon as it gets some indication of progress
- (void)testCancelStressTest
{
[self setCancelStartDate:[NSDate dateWithTimeIntervalSinceNow:30]];
[self performCancelRequest];
while ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
}
NSLog(@"Stress test: DONE");
}
- (void)performCancelRequest
{
[self setCancelRequest:[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1/ASIHTTPRequest/tests/the_great_american_novel.txt"]]];
if ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
[[self cancelRequest] setDownloadProgressDelegate:self];
[[self cancelRequest] setShowAccurateProgress:YES];
NSLog(@"Stress test: Start request %@",[self cancelRequest]);
[[self cancelRequest] startAsynchronous];
}
}
// Another stress test that looks from problems when redirecting
- (void)testRedirectStressTest
{
[self setCancelStartDate:[NSDate dateWithTimeIntervalSinceNow:30]];
[self performRedirectRequest];
while ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
}
NSLog(@"Redirect stress test: DONE");
}
- (void)performRedirectRequest
{
[self setCancelRequest:[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1/ASIHTTPRequest/tests/one_infinite_loop"]]];
if ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
NSLog(@"Redirect stress test: Start request %@",[self cancelRequest]);
[[self cancelRequest] startAsynchronous];
[self performSelector:@selector(cancelRedirectRequest) withObject:nil afterDelay:0.2];
}
}
- (void)cancelRedirectRequest
{
NSLog(@"Redirect stress test: Cancel request %@",[self cancelRequest]);
[[self cancelRequest] cancel];
[self performRedirectRequest];
}
// Ensures we can set the delegate while the request is running without problems
- (void)testSetDelegate
{
[self setCreateRequestLock:[[[NSLock alloc] init] autorelease]];
[self setCancelStartDate:[NSDate dateWithTimeIntervalSinceNow:30]];
[self performSetDelegateRequest];
while ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
}
NSLog(@"Set delegate stress test: DONE");
}
- (void)performSetDelegateRequest
{
[self setDelegate:nil];
[createRequestLock lock];
[self setCancelRequest:[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1/ASIHTTPRequest/tests/the_great_american_novel.txt"]]];
if ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
[self setDelegate:[[[MyDelegate alloc] init] autorelease]];
[[self delegate] setRequest:[self cancelRequest]];
[[self cancelRequest] setDelegate:delegate];
[[self cancelRequest] setShowAccurateProgress:YES];
NSLog(@"Set delegate stress test: Start request %@",[self cancelRequest]);
[[self cancelRequest] startAsynchronous];
[self performSelectorInBackground:@selector(cancelSetDelegateRequest) withObject:nil];
}
[createRequestLock unlock];
}
- (void)cancelSetDelegateRequest
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self performSetDelegateRequest];
[pool release];
}
- (void)setDoubleValue:(double)newProgress
{
[self setProgress:(float)newProgress];
}
- (void)setProgress:(float)newProgress
{
progress = newProgress;
// For cancel test
if (newProgress > 0 && [self cancelRequest]) {
NSLog(@"Stress test: Cancel request %@",[self cancelRequest]);
[[self cancelRequest] cancel];
[self performSelector:@selector(performCancelRequest) withObject:nil afterDelay:0.2];
[self setCancelRequest:nil];
}
}
@synthesize cancelRequest;
@synthesize cancelStartDate;
@synthesize delegate;
@synthesize createRequestLock;
@end