Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Dec 2, 2024
1 parent 75a4c2c commit a7b9c39
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 57 deletions.
30 changes: 15 additions & 15 deletions Tests/RealtimeTests/RealtimeChannelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
// Created by Guilherme Souza on 09/09/24.
//

import Helpers
import InlineSnapshotTesting
import TestHelpers
import XCTest
import XCTestDynamicOverlay

@testable import Realtime

@MainActor
final class RealtimeChannelTests: XCTestCase {
let sut = RealtimeChannelV2(
topic: "topic",
Expand All @@ -19,7 +22,7 @@ final class RealtimeChannelTests: XCTestCase {
presence: PresenceJoinConfig(),
isPrivate: false
),
socket: .mock,
socket: .test(),
logger: nil
)

Expand Down Expand Up @@ -127,20 +130,17 @@ final class RealtimeChannelTests: XCTestCase {
}
}

extension Socket {
static var mock: Socket {
Socket(
broadcastURL: unimplemented(),
status: unimplemented(),
options: unimplemented(),
accessToken: unimplemented(),
apiKey: unimplemented(),
makeRef: unimplemented(),
connect: unimplemented(),
addChannel: unimplemented(),
removeChannel: unimplemented(),
push: unimplemented(),
httpSend: unimplemented()
extension RealtimeClientV2 {
static func test(
options: RealtimeClientOptions = RealtimeClientOptions(),
ws: any WebSocketClient = MockWebSocketClient(),
http: any HTTPClientType = HTTPClientMock()
) -> RealtimeClientV2 {
RealtimeClientV2(
url: URL(string: "http://localhost:54321/realtime")!,
options: options,
ws: ws,
http: http
)
}
}
13 changes: 7 additions & 6 deletions Tests/RealtimeTests/RealtimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import XCTest
import FoundationNetworking
#endif

@MainActor
final class RealtimeTests: XCTestCase {
let url = URL(string: "https://localhost:54321/realtime/v1")!
let apiKey = "anon.api.key"
Expand Down Expand Up @@ -79,10 +80,10 @@ final class RealtimeTests: XCTestCase {

XCTAssertEqual(socketStatuses.value, [.disconnected, .connecting, .connected])

let messageTask = sut.mutableState.messageTask
let messageTask = sut.messageTask
XCTAssertNotNil(messageTask)

let heartbeatTask = sut.mutableState.heartbeatTask
let heartbeatTask = sut.heartbeatTask
XCTAssertNotNil(heartbeatTask)

let channelStatuses = LockIsolated([RealtimeChannelStatus]())
Expand Down Expand Up @@ -202,7 +203,7 @@ final class RealtimeTests: XCTestCase {
},
{
"event" : "phx_join",
"join_ref" : "2",
"join_ref" : "3",
"payload" : {
"access_token" : "custom.access.token",
"config" : {
Expand All @@ -219,7 +220,7 @@ final class RealtimeTests: XCTestCase {
"private" : false
}
},
"ref" : "2",
"ref" : "3",
"topic" : "realtime:public:messages"
}
]
Expand Down Expand Up @@ -279,7 +280,7 @@ final class RealtimeTests: XCTestCase {

await fulfillment(of: [sentHeartbeatExpectation], timeout: 2)

let pendingHeartbeatRef = sut.mutableState.pendingHeartbeatRef
let pendingHeartbeatRef = sut.pendingHeartbeatRef
XCTAssertNotNil(pendingHeartbeatRef)

// Wait until next heartbeat
Expand All @@ -304,7 +305,7 @@ final class RealtimeTests: XCTestCase {
await http.when {
$0.url.path.hasSuffix("broadcast")
} return: { _ in
HTTPResponse(
await HTTPResponse(
data: "{}".data(using: .utf8)!,
response: HTTPURLResponse(
url: self.sut.broadcastURL,
Expand Down
73 changes: 37 additions & 36 deletions Tests/RealtimeTests/_PushTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
//

import ConcurrencyExtras
@testable import Realtime
import TestHelpers
import XCTest

@testable import Realtime

@MainActor
final class _PushTests: XCTestCase {
var ws: MockWebSocketClient!
var socket: RealtimeClientV2!
Expand All @@ -24,13 +26,11 @@ final class _PushTests: XCTestCase {
super.setUp()

ws = MockWebSocketClient()
socket = RealtimeClientV2(
url: URL(string: "https://localhost:54321/v1/realtime")!,
socket = RealtimeClientV2.test(
options: RealtimeClientOptions(
headers: ["apiKey": "apikey"]
),
ws: ws,
http: HTTPClientMock()
ws: ws
)
}

Expand All @@ -42,7 +42,7 @@ final class _PushTests: XCTestCase {
presence: .init(),
isPrivate: false
),
socket: Socket(client: socket),
socket: socket,
logger: nil
)
let push = PushV2(
Expand All @@ -61,34 +61,35 @@ final class _PushTests: XCTestCase {
}

// FIXME: Flaky test, it fails some time due the task scheduling, even tho we're using withMainSerialExecutor.
// func testPushWithAck() async {
// let channel = RealtimeChannelV2(
// topic: "realtime:users",
// config: RealtimeChannelConfig(
// broadcast: .init(acknowledgeBroadcasts: true),
// presence: .init()
// ),
// socket: socket,
// logger: nil
// )
// let push = PushV2(
// channel: channel,
// message: RealtimeMessageV2(
// joinRef: nil,
// ref: "1",
// topic: "realtime:users",
// event: "broadcast",
// payload: [:]
// )
// )
//
// let task = Task {
// await push.send()
// }
// await Task.megaYield()
// await push.didReceive(status: .ok)
//
// let status = await task.value
// XCTAssertEqual(status, .ok)
// }
// func testPushWithAck() async {
// let channel = RealtimeChannelV2(
// topic: "realtime:users",
// config: RealtimeChannelConfig(
// broadcast: .init(acknowledgeBroadcasts: true),
// presence: .init(),
// isPrivate: false
// ),
// socket: socket,
// logger: nil
// )
// let push = PushV2(
// channel: channel,
// message: RealtimeMessageV2(
// joinRef: nil,
// ref: "1",
// topic: "realtime:users",
// event: "broadcast",
// payload: [:]
// )
// )
//
// let task = Task {
// await push.send()
// }
// await Task.yield()
// push.didReceive(status: .ok)
//
// let status = await task.value
// XCTAssertEqual(status, .ok)
// }
}

0 comments on commit a7b9c39

Please sign in to comment.