Setting up environment to debug a network protocol

Hello, everyone!

I am writing a package for a little bit involved network protocol which I have outlined here (some details like the use of TOR and ballot key had become obsolete). An essential part of the protocol is the necessity of having parallel user connections, and so writing tests start to take much more time than actual code.

I came up with an idea how to relieve my test writing process by having a single test file:

using Sockets

@sync begin
    ### The server
    @async let 
        server = listen(2000)
        try 
            usersocket1 = accept(server)
            usersocket2 = accept(server)
            usersocket3 = accept(server)

            println(usersocket1,"Hello")

            println(usersocket2,"World")
            println(usersocket2,"World")

            println(usersocket3,"Third")
            println(usersocket3,"Third")

            println(usersocket1,"Hello")
        finally
            close(server)
        end
    end
    ### Three users
    @async let 
        usersocket = connect(2000)
        @show readline(usersocket)
        @show readline(usersocket)
    end
    @async let 
        usersocket = connect(2000)
        @show readline(usersocket)
        @show readline(usersocket)
    end
    @async let 
        usersocket = connect(2000)
        @show readline(usersocket)
        @show readline(usersocket)
    end
end

which seems to work excellent.

The next step for the test would be to put code in the let blocks into separate files. But when I do it like this:

@sync begin
    @async let 
        try 
            include("server.jl")
        finally
            close(server)
        end
    end
    @async let 
        include("user.jl")
    end
    @async let 
        include("user.jl")
    end
    @async let 
        include("user.jl")
    end
end

where user.jl, server.jl and router.jl contains actual code it does not work (users do not get all messages).

Is there a better way how I could load the code into corresponding places?

I managed to solve the problem successfully by using functions. So the test file for me looks something like:

@sync begin
    @async begin
        server = listen(2000) 
        try 
            servercom(server)
        finally
            close(server)
        end
    end
    @async user(2000)
    @async user(2000)
    @async user(2000)
end

which forks perfectly for the development :slight_smile: