io/tcp-abort.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
418e8e6855f7c72121c2b070e3eb5726b7dee4bc
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

// Explicit TCP transport abort over loopback.
//
// Requires: bin/protos --allow-network protos/examples/io/tcp-abort.protos
//
// The program listens on 127.0.0.1 with an OS-selected port, connects to it,
// and then aborts its own connection. abort() irreversibly retires the
// transport: later reads and writes fail with IOLifecycleError. Abort does not
// promise a TCP RST, immediate peer detection or rollback of committed output.
// Results are checked in Protos and reported through an explicit Process
// writer.

Assertions: import("std:test/assertions")
ProcessStreams: import("std:io/process_streams")
IpAddress: import("std:network/ip_addresses").IpAddress
IpEndpoint: import("std:network/ip_endpoints").IpEndpoint

out: ProcessStreams.stdoutWriter(process)
report: (line) => { out.writeLine(line).value() }

loopback: IpAddress(4, 2130706433)    // 127.0.0.1
listener: network.listenTcp(ipVersion: 4, address: loopback, port: null).value()

session: () => {
    pendingPeer: listener.accept()
    connection: network.connectTcp(IpEndpoint(loopback, listener.localPort())).value()
    peer: pendingPeer.value()

    exercise: () => {
        connection.write(Encoding.UTF8.encode("hello")).value()

        // Each retirement call answers a fresh Future observing one retirement.
        first: connection.abort()
        second: connection.abort()
        Assertions.require(first !== second)
        Assertions.require(first.value() === connection)
        Assertions.require(second.value() === connection)
        Assertions.require(connection.close().value() === connection)
        report("abort retired the connection")

        // New work is rejected after retirement.
        Assertions.require(IOLifecycleError.handle(
            () => { connection.read(1).value(); false },
            (error) => { true }
        ))
        Assertions.require(IOLifecycleError.handle(
            () => { connection.write(Encoding.UTF8.encode("late")).value(); false },
            (error) => { true }
        ))
        report("reads and writes after abort fail with IOLifecycleError")
    }
    exercise.ensure(() => { peer.close().value() })
}
session.ensure(() => { listener.close().value() })

report("peer and listener closed")
out.close().value()