Statistics
| Branch: | Revision:

root / asi-http-request-with-pithos / Classes / Tests / StressTests.m @ be116d22

History | View | Annotate | Download (5.6 kB)

1
//
2
//  StressTests.m
3
//  Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest
4
//
5
//  Created by Ben Copsey on 30/10/2009.
6
//  Copyright 2009 All-Seeing Interactive. All rights reserved.
7
//
8

    
9
/*
10
IMPORTANT
11
All these tests depend on you running a local webserver on port 80
12
These tests create 1000s of requests in a very short space of time - DO NOT RUN THESE TESTS ON A REMOTE WEBSERVER
13
IMPORTANT
14
*/
15

    
16
#import "StressTests.h"
17
#import "ASIHTTPRequest.h"
18
#import "ASINetworkQueue.h"
19

    
20

    
21

    
22
@implementation MyDelegate;
23
- (void)dealloc
24
{
25
	[request setDelegate:nil];
26
	[request release];
27
	[super dealloc];
28
}
29
@synthesize request;
30
@end
31

    
32
// Stop clang complaining about undeclared selectors
33
@interface StressTests ()
34
- (void)cancelRedirectRequest;
35
- (void)cancelSetDelegateRequest;
36
@end
37

    
38

    
39

    
40
@implementation StressTests
41

    
42
// A test for a potential crasher that used to exist when requests were cancelled
43
// We aren't testing a specific condition here, but rather attempting to trigger a crash
44
- (void)testCancelQueue
45
{
46
	ASINetworkQueue *queue = [ASINetworkQueue queue];
47
	
48
	// Increase the risk of this crash
49
	[queue setMaxConcurrentOperationCount:25];
50
	int i;
51
	for (i=0; i<100; i++) {
52
		ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1"]];
53
		[queue addOperation:request];
54
	}
55
	[queue go];
56
	[queue cancelAllOperations];
57
	
58
	// Run the test again with requests running on a background thread
59
	queue = [ASINetworkQueue queue];
60

    
61
	[queue setMaxConcurrentOperationCount:25];
62
	for (i=0; i<100; i++) {
63
		ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1"]];
64
		[queue addOperation:request];
65
	}
66
	[queue go];
67
	[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
68
	[queue cancelAllOperations];
69
	
70
}
71

    
72
// This test looks for thread-safety problems with cancelling requests
73
// It will run for 30 seconds, creating a request, then cancelling it and creating another as soon as it gets some indication of progress
74

    
75
- (void)testCancelStressTest
76
{
77
	[self setCancelStartDate:[NSDate dateWithTimeIntervalSinceNow:30]];
78
	[self performCancelRequest];
79
	while ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
80
		[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
81
	}
82
	NSLog(@"Stress test: DONE");
83
}
84

    
85
- (void)performCancelRequest
86
{
87
	[self setCancelRequest:[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1/ASIHTTPRequest/tests/the_great_american_novel.txt"]]];
88
	if ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
89
		[[self cancelRequest] setDownloadProgressDelegate:self];
90
		[[self cancelRequest] setShowAccurateProgress:YES];
91
		NSLog(@"Stress test: Start request %@",[self cancelRequest]);
92
		[[self cancelRequest] startAsynchronous];
93
	}
94
}
95

    
96

    
97
// Another stress test that looks from problems when redirecting
98

    
99
- (void)testRedirectStressTest
100
{
101
	[self setCancelStartDate:[NSDate dateWithTimeIntervalSinceNow:30]];
102
	[self performRedirectRequest];
103
	while ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
104
		[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
105
	}
106
	NSLog(@"Redirect stress test: DONE");
107
}
108

    
109
- (void)performRedirectRequest
110
{
111
	[self setCancelRequest:[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1/ASIHTTPRequest/tests/one_infinite_loop"]]];
112
	if ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
113
		NSLog(@"Redirect stress test: Start request %@",[self cancelRequest]);
114
		[[self cancelRequest] startAsynchronous];
115
		[self performSelector:@selector(cancelRedirectRequest) withObject:nil afterDelay:0.2];
116
	}
117
}
118

    
119
- (void)cancelRedirectRequest
120
{
121
	NSLog(@"Redirect stress test: Cancel request %@",[self cancelRequest]);
122
	[[self cancelRequest] cancel];
123
	[self performRedirectRequest];
124
}
125

    
126
// Ensures we can set the delegate while the request is running without problems
127
- (void)testSetDelegate
128
{
129
	[self setCreateRequestLock:[[[NSLock alloc] init] autorelease]];
130
	[self setCancelStartDate:[NSDate dateWithTimeIntervalSinceNow:30]];
131
	[self performSetDelegateRequest];
132
	while ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
133
		[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
134
	}
135
	NSLog(@"Set delegate stress test: DONE");
136
}
137

    
138
- (void)performSetDelegateRequest
139
{
140
	[self setDelegate:nil];
141
	
142
	[createRequestLock lock];
143
	[self setCancelRequest:[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1/ASIHTTPRequest/tests/the_great_american_novel.txt"]]];
144
	if ([[self cancelStartDate] timeIntervalSinceNow] > 0) {
145
		[self setDelegate:[[[MyDelegate alloc] init] autorelease]];
146
		[[self delegate] setRequest:[self cancelRequest]];
147
		[[self cancelRequest] setDelegate:delegate];
148
		[[self cancelRequest] setShowAccurateProgress:YES];
149
		NSLog(@"Set delegate stress test: Start request %@",[self cancelRequest]);
150
		[[self cancelRequest] startAsynchronous];
151
		[self performSelectorInBackground:@selector(cancelSetDelegateRequest) withObject:nil];
152
	}
153
	[createRequestLock unlock];
154
}
155

    
156
- (void)cancelSetDelegateRequest
157
{
158
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
159
	[self performSetDelegateRequest];
160
	[pool release];
161
}
162

    
163

    
164
- (void)setDoubleValue:(double)newProgress
165
{
166
	[self setProgress:(float)newProgress];
167
}
168

    
169
- (void)setProgress:(float)newProgress
170
{
171
	progress = newProgress;
172
	
173
	// For cancel test
174
	if (newProgress > 0 && [self cancelRequest]) {
175
		
176
		NSLog(@"Stress test: Cancel request %@",[self cancelRequest]);
177
		[[self cancelRequest] cancel];
178
		
179
		[self performSelector:@selector(performCancelRequest) withObject:nil afterDelay:0.2];
180
		[self setCancelRequest:nil];
181
	}
182
}
183

    
184

    
185

    
186

    
187

    
188
@synthesize cancelRequest;
189
@synthesize cancelStartDate;
190
@synthesize delegate;
191
@synthesize createRequestLock;
192
@end