go123: lab.nexedi.com/kirr/go123/xnet/lonet Index | Files

package lonet

import "lab.nexedi.com/kirr/go123/xnet/lonet"

Package lonet provides TCP network simulated on top of localhost TCP loopback.

For testing distributed systems it is sometimes handy to imitate network of several TCP hosts. It is also handy that ports allocated on Dial/Listen/Accept on that hosts be predictable - that would help tests to verify network events against expected sequence. When whole system could be imitated in 1 OS-level process, package lab.nexedi.com/kirr/go123/xnet/pipenet serves the task via providing TCP-like synchronous in-memory network of net.Pipes. However pipenet cannot be used for cases where tested system consists of 2 or more OS-level processes. This is where lonet comes into play:

Similarly to pipenet addresses on lonet are host:port pairs and several hosts could be created with different names. A host is xnet.Networker and so can be worked with similarly to regular TCP network access-point with Dial/Listen/Accept. Host's ports allocation is predictable: ports of a host are contiguous integer sequence starting from 1 that are all initially free, and whenever autobind is requested the first free port of the host will be used.

Internally lonet network maintains registry of hosts so that lonet addresses could be resolved to OS-level addresses, for example α:1 and β:1 to 127.0.0.1:4567 and 127.0.0.1:8765, and once lonet connection is established it becomes served by OS-level TCP connection over loopback.

Example:

net, err := lonet.Join(ctx, "mynet")
hα, err := net.NewHost(ctx, "α")
hβ, err := net.NewHost(ctx, "β")

// starts listening on address "α:10"
l, err := hα.Listen(":10")
go func() {
	csrv, err := l.Accept()   // csrv will have LocalAddr "α:1"
}()
ccli, err := hβ.Dial(ctx, "α:10") // ccli will be connection between "β:1" - "α:1"

Once again lonet is similar to pipenet, but since it works via OS TCP stack it could be handy for testing networked application when there are several OS-level processes involved.

Package lonet also provides corresponding Python package for accessing lonet networks from Python(*).

--------

(*) use https://pypi.org/project/pygolang to import.

Index

Package Files

lonet.go registry_sqlite.go

func Join Uses

func Join(ctx context.Context, network string) (_ *virtnet.SubNetwork, err error)

Join joins or creates new lonet network with given name.

Network is the name of this network under "lonet" namespace, e.g. "α" will give full network name "lonetα".

If network is "" new network with random unique name will be created.

Join returns new subnetwork on the joined network.

See package lab.nexedi.com/kirr/go123/xnet/virtnet for documentation on how to use returned subnetwork.

Package lonet imports 16 packages (graph). Updated 2019-08-09. Refresh now. Tools for package owners.