NibiruChain / nibiru

Nibiru Chain: The breakthrough smart contract platform ushering in the next era of money. Nibiru powers an ecosystem of dApps including perps, RWAs, and more.
https://nibiru.fi
Apache License 2.0
178 stars 208 forks source link

test(testutil): Perform synchronous cleanup of nodes on cleanup #1955

Closed Unique-Divine closed 15 hours ago

Unique-Divine commented 1 week ago

Is there a cleaner way to do this with a synchronous check?

// Cleanup removes the root testing (temporary) directory and stops both the
// Tendermint and API services. It allows other callers to create and start
// test networks. This method must be called when a test is finished, typically
// in a defer.
func (n *Network) Cleanup() {
    defer func() {
        lock.Unlock()
        n.Logger.Log("released test network lock")
    }()

    n.Logger.Log("cleaning up test network...")

    for _, v := range n.Validators {
        if v.tmNode != nil && v.tmNode.IsRunning() {
            _ = v.tmNode.Stop()
        }

        if v.api != nil {
            _ = v.api.Close()
        }

        if v.grpc != nil {
            v.grpc.Stop()
            if v.grpcWeb != nil {
                _ = v.grpcWeb.Close()
            }
        }
        if v.jsonrpc != nil {
            _ = v.jsonrpc.Close()
        }
    }

    for _, v := range n.Validators {
        _ = v.tmNode.Stop()
    }

    // TODO: Is there a cleaner way to do this with a synchronous check?

    // Give a brief pause for things to finish closing in other processes.
    // Hopefully this helps with the address-in-use errors.
    // Timeout of 100ms chosen randomly.
    // Timeout of 500ms chosen because 100ms was not enough. | 2024-07-02
    time.Sleep(500 * time.Millisecond)

    if n.Config.CleanupDir {
        _ = os.RemoveAll(n.BaseDir)
    }

    n.Logger.Log("finished cleaning up test network")
}