@@ -365,6 +365,7 @@ static void *
365365_testBRWalletManagerConnectThread (void * context ) {
366366 BRRunTestWalletManagerSyncThreadState * state = (BRRunTestWalletManagerSyncThreadState * ) context ;
367367 while (!state -> kill ) {
368+ nanosleep (& (struct timespec ){0 , 50000000 }, NULL );
368369 BRWalletManagerConnect (state -> manager );
369370 }
370371 return NULL ;
@@ -374,6 +375,7 @@ static void *
374375_testBRWalletManagerDisconnectThread (void * context ) {
375376 BRRunTestWalletManagerSyncThreadState * state = (BRRunTestWalletManagerSyncThreadState * ) context ;
376377 while (!state -> kill ) {
378+ nanosleep (& (struct timespec ){0 , 50000000 }, NULL );
377379 BRWalletManagerDisconnect (state -> manager );
378380 }
379381 return NULL ;
@@ -383,15 +385,27 @@ static void *
383385_testBRWalletManagerScanThread (void * context ) {
384386 BRRunTestWalletManagerSyncThreadState * state = (BRRunTestWalletManagerSyncThreadState * ) context ;
385387 while (!state -> kill ) {
388+ nanosleep (& (struct timespec ){0 , 50000000 }, NULL );
386389 BRWalletManagerScan (state -> manager );
387390 }
388391 return NULL ;
389392}
390393
394+ static void *
395+ _testBRWalletManagerNetworkThread (void * context ) {
396+ BRRunTestWalletManagerSyncThreadState * state = (BRRunTestWalletManagerSyncThreadState * ) context ;
397+ while (!state -> kill ) {
398+ nanosleep (& (struct timespec ){0 , 50000000 }, NULL );
399+ BRWalletManagerSetNetworkReachable (state -> manager , rand () % 2 );
400+ }
401+ return NULL ;
402+ }
403+
391404static void *
392405_testBRWalletManagerSwapThread (void * context ) {
393406 BRRunTestWalletManagerSyncThreadState * state = (BRRunTestWalletManagerSyncThreadState * ) context ;
394407 while (!state -> kill ) {
408+ nanosleep (& (struct timespec ){0 , 50000000 }, NULL );
395409 switch (BRWalletManagerGetMode (state -> manager )) {
396410 case SYNC_MODE_BRD_ONLY :
397411 BRWalletManagerSetMode (state -> manager , SYNC_MODE_P2P_ONLY );
@@ -943,11 +957,7 @@ BRRunTestWalletManagerSyncForMode (const char *testName,
943957 }
944958
945959 printf ("Testing BRWalletManager threading...\n" );
946- if (mode == SYNC_MODE_P2P_ONLY ) {
947- // TODO(fix): There is a thread-related issue in BRPeerManager/BRPeer where we have a use after free; re-enable once that is fixed
948- fprintf (stderr , "***WARNING*** %s:%d: BRWalletManager threading test is disabled for SYNC_MODE_P2P_ONLY\n" , testName , __LINE__ );
949-
950- } else {
960+ {
951961 // Test setup
952962 BRRunTestWalletManagerSyncState state = {0 };
953963 BRRunTestWalletManagerSyncTestSetup (& state , blockHeight , 1 );
@@ -956,22 +966,24 @@ BRRunTestWalletManagerSyncForMode (const char *testName,
956966 BRWalletManagerStart (manager );
957967
958968 BRRunTestWalletManagerSyncThreadState threadState = {0 , manager };
959- pthread_t connectThread = (pthread_t ) NULL , disconnectThread = (pthread_t ) NULL , scanThread = (pthread_t ) NULL ;
969+ pthread_t connectThread = (pthread_t ) NULL , disconnectThread = (pthread_t ) NULL , scanThread = (pthread_t ) NULL , networkThread = ( pthread_t ) NULL ;
960970
961971 success = (0 == pthread_create (& connectThread , NULL , _testBRWalletManagerConnectThread , (void * ) & threadState ) &&
962972 0 == pthread_create (& disconnectThread , NULL , _testBRWalletManagerDisconnectThread , (void * ) & threadState ) &&
963- 0 == pthread_create (& scanThread , NULL , _testBRWalletManagerScanThread , (void * ) & threadState ));
973+ 0 == pthread_create (& scanThread , NULL , _testBRWalletManagerScanThread , (void * ) & threadState ) &&
974+ 0 == pthread_create (& networkThread , NULL , _testBRWalletManagerNetworkThread , (void * ) & threadState ));
964975 if (!success ) {
965976 fprintf (stderr , "***FAILED*** %s:%d: pthread_creates failed\n" , testName , __LINE__ );
966977 return success ;
967978 }
968979
969- sleep (5 );
980+ sleep (10 );
970981
971982 threadState .kill = 1 ;
972983 success = (0 == pthread_join (connectThread , NULL ) &&
973984 0 == pthread_join (disconnectThread , NULL ) &&
974- 0 == pthread_join (scanThread , NULL ));
985+ 0 == pthread_join (scanThread , NULL ) &&
986+ 0 == pthread_join (networkThread , NULL ));
975987 if (!success ) {
976988 fprintf (stderr , "***FAILED*** %s:%d: pthread_joins failed\n" , testName , __LINE__ );
977989 return success ;
@@ -1139,11 +1151,7 @@ BRRunTestWalletManagerSyncAllModes (const char *testName,
11391151 }
11401152
11411153 printf ("Testing BRWalletManager mode swap threading...\n" );
1142- if (primaryMode == SYNC_MODE_P2P_ONLY || secondaryMode == SYNC_MODE_P2P_ONLY ) {
1143- // TODO(fix): There is a thread-related issue in BRPeerManager/BRPeer where we have a use after free; re-enable once that is fixed
1144- fprintf (stderr , "***WARNING*** %s:%d: BRWalletManager mode swap threading test is disabled\n" , testName , __LINE__ );
1145-
1146- } else {
1154+ {
11471155 // Test setup
11481156 BRRunTestWalletManagerSyncState state = {0 };
11491157 BRRunTestWalletManagerSyncTestSetup (& state , blockHeight , 1 );
@@ -1152,23 +1160,26 @@ BRRunTestWalletManagerSyncAllModes (const char *testName,
11521160 BRWalletManagerStart (manager );
11531161
11541162 BRRunTestWalletManagerSyncThreadState threadState = {0 , manager };
1155- pthread_t connectThread = (pthread_t ) NULL , disconnectThread = (pthread_t ) NULL , scanThread = (pthread_t ) NULL , swapThread = (pthread_t ) NULL ;
1163+ pthread_t connectThread = (pthread_t ) NULL , disconnectThread = (pthread_t ) NULL , scanThread = (pthread_t ) NULL ;
1164+ pthread_t networkThread = (pthread_t ) NULL , swapThread = (pthread_t ) NULL ;
11561165
11571166 success = (0 == pthread_create (& connectThread , NULL , _testBRWalletManagerConnectThread , (void * ) & threadState ) &&
11581167 0 == pthread_create (& disconnectThread , NULL , _testBRWalletManagerDisconnectThread , (void * ) & threadState ) &&
11591168 0 == pthread_create (& scanThread , NULL , _testBRWalletManagerScanThread , (void * ) & threadState ) &&
1169+ 0 == pthread_create (& networkThread , NULL , _testBRWalletManagerNetworkThread , (void * ) & threadState ) &&
11601170 0 == pthread_create (& swapThread , NULL , _testBRWalletManagerSwapThread , (void * ) & threadState ));
11611171 if (!success ) {
11621172 fprintf (stderr , "***FAILED*** %s:%d: pthread_creates failed\n" , testName , __LINE__ );
11631173 return success ;
11641174 }
11651175
1166- sleep (5 );
1176+ sleep (10 );
11671177
11681178 threadState .kill = 1 ;
11691179 success = (0 == pthread_join (connectThread , NULL ) &&
11701180 0 == pthread_join (disconnectThread , NULL ) &&
11711181 0 == pthread_join (scanThread , NULL ) &&
1182+ 0 == pthread_join (networkThread , NULL ) &&
11721183 0 == pthread_join (swapThread , NULL ));
11731184 if (!success ) {
11741185 fprintf (stderr , "***FAILED*** %s:%d: pthread_joins failed\n" , testName , __LINE__ );
@@ -1399,7 +1410,7 @@ BRRunTestWalletManagerFileService (const char *storagePath) {
13991410 BRTransactionFree (tx );
14001411 BRTransactionFree (tx2 );
14011412 BRSetFree (transactionSet );
1402-
1413+
14031414 ///
14041415 /// Peer
14051416 ///
@@ -1425,7 +1436,7 @@ BRRunTestWalletManagerFileService (const char *storagePath) {
14251436
14261437 free (p2 );
14271438 BRSetFree (peerSet );
1428-
1439+
14291440 fileServiceClose (fs );
14301441 fileServiceRelease (fs );
14311442
0 commit comments