<commit_before>//
//  Thali CordovaPlugin
//  TCPClientTests.swift
//
//  Copyright (C) Microsoft. All rights reserved.
//  Licensed under the MIT license.
//  See LICENSE.txt file in the project root for full license information.
//

@testable import ThaliCore
import XCTest

class TCPClientTests: XCTestCase {

    // MARK: - State
    let noTCPTimeout: NSTimeInterval = -1
    let defaultTCPDataTag = 0
    let acceptConnectionTimeout: NSTimeInterval = 5.0
    let readDataTimeout: NSTimeInterval = 5.0
    let disconnectClientTimeout: NSTimeInterval = 5.0

    // MARK: - Tests
    func testTCPClientCanConnectToServerAndReturnsListenerPort() {
        // Expectations
        var mockServerAcceptedConnection: XCTestExpectation?

        // Given
        // Mock server that listening for incoming TCP connecitons
        let serverMock = TCPServerMock(didAcceptConnection: {
                                           mockServerAcceptedConnection?.fulfill()
                                       },
                                       didReadData: unexpectedReadDataHandler,
                                       didDisconnect: { _ in })

        var listenerPort: UInt16 = 0
        do {
            listenerPort = try serverMock.startListening()
        } catch {

        }

        XCTAssertNotEqual(listenerPort, 0)

        mockServerAcceptedConnection =
            expectationWithDescription("Mock server accepted connection")

        // When
        // TCP Client is trying to connect to TCP mock server
        let tcpClient = TCPClient(with: unexpectedReadDataHandler,
                                  didDisconnect: { _ in })
        tcpClient.connectToLocalhost(onPort: listenerPort) {
            socket, port, error in
            XCTAssertNil(error)
            XCTAssertEqual(port, listenerPort)
        }

        // Then
        waitForExpectationsWithTimeout(acceptConnectionTimeout) {
            error in
            mockServerAcceptedConnection = nil
        }
    }

    func testReadDataHandlerInvokedWhenTCPClientGetsData() {
        // Expectations
        var mockServerAcceptedConnection: XCTestExpectation?
        var dataReadHandler: XCTestExpectation?

        // Given
        // Mock server that listening for incoming TCP connecitons
        let serverMock = TCPServerMock(didAcceptConnection: {
                                           mockServerAcceptedConnection?.fulfill()
                                       },
                                       didReadData: { _ in },
                                       didDisconnect: { _ in })

        var listenerPort: UInt16 = 0
        do {
            listenerPort = try serverMock.startListening()
        } catch {

        }
        XCTAssertNotEqual(listenerPort, 0)

        mockServerAcceptedConnection =
            expectationWithDescription("Mock server accepted connection")

        // TCP Client is trying to connect to TCP mock server
        let tcpClient = TCPClient(with: {
                                      data in
                                      dataReadHandler?.fulfill()
                                  },
                                  didDisconnect: { _ in })
        tcpClient.connectToLocalhost(onPort: listenerPort) {
            socket, port, error in
            socket!.readDataWithTimeout(self.noTCPTimeout, tag: self.defaultTCPDataTag)
            XCTAssertNil(error)
            XCTAssertEqual(port, listenerPort)
        }

        waitForExpectationsWithTimeout(acceptConnectionTimeout) {
            error in
            mockServerAcceptedConnection = nil
        }

        // When
        // Mock server sends some data to TCP client
        dataReadHandler = expectationWithDescription("dataReadHandler invoked")
        serverMock.sendRandomMessage(length: 100)

        // Then
        waitForExpectationsWithTimeout(readDataTimeout) {
            error in
            dataReadHandler = nil
        }
    }

    func testDisconnectHandlerInvokedWhenServerDisconnects() {
        // Expectations
        var mockServerAcceptedConnection: XCTestExpectation?
        var didDisconnectHandler: XCTestExpectation?

        // Given
        // Mock server that listening for incoming TCP connecitons
        let serverMock = TCPServerMock(didAcceptConnection: {
                                           mockServerAcceptedConnection?.fulfill()
                                       },
                                       didReadData: unexpectedReadDataHandler,
                                       didDisconnect: { _ in })

        var listenerPort: UInt16 = 0
        do {
            listenerPort = try serverMock.startListening()
        } catch {

        }

        // TCP Client is trying to connect to TCP mock server
        mockServerAcceptedConnection =
            expectationWithDescription("Mock server accepted connection")

        let tcpClient = TCPClient(with: unexpectedReadDataHandler,
                                  didDisconnect: {
                                      socket in
                                      didDisconnectHandler?.fulfill()
                                  })
        tcpClient.connectToLocalhost(onPort: listenerPort) {
            socket, port, error in
            XCTAssertNil(error)
            XCTAssertEqual(port, listenerPort)
        }

        waitForExpectationsWithTimeout(acceptConnectionTimeout) {
            error in
            mockServerAcceptedConnection = nil
        }

        // When
        // Mock server disconnects TCP client
        didDisconnectHandler =
            expectationWithDescription("didDisconnectHandler invoked")
        serverMock.disconnectAllClients()

        // Then
        waitForExpectationsWithTimeout(disconnectClientTimeout) {
            error in
            didDisconnectHandler = nil
        }
    }
}<commit_msg>Improving code style in TCPClientTests.swift.<commit_after>//
//  Thali CordovaPlugin
//  TCPClientTests.swift
//
//  Copyright (C) Microsoft. All rights reserved.
//  Licensed under the MIT license.
//  See LICENSE.txt file in the project root for full license information.
//

