<commit_before>package com.jenjinstudios.io.server

import com.jenjinstudios.io.connection.Connection
import com.jenjinstudios.io.connection.ReusableConnectionBuilder
import spock.lang.Specification

import java.util.function.Consumer

public class ServerSpec extends Specification {
    def "When started, Server should listen for and accept inbound connections"() {
        given: "A ServerSocket and Server"
            def serverSocket = Mock(ServerSocket)
            def server = new Server(serverSocket, null, [], [], [], [], [])

        when: "The server is started"
            server.start()
            Thread.sleep(100) // Give threads time to catch up

        then: "The ServerSocket should be accepting connections"
            (1.._) * serverSocket.accept()

        cleanup: "Stop the server"
            server.stop()
    }

    def "When stopped, Server should attempt to gracefully close all open connections"() {
        given: "A ServerSocket which returns a valid connection then blocks"
            def serverSocket = Mock(ServerSocket)
            def socket = Mock(Socket)
            serverSocket.accept() >>> [socket, null]
            def connection = Mock(Connection)
            def connectionBuilder = Mock(ReusableConnectionBuilder)
            connectionBuilder.build(socket) >> connection

        and: "A Server using the given socket"
            def server = new Server(serverSocket, connectionBuilder, [], [], [], [], [])

        when: "The server is started"
            server.start()
            Thread.sleep(100) // Give threads time to catch up

        then: "One connection should be made"
            server.connectionCount == 1

        when: "The server is stopped"
            server.stop()
            Thread.sleep(100) // Give threads time to catch up

        then: "The connection should be closed and removed"
            1 * connection.stop()
            1 * serverSocket.close()
            server.connectionCount == 0
    }

    def "When Server starts, callback(s) should be invoked"() {
        given: "A Server with a start callback"
            def serverSocket = Mock(ServerSocket)
            def callback = Mock(Consumer)
            def callbacks = [callback]
            def server = new Server(serverSocket, null, [], [], [], callbacks, [])

        when: "The Server is started"
            server.start()

        then: "The startup callback should be invoked"
            1 * callback.accept(server)

        cleanup: "Shutdown the server"
            server.stop()
    }

    def "When Server stops, callback(s) should be invoked"() {
        given: "A Server with a stop callback"
            def serverSocket = Mock(ServerSocket)
            def callback = Mock(Consumer)
            def callbacks = [callback]
            def server = new Server(serverSocket, null, [], [], [], [], callbacks)

        when: "The Server is started"
            server.start()

        then:
            0 * callback.accept(server)

        and: "The Server stops"
            server.stop()

        then: "The callback should be invoked"
            1 * callback.accept(server)
    }

    def "When Server adds Connection, callback(s) should be invoked"() {
        given: "A ServerSocket which returns a mocked connection"
            def serverSocket = Mock(ServerSocket)
            def socket = Mock(Socket)<commit_msg>Fix null issues in ServerSpec<commit_after>package com.jenjinstudios.io.server

import com.jenjinstudios.io.connection.Connection
import com.jenjinstudios.io.connection.ReusableConnectionBuilder
import spock.lang.Specification

import java.util.function.Consumer

public class ServerSpec extends Specification {
    def "When started, Server should listen for and accept inbound connections"() {
        given: "A ServerSocket and Server"
            def connectionBuilder = Mock(ReusableConnectionBuilder)
            def serverSocket = Mock(ServerSocket)
            def server = new Server(serverSocket, connectionBuilder, [], [], [], [], [])

        when: "The server is started"
            server.start()
            Thread.sleep(100) // Give threads time to catch up

        then: "The ServerSocket should be accepting connections"
            (1.._) * serverSocket.accept()

        cleanup: "Stop the server"
            server.stop()
    }

    def "When stopped, Server should attempt to gracefully close all open connections"() {
        given: "A ServerSocket which returns a valid connection then blocks"
            def serverSocket = Mock(ServerSocket)
            def socket = Mock(Socket)
            serverSocket.accept() >>> [socket, null]
            def connection = Mock(Connection)
            def connectionBuilder = Mock(ReusableConnectionBuilder)
            connectionBuilder.build(socket) >> connection

        and: "A Server using the given socket"
            def server = new Server(serverSocket, connectionBuilder, [], [], [], [], [])

        when: "The server is started"
            server.start()
            Thread.sleep(100) // Give threads time to catch up

        then: "One connection should be made"
            server.connectionCount == 1

        when: "The server is stopped"
            server.stop()
            Thread.sleep(100) // Give threads time to catch up

        then: "The connection should be closed"
            1 * connection.stop()
            1 * serverSocket.close()
    }

    def "When Server starts, callback(s) should be invoked"() {
        given: "A Server with a start callback"
            def connectionBuilder = Mock(ReusableConnectionBuilder)
            def serverSocket = Mock(ServerSocket)
            def callback = Mock(Consumer)
            def callbacks = [callback]
            def server = new Server(serverSocket, connectionBuilder, [], [], [], callbacks, [])

        when: "The Server is started"
            server.start()

        then: "The startup callback should be invoked"
            1 * callback.accept(server)

        cleanup: "Shutdown the server"
            server.stop()
    }

    def "When Server stops, callback(s) should be invoked"() {
        given: "A Server with a stop callback"
            def connectionBuilder = Mock(ReusableConnectionBuilder)
            def serverSocket = Mock(ServerSocket)
            def callback = Mock(Consumer)
            def callbacks = [callback]
            def server = new Server(serverSocket, connectionBuilder, [], [], [], [], callbacks)

        when: "The Server is started"
            server.start()

        then:
            0 * callback.accept(server)

        and: "The Server stops"
            server.stop()

        then: "The callback should be invoked"
            1 * callback.accept(server)
    }

    def "When Server adds Connection, callback(s) should be invoked"() {
        given: "A ServerSocket which returns a mocked connection"
            def serverSocket = Mock(ServerSocket)
            def socket = Mock(Socket)
{'fraction_non_alphanumeric': 0.09149576939624265, 'fraction_numerical': 0.0047325397963573785, 'mean_word_length': 2.218274111675127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3018417', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1660, 'n_words': 588}