forked from libp2p/go-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
58 lines (48 loc) · 1.14 KB
/
main_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
package main
import (
"context"
"log"
"testing"
"github.com/libp2p/go-libp2p/examples/testutils"
)
func TestMain(t *testing.T) {
var h testutils.LogHarness
h.Expect("listening for connections")
h.Expect("sender opening stream")
h.Expect("sender saying hello")
h.Expect("listener received new stream")
h.Expect("read: Hello, world!")
h.Expect(`read reply: "Hello, world!\n"`)
h.Run(t, func() {
// Create a context that will stop the hosts when the tests end
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Get a tcp port for the listener
lport, err := testutils.FindFreePort(t, "", 5)
if err != nil {
log.Println(err)
return
}
// Get a tcp port for the sender
sport, err := testutils.FindFreePort(t, "", 5)
if err != nil {
log.Println(err)
return
}
// Make listener
lh, err := makeBasicHost(lport, true, 1)
if err != nil {
log.Println(err)
return
}
startListener(ctx, lh, lport, true)
// Make sender
listenAddr := getHostAddress(lh)
sh, err := makeBasicHost(sport, true, 2)
if err != nil {
log.Println(err)
return
}
runSender(ctx, sh, listenAddr)
})
}