Browse Source

Added basic Sync' thread/queue/callback test, fixed PEPqueue that wasn't using NSCondition in a proper way, fixed possible race condition in sync thread.

IOSAD-21
Edouard Tisserant 5 years ago
parent
commit
262e05068c
4 changed files with 123 additions and 57 deletions
  1. +19
    -14
      pEpObjCAdapter/PEPObjCAdapter.m
  2. +0
    -2
      pEpObjCAdapter/PEPQueue.h
  3. +26
    -40
      pEpObjCAdapter/PEPQueue.m
  4. +78
    -1
      pEpObjCTests/iOSTests.m

+ 19
- 14
pEpObjCAdapter/PEPObjCAdapter.m View File

@ -305,26 +305,15 @@ static id <PEPSyncDelegate> syncDelegate = nil;
{
[syncThreadJoinCond lock];
PEP_STATUS status = init(&sync_session);
if (status != PEP_STATUS_OK) {
return;
}
register_sync_callbacks(sync_session,
/* "management" : queuing (unused) */
"NOTNULL",
message_to_send,
notify_handshake,
inject_sync_msg,
retrieve_next_sync_msg);
PEP_STATUS status;
status = do_sync_protocol(sync_session,
/* "object" : notifying, sending (unused) */
"NOTNULL");
// TODO : detach all attached sessions
// TODO : log something if status not as expected
release(sync_session);
[syncThreadJoinCond unlockWithCondition:YES];
}
@ -349,6 +338,19 @@ static id <PEPSyncDelegate> syncDelegate = nil;
syncQueue = [[PEPQueue alloc]init];
syncThreadJoinCond = [[NSConditionLock alloc] initWithCondition:NO];
PEP_STATUS status = init(&sync_session);
if (status != PEP_STATUS_OK) {
return;
}
register_sync_callbacks(sync_session,
/* "management" : queuing (unused) */
"NOTNULL",
message_to_send,
notify_handshake,
inject_sync_msg,
retrieve_next_sync_msg);
syncThread = [[NSThread alloc]
initWithTarget:self
@ -392,7 +394,10 @@ static id <PEPSyncDelegate> syncDelegate = nil;
[syncThreadJoinCond lockWhenCondition:YES];
[syncThreadJoinCond unlock];
release(sync_session);
sync_session = NULL;
syncThread = nil;
syncQueue = nil;
syncThreadJoinCond = nil;


+ 0
- 2
pEpObjCAdapter/PEPQueue.h View File

@ -22,6 +22,4 @@ typedef void (^deleteOp)(id);
- (void)purge:(deleteOp)del;
- (NSUInteger)count;
@end

+ 26
- 40
pEpObjCAdapter/PEPQueue.m View File

@ -33,23 +33,15 @@
- (void)enqueue:(id)object
{
@synchronized(self) {
if (_queue)
[_queue insertObject:object atIndex:0];
}
[_cond lock];
if (_queue)
[_queue insertObject:object atIndex:0];
[_cond signal];
}
- (BOOL)condwait
{
BOOL res;
@synchronized(self) {
res = _queue && _queue.count == 0;
}
return res;
[_cond unlock];
}
- (id)timedDequeue:(time_t*)timeout
@ -58,7 +50,7 @@
[_cond lock];
while ([self condwait])
while (_queue && _queue.count == 0)
{
if (*timeout == 0)
{
@ -79,14 +71,13 @@
}
}
@synchronized(self) {
if (_queue)
{
tmp = [_queue lastObject];
[_queue removeLastObject];
}
if (_queue)
{
tmp = [_queue lastObject];
[_queue removeLastObject];
}
[_cond unlock];
return tmp;
@ -100,35 +91,30 @@
- (void)kill
{
@synchronized(self) {
_queue = nil;
}
[_cond lock];
_queue = nil;
[_cond signal];
[_cond unlock];
}
- (void)purge:(deleteOp)del
{
@synchronized(self) {
id item;
for (item in _queue)
{
del(item);
}
_queue = nil;
[_cond lock];
id item;
for (item in _queue)
{
del(item);
}
_queue = nil;
[_cond signal];
[_cond unlock];
}
- (NSUInteger)count
{
NSUInteger res;
@synchronized(self) {
res = [_queue count];
}
return res;
}
- (void)dealloc
{


+ 78
- 1
pEpObjCTests/iOSTests.m View File

@ -11,6 +11,56 @@
#import "PEPObjCAdapter.h"
#import "PEPSession.h"
@interface someSyncDelegate : NSObject <PEPSyncDelegate>
- (bool)waitUntilSent:(time_t)maxSec;
@property (nonatomic) bool sendWasCalled;
@property (nonatomic, strong) NSCondition *cond;
@end
@implementation someSyncDelegate
- (id)init {
if (self = [super init]) {
self.sendWasCalled = false;
self.cond = [[NSCondition alloc] init];
}
return self;
}
- (PEP_STATUS)notifyHandshakeWithSignal:(sync_handshake_signal)signal me:(id)me partner:(id)partner {
return PEP_STATUS_OK;
}
- (PEP_STATUS)sendMessage:(id)msg {
[_cond lock];
_sendWasCalled = true;
[_cond signal];
[_cond unlock];
return PEP_STATUS_OK;
}
- (PEP_STATUS)fastPolling:(bool)isfast {
return PEP_STATUS_OK;
}
- (bool)waitUntilSent:(time_t)maxSec {
bool res;
[_cond lock];
[_cond waitUntilDate:[NSDate dateWithTimeIntervalSinceNow: 2]];
res = _sendWasCalled;
[_cond unlock];
return res;
}
@end
@interface iOSTests : XCTestCase
@end
@ -153,7 +203,7 @@ PEPSession *session;
[self pEpSetUp];
PEPSession *session2 = [[PEPSession alloc] init];
sleep(1);
session2 = nil;
[self pEpCleanUp];
@ -195,6 +245,33 @@ PEPSession *session;
}
- (void)testSyncSession {
someSyncDelegate *syncDelegate = [[someSyncDelegate alloc] init];
[self pEpSetUp];
// This should attach session just created
[PEPObjCAdapter startSync:syncDelegate];
NSMutableDictionary *identMe = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"pEp Test iOS GenKey", @"username",
@"pep.test.iosgenkey@pep-project.org", @"address",
@"Me", @"user_id",
nil];
[session mySelf:identMe];
bool res = [syncDelegate waitUntilSent:2];
XCTAssert(res);
// This should detach session just created
[PEPObjCAdapter stopSync];
[self pEpCleanUp];
}
- (void)testTrustWords {
[self pEpSetUp];


Loading…
Cancel
Save