-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GWL-162] Trinet 소켓 Providable 추가 + 테스트 코드 추가 (#177)
* add: WebSocketTaskProtocol 추가 - WebSocketTask의 모든 사용을 방지하고, 오로지 send, receive만 처리할 수 있도록 추상화했습니다. * add: URLSessionWebSocketProtocol 추가 - URLSession에서 사용하는 WebSocketTask 메서드를 하나의 프로토콜로 추상화했습니다. - Mocking하기 위함입니다. * feat: TNSocketProvider 구현 * feat: WebSocketFrame 추가 및 send에 래핑 * add: SocketURL xcconfig 코드 추가 * add: Add comments * feat: 테스트 샘플 코드 추가 * delete: 쓰지 않는 import문 삭제 * test: MockWebSocketSession 테스트 코드 추가 * chore: WebSocketFrame event 값 수정 - workout_session으로 변경 (서버 api 변경)
- Loading branch information
Showing
9 changed files
with
378 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
iOS/Projects/Core/Network/Sources/MockWebSocketSession.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// MockWebSocketSession.swift | ||
// Trinet | ||
// | ||
// Created by 홍승현 on 11/29/23. | ||
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - MockWebSocketTask | ||
|
||
public final class MockWebSocketTask: WebSocketTaskProtocol { | ||
private var sentMessage: URLSessionWebSocketTask.Message? | ||
private var receiveContinuation: CheckedContinuation<URLSessionWebSocketTask.Message, Never>? | ||
|
||
public func send(_ message: URLSessionWebSocketTask.Message) async throws { | ||
sentMessage = message | ||
receiveContinuation?.resume(returning: message) | ||
} | ||
|
||
public func receive() async throws -> URLSessionWebSocketTask.Message { | ||
if let receivedMessage = sentMessage { | ||
sentMessage = nil | ||
return receivedMessage | ||
} | ||
|
||
return await withCheckedContinuation { continuation in | ||
receiveContinuation = continuation | ||
} | ||
} | ||
|
||
public func resume() {} | ||
} | ||
|
||
// MARK: - MockWebSocketSession | ||
|
||
public struct MockWebSocketSession: URLSessionWebSocketProtocol { | ||
var webSocketTask: MockWebSocketTask = .init() | ||
|
||
public func webSocketTask(with _: URLRequest) -> WebSocketTaskProtocol { | ||
return webSocketTask | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// TNSocketProvider.swift | ||
// Trinet | ||
// | ||
// Created by 홍승현 on 11/29/23. | ||
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - TNSocketProvidable | ||
|
||
public protocol TNSocketProvidable { | ||
func send<Model: Codable>(model: Model) async throws | ||
func receive() async throws -> URLSessionWebSocketTask.Message? | ||
} | ||
|
||
// MARK: - TNSocketProvider | ||
|
||
public struct TNSocketProvider<EndPoint: TNEndPoint>: TNSocketProvidable { | ||
private let session: URLSessionWebSocketProtocol | ||
private var task: WebSocketTaskProtocol? | ||
private let jsonEncoder: JSONEncoder = .init() | ||
|
||
public init(session: URLSessionWebSocketProtocol = URLSession.shared, endPoint: EndPoint) { | ||
self.session = session | ||
task = try? session.webSocketTask(with: endPoint.request()) | ||
task?.resume() | ||
} | ||
|
||
public func send(model: some Codable) async throws { | ||
let frame = WebSocketFrame(data: model) | ||
try await task?.send(.data(jsonEncoder.encode(frame))) | ||
} | ||
|
||
public func receive() async throws -> URLSessionWebSocketTask.Message? { | ||
return try await task?.receive() | ||
} | ||
} | ||
|
||
// MARK: - WebSocketFrame | ||
|
||
struct WebSocketFrame<T: Codable>: Codable { | ||
let event: String | ||
let data: T | ||
|
||
init(event: String = "workout_session", data: T) { | ||
self.event = event | ||
self.data = data | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
iOS/Projects/Core/Network/Sources/URLSessionWebSocketProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// URLSessionWebSocketProtocol.swift | ||
// Trinet | ||
// | ||
// Created by 홍승현 on 11/29/23. | ||
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - URLSessionWebSocketProtocol | ||
|
||
public protocol URLSessionWebSocketProtocol { | ||
func webSocketTask(with request: URLRequest) -> WebSocketTaskProtocol | ||
} | ||
|
||
// MARK: - URLSession + URLSessionWebSocketProtocol | ||
|
||
extension URLSession: URLSessionWebSocketProtocol { | ||
public func webSocketTask(with request: URLRequest) -> WebSocketTaskProtocol { | ||
let socketTask: URLSessionWebSocketTask = webSocketTask(with: request) | ||
return socketTask | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
iOS/Projects/Core/Network/Sources/WebSocketTaskProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// WebSocketTaskProtocol.swift | ||
// Trinet | ||
// | ||
// Created by 홍승현 on 11/29/23. | ||
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - WebSocketTaskProtocol | ||
|
||
public protocol WebSocketTaskProtocol { | ||
func send(_ message: URLSessionWebSocketTask.Message) async throws | ||
|
||
func receive() async throws -> URLSessionWebSocketTask.Message | ||
|
||
func resume() | ||
} | ||
|
||
// MARK: - URLSessionWebSocketTask + WebSocketTaskProtocol | ||
|
||
extension URLSessionWebSocketTask: WebSocketTaskProtocol {} |
Oops, something went wrong.