pEpObjCTests/PEPSessionProviderTest.m
author Dirk Zimmermann <dz@pep.security>
Fri, 06 Sep 2019 11:03:27 +0200
branchIOS-1784
changeset 1225 6d89f5f624db
parent 738 7ac89df4a502
permissions -rw-r--r--
IOS-1784 Separate concerns.
     1 //
     2 //  PEPSessionProviderTest.m
     3 //  pEpObjCAdapterTests
     4 //
     5 //  Created by Andreas Buff on 17.01.18.
     6 //  Copyright © 2018 p≡p. All rights reserved.
     7 //
     8 
     9 #import <XCTest/XCTest.h>
    10 
    11 #import "PEPSession.h"
    12 #import "PEPInternalSession.h"
    13 #import "PEPSessionProvider.h"
    14 
    15 @interface PEPSessionProviderTest : XCTestCase
    16 
    17 @end
    18 
    19 @implementation PEPSessionProviderTest
    20 
    21 - (void)tearDown
    22 {
    23     [PEPSession cleanup];
    24 }
    25 
    26 - (void)testSeperatedSessionPerThread {
    27     // Get main session
    28     PEPInternalSession *sessionMain = [PEPSessionProvider session];
    29     __block PEPInternalSession *sessionBackground = nil;
    30     XCTestExpectation *exp = [self expectationWithDescription:@"background session created"];
    31 
    32     // Get background session
    33     dispatch_queue_t backgroundQueue = dispatch_queue_create("PEPSessionProviderTest.peptest1", 0);
    34     dispatch_async(backgroundQueue, ^{
    35         sessionBackground = [PEPSessionProvider session];
    36         [exp fulfill];
    37     });
    38 
    39     [self waitForExpectationsWithTimeout:1.0 handler:^(NSError * _Nullable error) {
    40         if (error) { XCTFail(@"timeout: %@", error); }
    41     }];
    42     XCTAssertNotNil(sessionMain);
    43     XCTAssertNotNil(sessionBackground);
    44 
    45     // Make sure we have seperated sessions
    46     XCTAssertNotEqual(sessionBackground, sessionMain,
    47                       @"We should have seperated sessions, one per thread");
    48 }
    49 
    50 - (void)testMainSessionDoesNotChange {
    51     // Get main session
    52     PEPInternalSession *sessionMain = [PEPSessionProvider session];
    53     __block PEPInternalSession *sessionBackground = nil;
    54     XCTestExpectation *exp = [self expectationWithDescription:@"background session created"];
    55 
    56     // Get background session
    57     dispatch_queue_t backgroundQueue = dispatch_queue_create("PEPSessionProviderTest.peptest1", 0);
    58     dispatch_async(backgroundQueue, ^{
    59         sessionBackground = [PEPSessionProvider session];
    60         [exp fulfill];
    61     });
    62 
    63     [self waitForExpectationsWithTimeout:1.0 handler:^(NSError * _Nullable error) {
    64         if (error) { XCTFail(@"timeout: %@", error); }
    65     }];
    66     // Get main session again
    67     PEPInternalSession *sessionMain2 = [PEPSessionProvider session];
    68     XCTAssertNotNil(sessionMain);
    69     XCTAssertNotNil(sessionMain2);
    70     XCTAssertNotNil(sessionBackground);
    71     XCTAssertEqual(sessionMain, sessionMain2, @"The main session stayed the same (was kept \
    72                    alive, was not recreated)");
    73 }
    74 
    75 - (void)testNewMainSessionAfterCleanup {
    76     // Get main session
    77     PEPInternalSession *sessionMain = [PEPSessionProvider session];
    78     __block PEPInternalSession *sessionBackground = nil;
    79     XCTestExpectation *exp = [self expectationWithDescription:@"background session created"];
    80 
    81     // Get background session
    82     dispatch_queue_t backgroundQueue = dispatch_queue_create("PEPSessionProviderTest.peptest1", 0);
    83     dispatch_async(backgroundQueue, ^{
    84         sessionBackground = [PEPSessionProvider session];
    85         [exp fulfill];
    86     });
    87 
    88     [self waitForExpectationsWithTimeout:1.0 handler:^(NSError * _Nullable error) {
    89         if (error) { XCTFail(@"timeout: %@", error); }
    90     }];
    91     // Get main session again
    92     PEPInternalSession *sessionMain2 = [PEPSessionProvider session];
    93     XCTAssertNotNil(sessionMain);
    94     XCTAssertNotNil(sessionMain2);
    95     XCTAssertNotNil(sessionBackground);
    96     XCTAssertEqual(sessionMain, sessionMain2, @"The main session stayed the same (was kept \
    97                    alive, was not recreated)");
    98     [PEPSession cleanup];
    99     PEPInternalSession *sessionMainAfterCleanup = [PEPSessionProvider session];
   100     XCTAssertNotNil(sessionMainAfterCleanup);
   101     XCTAssertNotEqual(sessionMainAfterCleanup, sessionMain,
   102                       @"We got a new main session after cleanup");
   103 }
   104 
   105 @end
   106