<commit_before>import Dispatch
import TCP
import XCTest

class SocketsTests: XCTestCase {
    func testConnect() throws {
        let socket = try Socket()
        try socket.connect(hostname: "google.com")

        let data = """
        GET / HTTP/1.1\r
        Host: google.com\r
        Content-Length: 2\r
        \r
        hi
        """.data(using: .utf8)!

        let queue = DispatchQueue(label: "codes.vapor.test")

        let write = DispatchSource.makeWriteSource(fileDescriptor: socket.descriptor.raw, queue: queue)
        write.setEventHandler {
            _ = try! socket.write(data)
        }
        write.resume()

        let semaphore = DispatchSemaphore(value: 0)

        let read = DispatchSource.makeReadSource(fileDescriptor: socket.descriptor.raw, queue: queue)
        read.setEventHandler {
            let response = try! socket.read(max: 8_192)

            let string = String(data: response, encoding: .utf8)
            XCTAssert(string?.contains("HTTP/1.0 400 Bad Request") == true)
            semaphore.signal()
        }
        read.resume()

        XCTAssertNotNil([read, write])
        semaphore.wait()
    }

    func testBind() throws {
        let server = try Socket()
        try server.bind(hostname: "localhost", port: 8337)
        try server.listen()

        let queue = DispatchQueue(label: "codes.vapor.test")
        let semaphore = DispatchSemaphore(value: 0)

        var accepted: (Socket, DispatchSourceRead)?

        let read = DispatchSource.makeReadSource(fileDescriptor: server.descriptor.raw, queue: queue)
        read.setEventHandler {
            let client = try! server.accept()
            let read = DispatchSource.makeReadSource(
                fileDescriptor: client.descriptor.raw,
                queue: queue
            )
            read.setEventHandler {
                let data = try! client.read(max: 8_192)
                XCTAssertEqual(String(data: data, encoding: .utf8), "hello")
                semaphore.signal()
            }
            read.resume()
            
            accepted = (client, read)
            XCTAssertNotNil(accepted)
            XCTAssertEqual(client.address?.remoteAddress, "127.0.0.1")
        }
        read.resume()
        XCTAssertNotNil(read)

        try clientHello()

        semaphore.wait()
    }
    
    func testServer() throws {
        let server = try Server()
        try server.start(port: 8337)
        let semaphore = DispatchSemaphore(value: 0)
        
        server.drain { client in
            client.drain { buffer in
                XCTAssertEqual(String(bytes: buffer, encoding: .utf8), "hello")
                semaphore.signal()
            }
            
            client.start()
        }
        
        try clientHello()
        semaphore.wait()
    }

    static let allTests = [
        ("testConnect", testConnect),
        ("testBind", testBind),
        ("testServer", testServer),
    ]
}

fileprivate func clientHello() throws {
    do {
        let client = try Socket(isNonBlocking: false)
        try client.connect(hostname: "localhost", port: 8337)
        let data = "hello".data(using: .utf8)!
        _ = try! client.write(data)
    }
}
<commit_msg>fix 2 tests to be more relaxed<commit_after>import Dispatch
import TCP
import XCTest

class SocketsTests: XCTestCase {
    func testConnect() throws {
        let socket = try Socket()
        try socket.connect(hostname: "google.com")

        let data = """
        GET / HTTP/1.1\r
        Host: google.com\r
        Content-Length: 2\r
        \r
        hi
        """.data(using: .utf8)!

        let queue = DispatchQueue(label: "codes.vapor.test")

        let write = DispatchSource.makeWriteSource(fileDescriptor: socket.descriptor.raw, queue: queue)
        write.setEventHandler {
            _ = try! socket.write(data)
        }
        write.resume()

        let semaphore = DispatchSemaphore(value: 0)

        let read = DispatchSource.makeReadSource(fileDescriptor: socket.descriptor.raw, queue: queue)
        read.setEventHandler {
            let response = try! socket.read(max: 8_192)

            let string = String(data: response, encoding: .utf8)
            XCTAssert(string?.contains("HTTP/1.0 400 Bad Request") == true)
            semaphore.signal()
        }
        read.resume()

        XCTAssertNotNil([read, write])
        semaphore.wait()
    }

    func testBind() throws {
        let server = try Socket()
        try server.bind(hostname: "localhost", port: 8337)
        try server.listen()

        let queue = DispatchQueue(label: "codes.vapor.test")
        let semaphore = DispatchSemaphore(value: 0)

        var accepted: (Socket, DispatchSourceRead)?

        let read = DispatchSource.makeReadSource(fileDescriptor: server.descriptor.raw, queue: queue)
        read.setEventHandler {
            let client = try! server.accept()
            let read = DispatchSource.makeReadSource(
                fileDescriptor: client.descriptor.raw,
                queue: queue
            )
            read.setEventHandler {
                let data = try! client.read(max: 8_192)
                XCTAssertEqual(String(data: data, encoding: .utf8), "hello")
                semaphore.signal()
            }
            read.resume()
            
            accepted = (client, read)
            XCTAssertNotNil(accepted)
            XCTAssert(client.address?.remoteAddress == "127.0.0.1" || client.address?.remoteAddress == "0.0.0.0" || client.address?.remoteAddress == "::1")
        }
        read.resume()
        XCTAssertNotNil(read)

        try clientHello(port: 8337)

        semaphore.wait()
    }
    
    func testServer() throws {
        let server = try Server()
        try server.start(port: 8338)
        let semaphore = DispatchSemaphore(value: 0)
        
        server.drain { client in
            client.drain { buffer in
                XCTAssertEqual(String(bytes: buffer, encoding: .utf8), "hello")
                semaphore.signal()
            }
            
            client.start()
        }
        
        try clientHello(port: 8338)
        semaphore.wait()
    }

    static let allTests = [
        ("testConnect", testConnect),
        ("testBind", testBind),
        ("testServer", testServer),
    ]
}

fileprivate func clientHello(port: UInt16) throws {
    do {
        let client = try Socket(isNonBlocking: false)
        try client.connect(hostname: "localhost", port: port)
        let data = "hello".data(using: .utf8)!
        _ = try! client.write(data)
    }
}

{'fraction_non_alphanumeric': 0.10647023086269745, 'fraction_numerical': 0.014580801944106925, 'mean_word_length': 1.8730366492146597, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7536418', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1877, 'n_words': 423}