StressTests.m
4.26 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
//
// StressTests.m
// iPhone
//
// 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"
@implementation MyDelegate;
- (void)dealloc
{
[request setDelegate:nil];
[request release];
[super dealloc];
}
@synthesize request;
@end
@implementation StressTests
// 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
{
[[ASIHTTPRequest sharedRequestQueue] setMaxConcurrentOperationCount:20];
[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 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];
[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];
}
}
- (void)cancelSetDelegateRequest
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self performSetDelegateRequest];
[pool release];
}
- (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;
@end