diff --git a/README.md b/README.md index 0bdc014..6f20b47 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,6 @@ To obtain a clean `$GOPATH` execute the following: > mkdir /tmp/libp2p-examples > export GOPATH=/tmp/libp2p/examples ``` + +### Tests +`go test ./...` will run all tests in the repo diff --git a/chat-with-mdns/main_test.go b/chat-with-mdns/main_test.go new file mode 100644 index 0000000..12e30b6 --- /dev/null +++ b/chat-with-mdns/main_test.go @@ -0,0 +1,13 @@ +package main + +import ( + "testing" + "time" +) + +//"Chat with mdns" will poll for input forever, here just run the main logic to see if there are any runtime errors +func TestMain(t *testing.M) { + timer1 := time.NewTimer(2 * time.Second) + go main() + <-timer1.C +} diff --git a/chat-with-rendezvous/chat-with-rendezvous_test.go b/chat-with-rendezvous/chat-with-rendezvous_test.go new file mode 100644 index 0000000..9067d37 --- /dev/null +++ b/chat-with-rendezvous/chat-with-rendezvous_test.go @@ -0,0 +1,14 @@ +package main + +import ( + "testing" + "time" +) + +//"Chat with rendezvous" will poll for input forever, here just run the main logic to see if there are any runtime errors +//FIXME: As of the time of writing, Chat with rendezvous doesn't work properly, yet this test does not fail. +func TestMain(t *testing.M) { + timer1 := time.NewTimer(2 * time.Second) + go main() + <-timer1.C +} diff --git a/chat/chat_test.go b/chat/chat_test.go new file mode 100644 index 0000000..398cf21 --- /dev/null +++ b/chat/chat_test.go @@ -0,0 +1,13 @@ +package main + +import ( + "testing" + "time" +) + +//"Chat" will poll for input forever, here just run the main logic to see if there are any runtime errors +func TestMain(t *testing.M) { + timer1 := time.NewTimer(2 * time.Second) + go main() + <-timer1.C +} diff --git a/echo/main.go b/echo/main.go index cd6f582..f36fb27 100644 --- a/echo/main.go +++ b/echo/main.go @@ -76,28 +76,7 @@ func makeBasicHost(listenPort int, insecure bool, randseed int64) (host.Host, er return basicHost, nil } -func main() { - // LibP2P code uses golog to log messages. They log with different - // string IDs (i.e. "swarm"). We can control the verbosity level for - // all loggers with: - golog.SetAllLoggers(gologging.INFO) // Change to DEBUG for extra info - - // Parse options from the command line - listenF := flag.Int("l", 0, "wait for incoming connections") - target := flag.String("d", "", "target peer to dial") - insecure := flag.Bool("insecure", false, "use an unencrypted connection") - seed := flag.Int64("seed", 0, "set random seed for id generation") - flag.Parse() - - if *listenF == 0 { - log.Fatal("Please provide a port to bind on with -l") - } - - // Make a host that listens on the given multiaddress - ha, err := makeBasicHost(*listenF, *insecure, *seed) - if err != nil { - log.Fatal(err) - } +func echo(target string, ha host.Host) { // Set a stream handler on host A. /echo/1.0.0 is // a user-defined protocol name. @@ -111,7 +90,7 @@ func main() { } }) - if *target == "" { + if target == "" { log.Println("listening for connections") select {} // hang forever } @@ -119,7 +98,7 @@ func main() { // The following code extracts target's the peer ID from the // given multiaddress - ipfsaddr, err := ma.NewMultiaddr(*target) + ipfsaddr, err := ma.NewMultiaddr(target) if err != nil { log.Fatalln(err) } @@ -165,6 +144,33 @@ func main() { log.Printf("read reply: %q\n", out) } +func main() { + // LibP2P code uses golog to log messages. They log with different + // string IDs (i.e. "swarm"). We can control the verbosity level for + // all loggers with: + golog.SetAllLoggers(gologging.INFO) // Change to DEBUG for extra info + + // Parse options from the command line + listenF := flag.Int("l", 0, "wait for incoming connections") + target := flag.String("d", "", "target peer to dial") + insecure := flag.Bool("insecure", false, "use an unencrypted connection") + seed := flag.Int64("seed", 0, "set random seed for id generation") + flag.Parse() + + if *listenF == 0 { + log.Fatal("Please provide a port to bind on with -l") + } + + // Make a host that listens on the given multiaddress + ha, err := makeBasicHost(*listenF, *insecure, *seed) + if err != nil { + log.Fatal(err) + } + + echo(*target, ha) + +} + // doEcho reads a line of data a stream and writes it back func doEcho(s network.Stream) error { buf := bufio.NewReader(s) diff --git a/echo/main_test.go b/echo/main_test.go new file mode 100644 index 0000000..cb83129 --- /dev/null +++ b/echo/main_test.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + ma "github.com/multiformats/go-multiaddr" + "log" + "testing" +) + +// send an echo from port 7001 to port 7000 +func TestMain(m *testing.M) { + + listenHost, err := makeBasicHost(7000, false, 0) + if err != nil { + log.Fatal(err) + } + // Build host multiaddress + hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", listenHost.ID().Pretty())) + addr := listenHost.Addrs()[0] + fullAddr := addr.Encapsulate(hostAddr) + + pingHost, err := makeBasicHost(7001, false, 0) + if err != nil { + log.Fatal(err) + } + + go echo("", listenHost) + echo(fullAddr.String(), pingHost) +} diff --git a/libp2p-host/host_test.go b/libp2p-host/host_test.go new file mode 100644 index 0000000..6ace2d6 --- /dev/null +++ b/libp2p-host/host_test.go @@ -0,0 +1,9 @@ +package main + +import ( + "testing" +) + +func TestMain(t *testing.M) { + go main() +}