-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathSenAsyncTestCase.m
82 lines (63 loc) · 2.23 KB
/
SenAsyncTestCase.m
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
//
// SenAsyncTestCase.m
// AsyncSenTestingKit
//
// Created by 小野 将司 on 12/03/17.
// Copyright (c) 2012年 AppBankGames Inc. All rights reserved.
//
#import "SenAsyncTestCase.h"
@interface SenAsyncTestCase ()
@property (nonatomic, retain) NSDate *loopUntil;
@property (nonatomic, assign) BOOL notified;
@property (nonatomic, assign) SenAsyncTestCaseStatus notifiedStatus;
@property (nonatomic, assign) SenAsyncTestCaseStatus expectedStatus;
@end
@implementation SenAsyncTestCase
@synthesize loopUntil = _loopUntil;
@synthesize notified = _notified;
@synthesize notifiedStatus = _notifiedStatus;
@synthesize expectedStatus = _expectedStatus;
- (void)dealloc
{
self.loopUntil = nil;
[super dealloc];
}
#pragma mark - Public
- (void)waitForStatus:(SenAsyncTestCaseStatus)status timeout:(NSTimeInterval)timeout
{
self.notified = NO;
self.expectedStatus = status;
self.loopUntil = [NSDate dateWithTimeIntervalSinceNow:timeout];
NSDate *dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
while (!self.notified && [self.loopUntil timeIntervalSinceNow] > 0) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:dt];
dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
}
// Only assert when notified. Do not assert when timed out
// Fail if not notified
if (self.notified) {
STAssertEquals(self.notifiedStatus, self.expectedStatus, @"Notified status does not match the expected status.");
} else {
STFail(@"Async test timed out.");
}
}
- (void)waitForTimeout:(NSTimeInterval)timeout
{
self.notified = NO;
self.expectedStatus = SenAsyncTestCaseStatusUnknown;
self.loopUntil = [NSDate dateWithTimeIntervalSinceNow:timeout];
NSDate *dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
while (!self.notified && [self.loopUntil timeIntervalSinceNow] > 0) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:dt];
dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
}
}
- (void)notify:(SenAsyncTestCaseStatus)status
{
self.notifiedStatus = status;
// self.notified must be set at the last of this method
self.notified = YES;
}
@end