@testable import ThaliCore
import XCTest

class TCPClientTests: XCTestCase {

    // MARK: - State
    let noTCPTimeout: NSTimeInterval = -1
    let defaultTCPDataTag = 0
    let acceptConnectionTimeout: NSTimeInterval = 5.0
    let readDataTimeout: NSTimeInterval = 5.0
    let disconnectClientTimeout: NSTimeInterval = 5.0

    // MARK: - Tests
    func testTCPClientCanConnectToServerAndReturnsListenerPort() {
        // Expectations
        var mockServerAcceptedConnection: XCTestExpectation?

        // Given
        // Mock server that listening for incoming TCP connecitons
        let serverMock = TCPServerMock(didAcceptConnection: {
                                           mockServerAcceptedConnection?.fulfill()
                                       },
                                       didReadData: unexpectedReadDataHandler,
                                       didDisconnect: { _ in })

        var listenerPort: UInt16 = 0
        do {
            listenerPort = try serverMock.startListening()
        } catch {

        }

        XCTAssertNotEqual(listenerPort, 0)

        mockServerAcceptedConnection = expectationWithDescription("Mock server accepted connection")

        // When
        // TCP Client is trying to connect to TCP mock server
        let tcpClient = TCPClient(with: unexpectedReadDataHandler,
                                  didDisconnect: { _ in })
        tcpClient.connectToLocalhost(onPort: listenerPort) {
            socket, port, error in
            XCTAssertNil(error)
            XCTAssertEqual(port, listenerPort)
        }

        // Then
        waitForExpectationsWithTimeout(acceptConnectionTimeout) {
            error in
            mockServerAcceptedConnection = nil
        }
    }

    func testReadDataHandlerInvokedWhenTCPClientGetsData() {
        // Expectations
        var mockServerAcceptedConnection: XCTestExpectation?
        var dataReadHandler: XCTestExpectation?

        // Given
        // Mock server that listening for incoming TCP connecitons
        let serverMock = TCPServerMock(didAcceptConnection: {
                                           mockServerAcceptedConnection?.fulfill()
                                       },
                                       didReadData: { _ in },
                                       didDisconnect: { _ in })

        var listenerPort: UInt16 = 0
        do {
            listenerPort = try serverMock.startListening()
        } catch {

        }
        XCTAssertNotEqual(listenerPort, 0)

        mockServerAcceptedConnection = expectationWithDescription("Mock server accepted connection")

        // TCP Client is trying to connect to TCP mock server
        let tcpClient = TCPClient(with: {
                                      data in
                                      dataReadHandler?.fulfill()
                                  },
                                  didDisconnect: { _ in })
        tcpClient.connectToLocalhost(onPort: listenerPort) {
            socket, port, error in
            socket!.readDataWithTimeout(self.noTCPTimeout, tag: self.defaultTCPDataTag)
            XCTAssertNil(error)
            XCTAssertEqual(port, listenerPort)
        }

        waitForExpectationsWithTimeout(acceptConnectionTimeout) {
            error in
            mockServerAcceptedConnection = nil
        }

        // When
        // Mock server sends some data to TCP client
        dataReadHandler = expectationWithDescription("dataReadHandler invoked")
        serverMock.sendRandomMessage(length: 100)

        // Then
        waitForExpectationsWithTimeout(readDataTimeout) {
            error in
            dataReadHandler = nil
        }
    }

    func testDisconnectHandlerInvokedWhenServerDisconnects() {
        // Expectations
        var mockServerAcceptedConnection: XCTestExpectation?
        var didDisconnectHandler: XCTestExpectation?

        // Given
        // Mock server that listening for incoming TCP connecitons
        let serverMock = TCPServerMock(didAcceptConnection: {
                                           mockServerAcceptedConnection?.fulfill()
                                       },
                                       didReadData: unexpectedReadDataHandler,
                                       didDisconnect: { _ in })

        var listenerPort: UInt16 = 0
        do {
            listenerPort = try serverMock.startListening()
        } catch {

        }

        // TCP Client is trying to connect to TCP mock server
        mockServerAcceptedConnection = expectationWithDescription("Mock server accepted connection")

        let tcpClient = TCPClient(with: unexpectedReadDataHandler,
                                  didDisconnect: {
                                      socket in
                                      didDisconnectHandler?.fulfill()
                                  })
        tcpClient.connectToLocalhost(onPort: listenerPort) {
            socket, port, error in
            XCTAssertNil(error)
            XCTAssertEqual(port, listenerPort)
        }

        waitForExpectationsWithTimeout(acceptConnectionTimeout) {
            error in
            mockServerAcceptedConnection = nil
        }

        // When
        // Mock server disconnects TCP client
        didDisconnectHandler = expectationWithDescription("didDisconnectHandler invoked")
        serverMock.disconnectAllClients()

        // Then
        waitForExpectationsWithTimeout(disconnectClientTimeout) {
            error in
            didDisconnectHandler = nil
        }
    }
}
{'fraction_non_alphanumeric': 0.05770212765957447, 'fraction_numerical': 0.00374468085106383, 'mean_word_length': 1.6026578073089701, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3405290', 'n_tokens_mistral': 2722, 'n_tokens_neox': 2598, 'n_words': 630}