开发者

How to unit test a peer to peer GameKit application?

I would like to build a unit test with GameKit framework. Actually I've implemented a unit test that create two threads. One for the server and one for the client.

Here the code of the main method of the server thread :

@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;
}

The same code is for the client but with sessionMode = GKSessionModeClient.

Unfortunately, the client never connects to the server ! Here the log :

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

Here, the unit test method :

- (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];
  serv开发者_开发百科erThread = nil;
}

Here, the SessionManager initializer :

- (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;
}

The GKSession's delegate 'didChangeState' method is never called on the server AND on the client.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