You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.6 KiB

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