ObjFW  Diff

Differences From Artifact [c839b85266]:

To Artifact [8dbc611e84]:

  • File tests/OFKernelEventObserverTests.m — part of check-in [220513a3f5] at 2015-05-09 18:13:19 on branch trunk — More OFKernelEventObserver refactoring

    This was necessary because select(), poll() and kevent() on FreeBSD and
    OS X would sometimes return 0 events, even if there are some, and
    sometimes return the correct number of events that were pending, meaning
    the number of events returned is unreliable. To make things worse,
    whether it returns 0 or the number of events that were pending is
    completely non-deterministic on both FreeBSD and OS X (running the same
    tests multiple times in a row would make it sometimes work and sometimes
    fail).

    In order to prevent code from depending on the return value of
    -[observeForTimeInterval:] (which would depend on select(), poll() and
    kevent() returning the correct number), OFKernelObserver no longer
    returns whether there were pending events. It is expected that
    -[observe] or -[observeForTimeInterval:] is just called in a loop as
    long as events should be handled.

    The tests have been changed as well to reflect this. What they do now is
    set a deadline and call -[observeForTimeInterval:] with a small timeout
    in a loop until the deadline is reached or all events have been handled.

    Note: DragonFlyBSD has not been tested, but will most likely behave like
    FreeBSD and OS X. (user: js, size: 5326) [annotate] [blame] [check-ins using]


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
 * file.
 */

#include "config.h"

#import "OFKernelEventObserver.h"
#import "OFString.h"

#import "OFTCPSocket.h"
#import "OFAutoreleasePool.h"

#if defined(HAVE_SYS_SELECT_H) || defined(_WIN32)
# import "OFKernelEventObserver_select.h"
#endif
#if defined(HAVE_POLL_H) || defined(__wii__)
# import "OFKernelEventObserver_poll.h"
#endif
#ifdef HAVE_EPOLL
# import "OFKernelEventObserver_epoll.h"
#endif
#ifdef HAVE_KQUEUE
# import "OFKernelEventObserver_kqueue.h"
#endif

#import "TestsAppDelegate.h"



static OFString *module;





static OFKernelEventObserver *observer;

static int events;
static id expectedObject;
static bool readData, expectEOS;
static OFTCPSocket *accepted;

@interface ObserverDelegate: OFObject
- (void)objectIsReadyForReading: (id)object;
@end

@implementation ObserverDelegate










































































- (void)objectIsReadyForReading: (id)object
{
	events++;




	OF_ENSURE(object == expectedObject);








	if ([object isListening]) {
		accepted = [[object accept] retain];








		[accepted writeBuffer: "0"

			       length: 1];




	} else if (readData) {




		char buf;





		if (expectEOS)



			OF_ENSURE([object readIntoBuffer: &buf
						  length: 1] == 0);



		else {



			OF_ENSURE([object readIntoBuffer: &buf
						  length: 1] == 1);


			OF_ENSURE(buf == '0');
		}
	}
}
@end

@implementation TestsAppDelegate (OFKernelEventObserverTests)
- (void)kernelEventObserverTestsWithClass: (Class)class
{
	ObserverDelegate *delegate =
	    [[[ObserverDelegate alloc] init] autorelease];
	OFTCPSocket *sock1 = [OFTCPSocket socket];
	OFTCPSocket *sock2 = [OFTCPSocket socket];
	uint16_t port;

	module = [class className];
	events = 0;
	expectedObject = nil;
	readData = expectEOS = false;
	accepted = nil;

	port = [sock1 bindToHost: @"127.0.0.1"
			    port: 0];
	[sock1 listen];

	TEST(@"+[observer]",
	    (observer = [class observer]) &&
	    R([observer setDelegate: delegate]))

	TEST(@"-[addObjectForReading:]",
	    R([observer addObjectForReading: sock1]))

	[sock2 connectToHost: @"127.0.0.1"
			port: port];
	TEST(@"-[observe] waiting for connection",
	    (expectedObject = sock1) &&
	    [observer observeForTimeInterval: 0.01])
	[accepted autorelease];

	TEST(@"-[observe] waiting for data",
	    (expectedObject = sock2) &&
	    R([observer addObjectForReading: sock2]) &&
	    [observer observeForTimeInterval: 0.01])

	TEST(@"-[observe] keeping event until read",
	    R(readData = true) && [observer observeForTimeInterval: 0.01])

	TEST(@"-[observe] time out due to no events",
	    R(readData = false) && ![observer observeForTimeInterval: 0.01])

	[accepted close];
	TEST(@"-[observe] closed connection",
	    R(readData = true) && R(expectEOS = true) &&
	    [observer observeForTimeInterval: 0.01])

	TEST(@"-[observe] correct number of events", events == 4)
}

- (void)kernelEventObserverTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

#if defined(HAVE_SYS_SELECT_H) || defined(_WIN32)







>


















>
>

>
>
>
>
>
|
>
|
<
<
<
|
|
|


|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


<
>

>
>
|
>
>
>
>
>
>
>

<
|
>

>
>
>
>
>
>
|
>
|
>
>
>
>
|
>
>
>
>
|
>

>
>
>
|
>
>
>
|
|
>
>
>
|
>
>
>
|
<
>
>
|
<







