Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Added tests #193

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions chat-with-mdns/main_test.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions chat-with-rendezvous/chat-with-rendezvous_test.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions chat/chat_test.go
Original file line number Diff line number Diff line change
@@ -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
}
54 changes: 30 additions & 24 deletions echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -111,15 +90,15 @@ func main() {
}
})

if *target == "" {
if target == "" {
log.Println("listening for connections")
select {} // hang forever
}
/**** This is where the listener code ends ****/

// 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)
}
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions echo/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 9 additions & 0 deletions libp2p-host/host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"testing"
)

func TestMain(t *testing.M) {
go main()
}