Skip to content

Commit

Permalink
Implement frame test individually
Browse files Browse the repository at this point in the history
  • Loading branch information
cham-s committed Nov 3, 2024
1 parent a3bf095 commit fab4f26
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 33 deletions.
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.76.1"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.6.1"),
// .package(url: "https://github.com/swiftlang/swift-testing.git", from: "6.0.2"),
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.4.1"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump.git", from: "1.3.3"),
.package(url: "https://github.com/pointfreeco/swift-case-paths.git", from: "1.5.6"),
Expand Down Expand Up @@ -99,7 +98,6 @@ let package = Package(
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOPosix", package: "swift-nio"),
.product(name: "NIOWebSocket", package: "swift-nio"),
// .product(name: "Testing", package: "swift-testing"),
]
),
]
Expand Down
252 changes: 221 additions & 31 deletions Tests/AsyncWebSocketTests/AsyncWebSocketActorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,98 @@ class AsyncWebSocketClientTests {
}
}

// @Test(
// "Check sending a frame",
// .tags(.frame),
// .serialized,
// arguments: [
// AsyncWebSocketClient.Frame.ping(),
// .message(.binary("Hello".data(using: .utf8)!)),
// ]
// )
// func frame(frame: AsyncWebSocketClient.Frame) async throws {
// let webSocketActor = AsyncWebSocketClient.WebSocketActor()
// #expect(await webSocketActor.connections.count == 0)
// try #require(self.serverChannel != nil)
//
// let (host, port) = try #require(self.hostAndPort)
//
// let id = AsyncWebSocketClient.ID()
//
// let statuses = try await webSocketActor.open(
// settings: AsyncWebSocketClient.Settings(
// id: id,
// url: "ws://\(host)",
// port: port
// )
// )
//
// var statusIterator = statuses.makeAsyncIterator()
// #expect(await statusIterator.next() == .connecting)
// #expect(await statusIterator.next() == .connected)
//
// #expect(await webSocketActor.connections.count == 1)
//
// let connectionId = try #require(await webSocketActor.connections.keys.first)
// #expect(connectionId == id)
//
// let frameStream = try await webSocketActor.receive(id: id)
// var frameIterator = frameStream.makeAsyncIterator()
//
// let frameTask = Task {
// if frame.is(\.message.text) {
// try await webSocketActor.send(
// id: id,
// frame: .message(.text("Hello"))
// )
// } else if frame.is(\.message.binary) {
// try await webSocketActor.send(
// id: id,
// frame: .message(.binary("Hello".data(using: .utf8)!))
// )
// } else if frame.is(\.ping) {
// try await webSocketActor.send(
// id: id,
// frame: .ping()
// )
// }
// }
//
// await Task.yield()
// _ = try await frameTask.value
//
// let receivedFrame = await frameIterator.next()
//
// if frame.is(\.message.text) {
// #expect(receivedFrame == .message(.text("Hello")))
// } else if frame.is(\.message.binary) {
// #expect(receivedFrame == .message(.binary("Hello".data(using: .utf8)!)))
// } else if frame.is(\.ping) {
// #expect(receivedFrame == .pong())
// }
//
// let closeTask = Task {
// try await webSocketActor.send(
// id: id,
// frame: .close(code: .normalClosure)
// )
// }
//
// await Task.yield()
// _ = try await closeTask.value
//
// #expect(await statusIterator.next() == .didClose(.normalClosure))
//
// try await Task.sleep(for: .nanoseconds(500))
// #expect(await webSocketActor.connections.count == 0)
//
// }

