forked from libp2p/go-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_test.go
70 lines (56 loc) · 1.28 KB
/
chat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"context"
"crypto/rand"
"fmt"
"log"
"testing"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p/examples/testutils"
)
func TestMain(t *testing.T) {
var h testutils.LogHarness
h.Expect("Waiting for incoming connection")
h.Expect("Established connection to destination")
h.Expect("Got a new stream!")
h.Run(t, func() {
// Create a context that will stop the hosts when the tests end
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
port1, err := testutils.FindFreePort(t, "", 5)
if err != nil {
log.Println(err)
return
}
port2, err := testutils.FindFreePort(t, "", 5)
if err != nil {
log.Println(err)
return
}
h1, err := makeHost(port1, rand.Reader)
if err != nil {
log.Println(err)
return
}
go startPeer(ctx, h1, func(network.Stream) {
log.Println("Got a new stream!")
cancel() // end the test
})
dest := fmt.Sprintf("/ip4/127.0.0.1/tcp/%v/p2p/%s", port1, h1.ID().Pretty())
h2, err := makeHost(port2, rand.Reader)
if err != nil {
log.Println(err)
return
}
go func() {
rw, err := startPeerAndConnect(ctx, h2, dest)
if err != nil {
log.Println(err)
return
}
rw.WriteString("test message")
rw.Flush()
}()
<-ctx.Done()
})
}