Я хотел бы построить модульный тест с каркасом GameKit.
На самом деле я реализовал модульный тест, который создает два потока.
Один для сервера и один для клиента.
Вот код основного метода серверного потока:
@implementation ServerThread
- (void)main {
session = [[SessionManager alloc] initWithSessionId:@"TEST"
displayName:@"Server"
sessionMode:GKSessionModeServer];
session.delegate = self;
running = YES;
while (running) {
NSLog(@"Server running");
[NSThread sleepForTimeInterval:1.0];
}
[session release];
session = nil;
}
Тот же код для клиента, но с sessionMode = GKSessionModeClient.
К сожалению, клиент никогда не подключается к серверу!
Вот журнал:
2011-03-21 19:48:04.630 otest[41695:2f03] Server ready, peerId=352270827
2011-03-21 19:48:04.631 otest[41695:2f03] Server running
2011-03-21 19:48:04.651 otest[41695:2e07] Client ready, peerId=1781598997
2011-03-21 19:48:04.652 otest[41695:2e07] Client running
2011-03-21 19:48:05.633 otest[41695:2f03] Server running
2011-03-21 19:48:05.653 otest[41695:2e07] Client running
2011-03-21 19:48:06.633 otest[41695:2f03] Server running
2011-03-21 19:48:06.654 otest[41695:2e07] Client running
2011-03-21 19:48:07.634 otest[41695:2f03] Server running
2011-03-21 19:48:07.655 otest[41695:2e07] Client running
2011-03-21 19:48:08.635 otest[41695:2f03] Server running
2011-03-21 19:48:08.656 otest[41695:2e07] Client running
Здесь метод модульного тестирования:
- (void)test {
serverThread = [[ServerThread alloc] init];
clientThread = [[ClientThread alloc] init];
[serverThread start];
[clientThread start];
// The main thread must wait the end
while ([serverThread isExecuting] || [clientThread isExecuting]) {
[NSThread sleepForTimeInterval:1.0];
}
[clientThread release];
clientThread = nil;
[serverThread release];
serverThread = nil;
}
Здесь инициализатор SessionManager:
- (id)initWithSessionId:(NSString *)aSessionId
displayName:(NSString *)aDisplayName
sessionMode:(GKSessionMode)aSessionMode {
if ((self = [super init])) {
peers = [[NSMutableArray alloc] init];
// Peers need to have the same sessionID set on their GKSession to see each other.
sessionId = [aSessionId retain];
displayName = [aDisplayName retain];
sessionMode = aSessionMode;
gkSession = [[GKSession alloc] initWithSessionID:sessionId
displayName:displayName
sessionMode:sessionMode];
gkSession.delegate = self;
[gkSession setDataReceiveHandler:self
withContext:nil];
gkSession.available = YES;
switch (sessionMode) {
case GKSessionModeServer:
NSLog(@"Server ready, peerId=%@", gkSession.peerID);
break;
case GKSessionModeClient:
NSLog(@"Client ready, peerId=%@", gkSession.peerID);
break;
case GKSessionModePeer:
NSLog(@"Peer ready, peerId=%@", gkSession.peerID);
break;
}
}
return self;
}
Метод делегата GKSession 'didChangeState' никогда не вызывается на сервере И на клиенте.