IOSAD-141 Try to provoke it. IOSAD-141
authorDirk Zimmermann <dz@pep.security>
Thu, 12 Sep 2019 12:59:20 +0200
branchIOSAD-141
changeset 123738d1cefd5ee3
parent 1236 123cfc5f76b6
child 1238 9f82c0b1c722
IOSAD-141 Try to provoke it.
pEpObjCTests/PEPSessionTest.m
     1.1 --- a/pEpObjCTests/PEPSessionTest.m	Thu Sep 12 12:58:59 2019 +0200
     1.2 +++ b/pEpObjCTests/PEPSessionTest.m	Thu Sep 12 12:59:20 2019 +0200
     1.3 @@ -1426,6 +1426,86 @@
     1.4      }
     1.5  }
     1.6  
     1.7 +/**
     1.8 + Try to provoke sqlite lock contention by using the engine in parallel from several threads.
     1.9 +
    1.10 + See IOSAD-141.
    1.11 + */
    1.12 +- (void)testTryToProvokeContentienIssues
    1.13 +{
    1.14 +    PEPSession *sessionMain = [PEPSession new];
    1.15 +
    1.16 +    PEPIdentity *identMe = [[PEPIdentity alloc]
    1.17 +                            initWithAddress:@"me-myself-and-i@pep-project.org"
    1.18 +                            userID:@"me-myself-and-i"
    1.19 +                            userName:@"pEp Me"
    1.20 +                            isOwn:YES];
    1.21 +    NSError *error = nil;
    1.22 +    XCTAssertTrue([sessionMain mySelf:identMe error:&error]);
    1.23 +    XCTAssertNil(error);
    1.24 +
    1.25 +    PEPIdentity *aliceIdent = [self checkImportingKeyFilePath:@"6FF00E97_sec.asc"
    1.26 +                                                      address:@"pep.test.alice@pep-project.org"
    1.27 +                                                       userID:@"This Is Alice"
    1.28 +                                                  fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
    1.29 +                                                      session: nil];
    1.30 +    XCTAssertNotNil(aliceIdent);
    1.31 +
    1.32 +    PEPIdentity *bobIdent = [self checkImportingKeyFilePath:@"0xC9C2EE39.asc"
    1.33 +                                                    address:@"pep.test.bob@pep-project.org"
    1.34 +                                                     userID:@"This Is Bob"
    1.35 +                                                fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
    1.36 +                                                    session: nil];
    1.37 +    XCTAssertNotNil(bobIdent);
    1.38 +
    1.39 +    for (int iOuter = 10; iOuter < 100; ++iOuter) {
    1.40 +        XCTestExpectation *expThread1Finished = [self expectationWithDescription:@"Thread1 finished"];
    1.41 +        NSThread *thread1 = [[NSThread alloc] initWithBlock:^{
    1.42 +            PEPSession *session1 = [PEPSession new];
    1.43 +            NSError *error1 = nil;
    1.44 +
    1.45 +            PEPMessage *msg = [PEPMessage new];
    1.46 +            msg.to = @[aliceIdent, bobIdent];
    1.47 +            msg.direction = PEPMsgDirectionOutgoing;
    1.48 +            msg.shortMessage = @"Subject";
    1.49 +            msg.longMessage = @"Just some message";
    1.50 +
    1.51 +            for (int i = 0; i < 100; ++i) {
    1.52 +                NSLog(@"outgoingRatingForMessage %d\n", i);
    1.53 +                NSError *error = nil;
    1.54 +                NSNumber *num = [session1 outgoingRatingForMessage:msg error:&error1];
    1.55 +                XCTAssertNotNil(num);
    1.56 +                XCTAssertNil(error);
    1.57 +            }
    1.58 +            [expThread1Finished fulfill];
    1.59 +        }];
    1.60 +
    1.61 +        XCTestExpectation *expThread2Finished = [self expectationWithDescription:@"Thread2 finished"];
    1.62 +        NSThread *thread2 = [[NSThread alloc] initWithBlock:^{
    1.63 +            PEPSession *session2 = [PEPSession new];
    1.64 +            NSError *error2 = nil;
    1.65 +
    1.66 +            for (int i = 0; i < 2000; ++i) {
    1.67 +                NSLog(@"getTrustwordsIdentity1 %d\n", i);
    1.68 +                NSString *trustwords = [session2
    1.69 +                                        getTrustwordsIdentity1:aliceIdent
    1.70 +                                        identity2:bobIdent
    1.71 +                                        language:@"en"
    1.72 +                                        full:YES
    1.73 +                                        error:&error2];
    1.74 +                XCTAssertNotNil(trustwords);
    1.75 +                XCTAssertNil(error);
    1.76 +            }
    1.77 +            [expThread2Finished fulfill];
    1.78 +        }];
    1.79 +
    1.80 +        [thread1 start];
    1.81 +        [thread2 start];
    1.82 +
    1.83 +        [self waitForExpectations:@[expThread1Finished, expThread2Finished] timeout:20];
    1.84 +    }
    1.85 +}
    1.86 +
    1.87  #pragma mark - Helpers
    1.88  
    1.89  - (void)testSendMessageOnSession:(PEPSession *)session