-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GWL-162] Trinet 소켓 Providable 추가 + 테스트 코드 추가 #177
Changes from 9 commits
07f208f
6e1198c
c3f7a7d
3a46cd0
b21259e
fbbb467
d2dcc85
fc4397f
8fdd9bc
8feb431
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} | ||
} |
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 받자마자 실행되는 녀석이군요...? 혹시 이유가 있을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. provider를 사용하는 레포지토리에서 이 Task를 어느 시점에 시작(resume)시켜야할지를 모르겠더라고요. 결국 소켓 통신하는 시점은 생성되는 시점일 것이 분명할 것이고, resume을 해주는 타이밍을 다른 객체가 관리하는 것도 애매하다고 판단해서 바로 실행시켜두었습니다. |
||
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 = "events", data: T) { | ||
self.event = event | ||
self.data = data | ||
} | ||
} |
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 | ||
} | ||
} |
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 {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sync
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK!