55// Created by Guilherme Souza on 23/10/23.
66//
77
8- @testable import Auth
98import ConcurrencyExtras
109import CustomDump
1110import Helpers
@@ -14,6 +13,8 @@ import TestHelpers
1413import XCTest
1514import XCTestDynamicOverlay
1615
16+ @testable import Auth
17+
1718final class SessionManagerTests : XCTestCase {
1819 var http : HTTPClientMock !
1920
@@ -42,6 +43,12 @@ final class SessionManagerTests: XCTestCase {
4243 )
4344 }
4445
46+ override func invokeTest( ) {
47+ withMainSerialExecutor {
48+ super. invokeTest ( )
49+ }
50+ }
51+
4552 func testSession_shouldFailWithSessionNotFound( ) async {
4653 do {
4754 _ = try await sut. session ( )
@@ -57,57 +64,53 @@ final class SessionManagerTests: XCTestCase {
5764 }
5865
5966 func testSession_shouldReturnValidSession( ) async throws {
60- try await withMainSerialExecutor {
61- let session = Session . validSession
62- Dependencies [ clientID] . sessionStorage. store ( session)
67+ let session = Session . validSession
68+ Dependencies [ clientID] . sessionStorage. store ( session)
6369
64- let returnedSession = try await sut. session ( )
65- expectNoDifference ( returnedSession, session)
66- }
70+ let returnedSession = try await sut. session ( )
71+ expectNoDifference ( returnedSession, session)
6772 }
6873
6974 func testSession_shouldRefreshSession_whenCurrentSessionExpired( ) async throws {
70- try await withMainSerialExecutor {
71- let currentSession = Session . expiredSession
72- Dependencies [ clientID] . sessionStorage. store ( currentSession)
73-
74- let validSession = Session . validSession
75-
76- let refreshSessionCallCount = LockIsolated ( 0 )
77-
78- let ( refreshSessionStream, refreshSessionContinuation) = AsyncStream< Session> . makeStream( )
79-
80- await http. when (
81- { $0. url. path. contains ( " /token " ) } ,
82- return: { _ in
83- refreshSessionCallCount. withValue { $0 += 1 }
84- let session = await refreshSessionStream. first ( where: { _ in true } ) !
85- return . stub( session)
86- }
87- )
88-
89- // Fire N tasks and call sut.session()
90- let tasks = ( 0 ..< 10 ) . map { _ in
91- Task { [ weak self] in
92- try await self ? . sut. session ( )
93- }
94- }
75+ let currentSession = Session . expiredSession
76+ Dependencies [ clientID] . sessionStorage. store ( currentSession)
9577
96- await Task . yield ( )
78+ let validSession = Session . validSession
9779
98- refreshSessionContinuation. yield ( validSession)
99- refreshSessionContinuation. finish ( )
80+ let refreshSessionCallCount = LockIsolated ( 0 )
10081
101- // Await for all tasks to complete.
102- var result : [ Result < Session ? , Error > ] = [ ]
103- for task in tasks {
104- let value = await task. result
105- result. append ( value)
82+ let ( refreshSessionStream, refreshSessionContinuation) = AsyncStream< Session> . makeStream( )
83+
84+ await http. when (
85+ { $0. url. path. contains ( " /token " ) } ,
86+ return: { _ in
87+ refreshSessionCallCount. withValue { $0 += 1 }
88+ let session = await refreshSessionStream. first ( where: { _ in true } ) !
89+ return . stub( session)
90+ }
91+ )
92+
93+ // Fire N tasks and call sut.session()
94+ let tasks = ( 0 ..< 10 ) . map { _ in
95+ Task { [ weak self] in
96+ try await self ? . sut. session ( )
10697 }
98+ }
99+
100+ await Task . yield ( )
107101
108- // Verify that refresher and storage was called only once.
109- XCTAssertEqual ( refreshSessionCallCount. value, 1 )
110- XCTAssertEqual ( try result. map { try $0. get ( ) } , ( 0 ..< 10 ) . map { _ in validSession } )
102+ refreshSessionContinuation. yield ( validSession)
103+ refreshSessionContinuation. finish ( )
104+
105+ // Await for all tasks to complete.
106+ var result : [ Result < Session ? , Error > ] = [ ]
107+ for task in tasks {
108+ let value = await task. result
109+ result. append ( value)
111110 }
111+
112+ // Verify that refresher and storage was called only once.
113+ XCTAssertEqual ( refreshSessionCallCount. value, 1 )
114+ XCTAssertEqual ( try result. map { try $0. get ( ) } , ( 0 ..< 10 ) . map { _ in validSession } )
112115 }
113116}
0 commit comments