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