|
<
<
<
<


|
<
<
<
|
<
<
<


|
|


|

<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







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
186

187
188
189

190
191
192
193
194
195
196
197




198
199
200



201



202
203
204
205
206
207
208
209






210

















211
212
213
214
215
216
217
 * file.
 */

#include "config.h"

#import "OFKernelEventObserver.h"
#import "OFString.h"
#import "OFDate.h"
#import "OFTCPSocket.h"
#import "OFAutoreleasePool.h"

#if defined(HAVE_SYS_SELECT_H) || defined(_WIN32)
# import "OFKernelEventObserver_select.h"
#endif
#if defined(HAVE_POLL_H) || defined(__wii__)
# import "OFKernelEventObserver_poll.h"
#endif
#ifdef HAVE_EPOLL
# import "OFKernelEventObserver_epoll.h"
#endif
#ifdef HAVE_KQUEUE
# import "OFKernelEventObserver_kqueue.h"
#endif

#import "TestsAppDelegate.h"

#define EXPECTED_EVENTS 3

static OFString *module;

@interface ObserverTest: OFObject
{
@public
	TestsAppDelegate *_testsAppDelegate;
	OFKernelEventObserver *_observer;
	OFTCPSocket *_server, *_client, *_accepted;
	size_t _events;



}

- (void)run;
@end

@implementation ObserverTest
- initWithTestsAppDelegate: (TestsAppDelegate*)testsAppDelegate
{
	self = [super init];

	@try {
		uint16_t port;

		_testsAppDelegate = testsAppDelegate;

		_server = [[OFTCPSocket alloc] init];
		port = [_server bindToHost: @"127.0.0.1"
				      port: 0];
		[_server listen];

		_client = [[OFTCPSocket alloc] init];
		[_client connectToHost: @"127.0.0.1"
				  port: port];

		[_client writeBuffer: "0"
			      length: 1];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_server release];
	[_client release];
	[_accepted release];

	[super dealloc];
}

- (void)run
{
	OFDate *deadline;
	bool deadlineExceeded = false;

	[_testsAppDelegate outputTesting: @"-[observe] with listening socket"
				inModule: module];

	deadline = [OFDate dateWithTimeIntervalSinceNow: 1];
	while (_events < EXPECTED_EVENTS) {
		if ([deadline timeIntervalSinceNow] < 0) {
			deadlineExceeded = true;
			break;
		}

		[_observer observeForTimeInterval: 0.01];
	}

	if (!deadlineExceeded)
		[_testsAppDelegate
		    outputSuccess: @"-[observe] not exceeding deadline"
			 inModule: module];
	else
		[_testsAppDelegate
		    outputFailure: @"-[observe] not exceeding deadline"
			 inModule: module];

	if (_events == EXPECTED_EVENTS)
		[_testsAppDelegate
		    outputSuccess: @"-[observe] handling all events"
			 inModule: module];
	else
		[_testsAppDelegate
		    outputFailure: @"-[observe] handling all events"
			 inModule: module];
}

- (void)objectIsReadyForReading: (id)object
{

	char buf;

	switch (_events++) {
	case 0:
		if (object == _server)
			[_testsAppDelegate
			    outputSuccess: @"-[observe] with listening socket"
				 inModule: module];
		else
			[_testsAppDelegate
			    outputFailure: @"-[observe] with listening socket"
				 inModule: module];


		_accepted = [[object accept] retain];
		[_observer addObjectForReading: _accepted];

		[_testsAppDelegate
		    outputTesting: @"-[observe] with data to read available"
			 inModule: module];

		break;
	case 1:
		if (object == _accepted &&
		    [object readIntoBuffer: &buf
				    length: 1] == 1 && buf == '0')
			[_testsAppDelegate
			    outputSuccess: @"-[observe] with data to read "
					   @"available"
				 inModule: module];
		else
			[_testsAppDelegate
			    outputFailure: @"-[observe] with data to read "
					   @"available"
				 inModule: module];

		[_client close];

		[_testsAppDelegate
		    outputTesting: @"-[observe] with closed connection"
			 inModule: module];

		break;
	case 2:
		if (object == _accepted &&
		    [object readIntoBuffer: &buf
				    length: 1] == 0)
			[_testsAppDelegate
			    outputSuccess: @"-[observe] with closed connection"
				 inModule: module];
		else
			[_testsAppDelegate
			    outputFailure: @"-[observe] with closed connection"
				 inModule: module];


		break;
	default:
		OF_ENSURE(0);

	}
}
@end

@implementation TestsAppDelegate (OFKernelEventObserverTests)
- (void)kernelEventObserverTestsWithClass: (Class)class
{
	ObserverTest *test;





	module = [class className];
	test = [[[ObserverTest alloc]



	    initWithTestsAppDelegate: self] autorelease];




	TEST(@"+[observer]",
	    (test->_observer = [OFKernelEventObserver observer]))
	[test->_observer setDelegate: test];

	TEST(@"-[addObjectForReading:]",
	    R([test->_observer addObjectForReading: test->_server]))







	[test run];

















}

- (void)kernelEventObserverTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

#if defined(HAVE_SYS_SELECT_H) || defined(_WIN32)