Skip to content

Commit

Permalink
[M, F] updated some naming + new unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinz1911 committed Apr 13, 2023
1 parent 1bc0e9c commit 517c110
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# FusionKit

`FusionKit` is a library which implements the `FNConnection-Protocol`. The `FNConnection-Protocol` is proprietary networking protocol which uses a small and lightweight header with a performance as fast as raw tcp performance. Built directly on top of Apples `Network.framework` with support for plain tcp and tls encrypted connections. The implementation for the host is [Network](https://github.com/Vinz1911/network) written in golang with awesome concurrency support to ensure maximum performance.
`FusionKit` is a library which implements the `Fusion Framing Protocol (FFP)`. The `Fusion Framing Protocol (FFP)` is proprietary networking protocol which uses a small and lightweight header with a performance as fast as raw tcp performance. Built directly on top of Apples `Network.framework` with support for plain tcp and tls encrypted connections. The implementation for the host is [Fusion](https://github.com/Vinz1911/fusion) written in golang with awesome concurrency support to ensure maximum performance.

## License:
[![License](https://img.shields.io/badge/license-GPLv3-blue.svg?longCache=true&style=flat)](https://github.com/Vinz1911/FusionKit/blob/main/LICENSE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// NetworkFrameProtocol.swift
// FNConnectionFrameProtocol.swift
// FusionKit
//
// Created by Vinzenz Weist on 07.06.21.
Expand Down
48 changes: 38 additions & 10 deletions Tests/FusionKitTests/FusionKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FusionKitTests: XCTestCase {
private var connection = FNConnection(host: "127.0.0.1", port: 7878)
private var buffer = "50000"
private let timeout = 10.0
private var cases: TestCase? = nil
private let uuid = UUID().uuidString
private var exp: XCTestExpectation?

/// set up
Expand All @@ -28,40 +28,68 @@ class FusionKitTests: XCTestCase {

/// start test sending single text message
func testTextMessage() {
cases = .string; start()
start(test: .string)
}

/// start test sending single binary message
func testBinaryMessage() {
cases = .data; start()
start(test: .data)
}

/// start test sending single ping message
func testPingMessage() {
cases = .ping; start()
start(test: .ping)
}

/// start test creating and parsing string based message
func testParsingStringMessage() {
let message = uuid
framer(message: message)
}

/// start test creating and parsing data based message
func testParsingDataMessage() {
guard let message = uuid.data(using: .utf8) else { return }
framer(message: message)
}
}

// MARK: - Private API Extension -

private extension FusionKitTests {
/// create a connection and start
private func start() {
stateUpdateHandler(connection: connection)
/// - Parameter test: test case
private func start(test: TestCase) {
stateUpdateHandler(connection: connection, test: test)
connection.start()
wait(for: [exp!], timeout: timeout)
}

/// message framer
private func framer<T: FNConnectionMessage>(message: T) {
let framer = FNConnectionFrame()
let message = framer.create(message: message)
if let error = message.error { XCTFail("failed with error: \(error)") }
guard let data = message.data else { XCTFail("failed to get message data"); return }

framer.parse(data: data) { message, error in
if case let message as String = message { XCTAssertEqual(message, uuid); self.exp?.fulfill() }
if case let message as Data = message { XCTAssertEqual(message, uuid.data(using: .utf8)); self.exp?.fulfill() }
if let error = error { XCTFail("failed with error: \(error)") }
}
wait(for: [exp!], timeout: timeout)
}

/// state update handler for connection
/// - Parameter connection: instance of 'NetworkConnection'
private func stateUpdateHandler(connection: FNConnection) {
private func stateUpdateHandler(connection: FNConnection, test: TestCase) {
connection.stateUpdateHandler = { [weak self] state in
guard let self = self else { return }
switch state {
case .ready:
if self.cases == .string { connection.send(message: self.buffer) }
if self.cases == .data { connection.send(message: Data(count: Int(self.buffer)!)) }
if self.cases == .ping { connection.send(message: UInt16(self.buffer)!) }
if test == .string { connection.send(message: self.buffer) }
if test == .data { connection.send(message: Data(count: Int(self.buffer)!)) }
if test == .ping { connection.send(message: UInt16(self.buffer)!) }

case .message(let message):
if case let message as UInt16 = message {
Expand Down

0 comments on commit 517c110

Please sign in to comment.