@Test(
"Check sending a frame",
.tags(.frame),
.serialized,
arguments: [
AsyncWebSocketClient.Frame.ping(),
.message(.text("Hello")),
.message(.binary("Hello".data(using: .utf8)!)),
]
"Check text frame",
.tags(.frame)
)
func frame(frame: AsyncWebSocketClient.Frame) async throws {
func text() async throws {
let webSocketActor = AsyncWebSocketClient.WebSocketActor()
#expect(await webSocketActor.connections.count == 0)
try #require(self.serverChannel != nil)
Expand Down Expand Up @@ -198,37 +279,146 @@ class AsyncWebSocketClientTests {
var frameIterator = frameStream.makeAsyncIterator()

let frameTask = Task {
if frame.is(\.message.text) {
try await webSocketActor.send(
id: id,
frame: .message(.text("Hello"))
)
} else if frame.is(\.message.binary) {
try await webSocketActor.send(
id: id,
frame: .message(.binary("Hello".data(using: .utf8)!))
)
} else if frame.is(\.ping) {
try await webSocketActor.send(
id: id,
frame: .ping()
)
}
try await webSocketActor.send(
id: id,
frame: .message(.text("Hello"))
)
}

await Task.yield()
_ = try await frameTask.value

let receivedFrame = await frameIterator.next()

if frame.is(\.message.text) {
#expect(receivedFrame == .message(.text("Hello")))
} else if frame.is(\.message.binary) {
#expect(receivedFrame == .message(.binary("Hello".data(using: .utf8)!)))
} else if frame.is(\.ping) {
#expect(receivedFrame == .pong())
#expect(receivedFrame == .message(.text("Hello")))

let closeTask = Task {
try await webSocketActor.send(
id: id,
frame: .close(code: .normalClosure)
)
}

await Task.yield()
_ = try await closeTask.value

#expect(await statusIterator.next() == .didClose(.normalClosure))

try await Task.sleep(for: .nanoseconds(500))
#expect(await webSocketActor.connections.count == 0)
}

@Test(
"Check data frame",
.tags(.frame)
)

func data() async throws {
let webSocketActor = AsyncWebSocketClient.WebSocketActor()
#expect(await webSocketActor.connections.count == 0)
try #require(self.serverChannel != nil)

let (host, port) = try #require(self.hostAndPort)

let id = AsyncWebSocketClient.ID()

let statuses = try await webSocketActor.open(
settings: AsyncWebSocketClient.Settings(
id: id,
url: "ws://\(host)",
port: port
)
)

var statusIterator = statuses.makeAsyncIterator()
#expect(await statusIterator.next() == .connecting)
#expect(await statusIterator.next() == .connected)

#expect(await webSocketActor.connections.count == 1)

let connectionId = try #require(await webSocketActor.connections.keys.first)
#expect(connectionId == id)

let frameStream = try await webSocketActor.receive(id: id)
var frameIterator = frameStream.makeAsyncIterator()

let frameTask = Task {
try await webSocketActor.send(
id: id,
frame: .message(.binary("Hello".data(using: .utf8)!))
)
}

await Task.yield()
_ = try await frameTask.value

let receivedFrame = await frameIterator.next()

#expect(receivedFrame == .message(.binary("Hello".data(using: .utf8)!)))

let closeTask = Task {
try await webSocketActor.send(
id: id,
frame: .close(code: .normalClosure)
)
}

await Task.yield()
_ = try await closeTask.value

#expect(await statusIterator.next() == .didClose(.normalClosure))

try await Task.sleep(for: .nanoseconds(500))
#expect(await webSocketActor.connections.count == 0)
}

@Test(
"Check data frame",
.tags(.frame)
)
func ping() async throws {
let webSocketActor = AsyncWebSocketClient.WebSocketActor()
#expect(await webSocketActor.connections.count == 0)
try #require(self.serverChannel != nil)

let (host, port) = try #require(self.hostAndPort)

let id = AsyncWebSocketClient.ID()

let statuses = try await webSocketActor.open(
settings: AsyncWebSocketClient.Settings(
id: id,
url: "ws://\(host)",
port: port
)
)

var statusIterator = statuses.makeAsyncIterator()
#expect(await statusIterator.next() == .connecting)
#expect(await statusIterator.next() == .connected)

#expect(await webSocketActor.connections.count == 1)

let connectionId = try #require(await webSocketActor.connections.keys.first)
#expect(connectionId == id)

let frameStream = try await webSocketActor.receive(id: id)
var frameIterator = frameStream.makeAsyncIterator()

let frameTask = Task {
try await webSocketActor.send(
id: id,
frame: .ping()
)
}

await Task.yield()
_ = try await frameTask.value

let receivedFrame = await frameIterator.next()

#expect(receivedFrame == .pong())

let closeTask = Task {
try await webSocketActor.send(
id: id,
Expand Down

0 comments on commit fab4f26

Please sign in to comment.