Skip to content

Commit 699fdde

Browse files
committed
basic rcmgr integration tests
1 parent 1d2937d commit 699fdde

File tree

3 files changed

+345
-39
lines changed

3 files changed

+345
-39
lines changed

itest/echo.go

+81-8
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ var (
2626
type Echo struct {
2727
Host host.Host
2828

29-
WaitBeforeRead, WaitBeforeWrite func() error
30-
3129
mx sync.Mutex
3230
status EchoStatus
31+
32+
beforeReserve, beforeRead, beforeWrite, beforeDone func() error
3333
}
3434

3535
type EchoStatus struct {
@@ -53,13 +53,78 @@ func (e *Echo) Status() EchoStatus {
5353
return e.status
5454
}
5555

56+
func (e *Echo) BeforeReserve(f func() error) {
57+
e.mx.Lock()
58+
defer e.mx.Unlock()
59+
60+
e.beforeReserve = f
61+
}
62+
63+
func (e *Echo) BeforeRead(f func() error) {
64+
e.mx.Lock()
65+
defer e.mx.Unlock()
66+
67+
e.beforeRead = f
68+
}
69+
70+
func (e *Echo) BeforeWrite(f func() error) {
71+
e.mx.Lock()
72+
defer e.mx.Unlock()
73+
74+
e.beforeWrite = f
75+
}
76+
77+
func (e *Echo) BeforeDone(f func() error) {
78+
e.mx.Lock()
79+
defer e.mx.Unlock()
80+
81+
e.beforeDone = f
82+
}
83+
84+
func (e *Echo) getBeforeReserve() func() error {
85+
e.mx.Lock()
86+
defer e.mx.Unlock()
87+
88+
return e.beforeReserve
89+
}
90+
91+
func (e *Echo) getBeforeRead() func() error {
92+
e.mx.Lock()
93+
defer e.mx.Unlock()
94+
95+
return e.beforeRead
96+
}
97+
98+
func (e *Echo) getBeforeWrite() func() error {
99+
e.mx.Lock()
100+
defer e.mx.Unlock()
101+
102+
return e.beforeWrite
103+
}
104+
105+
func (e *Echo) getBeforeDone() func() error {
106+
e.mx.Lock()
107+
defer e.mx.Unlock()
108+
109+
return e.beforeDone
110+
}
111+
56112
func (e *Echo) handleStream(s network.Stream) {
57113
defer s.Close()
58114

59115
e.mx.Lock()
60116
e.status.StreamsIn++
61117
e.mx.Unlock()
62118

119+
if beforeReserve := e.getBeforeReserve(); beforeReserve != nil {
120+
if err := beforeReserve(); err != nil {
121+
echoLog.Debugf("error syncing before reserve: %s", err)
122+
123+
s.Reset()
124+
return
125+
}
126+
}
127+
63128
if err := s.Scope().SetService(EchoService); err != nil {
64129
echoLog.Debugf("error attaching stream to echo service: %s", err)
65130

@@ -82,9 +147,9 @@ func (e *Echo) handleStream(s network.Stream) {
82147
return
83148
}
84149

85-
if e.WaitBeforeRead != nil {
86-
if err := e.WaitBeforeRead(); err != nil {
87-
echoLog.Debugf("error waiting before read: %s", err)
150+
if beforeRead := e.getBeforeRead(); beforeRead != nil {
151+
if err := beforeRead(); err != nil {
152+
echoLog.Debugf("error syncing before read: %s", err)
88153

89154
s.Reset()
90155
return
@@ -116,9 +181,9 @@ func (e *Echo) handleStream(s network.Stream) {
116181
e.status.EchosIn++
117182
e.mx.Unlock()
118183

119-
if e.WaitBeforeWrite != nil {
120-
if err := e.WaitBeforeWrite(); err != nil {
121-
echoLog.Debugf("error waiting before write: %s", err)
184+
if beforeWrite := e.getBeforeWrite(); beforeWrite != nil {
185+
if err := beforeWrite(); err != nil {
186+
echoLog.Debugf("error syncing before write: %s", err)
122187

123188
s.Reset()
124189
return
@@ -143,6 +208,14 @@ func (e *Echo) handleStream(s network.Stream) {
143208
e.mx.Unlock()
144209

145210
s.CloseWrite()
211+
212+
if beforeDone := e.getBeforeDone(); beforeDone != nil {
213+
if err := beforeDone(); err != nil {
214+
echoLog.Debugf("error syncing before done: %s", err)
215+
216+
s.Reset()
217+
}
218+
}
146219
}
147220

148221
func (e *Echo) Echo(p peer.ID, what string) error {

itest/echo_test.go

+18-31
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ import (
77
"github.com/libp2p/go-libp2p"
88
"github.com/libp2p/go-libp2p-core/peer"
99
"github.com/libp2p/go-libp2p-core/peerstore"
10+
11+
"github.com/stretchr/testify/require"
1012
)
1113

12-
func createEchos(t *testing.T, count int, opts ...libp2p.Option) []*Echo {
14+
func createEchos(t *testing.T, count int, makeOpts ...func(int) libp2p.Option) []*Echo {
1315
result := make([]*Echo, 0, count)
1416

1517
for i := 0; i < count; i++ {
18+
opts := make([]libp2p.Option, 0, len(makeOpts))
19+
for _, makeOpt := range makeOpts {
20+
opts = append(opts, makeOpt(i))
21+
}
22+
1623
h, err := libp2p.New(opts...)
1724
if err != nil {
1825
t.Fatal(err)
@@ -35,46 +42,26 @@ func createEchos(t *testing.T, count int, opts ...libp2p.Option) []*Echo {
3542
return result
3643
}
3744

45+
func closeEchos(echos []*Echo) {
46+
for _, e := range echos {
47+
e.Host.Close()
48+
}
49+
}
50+
3851
func checkEchoStatus(t *testing.T, e *Echo, expected EchoStatus) {
3952
t.Helper()
40-
41-
status := e.Status()
42-
43-
if status.StreamsIn != expected.StreamsIn {
44-
t.Fatalf("expected %d streams in, got %d", expected.StreamsIn, status.StreamsIn)
45-
}
46-
if status.EchosIn != expected.EchosIn {
47-
t.Fatalf("expected %d echos in, got %d", expected.EchosIn, status.EchosIn)
48-
}
49-
if status.EchosOut != expected.EchosOut {
50-
t.Fatalf("expected %d echos out, got %d", expected.EchosOut, status.EchosOut)
51-
}
52-
if status.IOErrors != expected.IOErrors {
53-
t.Fatalf("expected %d I/O errors, got %d", expected.IOErrors, status.IOErrors)
54-
}
55-
if status.ResourceServiceErrors != expected.ResourceServiceErrors {
56-
t.Fatalf("expected %d service resource errors, got %d", expected.ResourceServiceErrors, status.ResourceServiceErrors)
57-
}
58-
if status.ResourceReservationErrors != expected.ResourceReservationErrors {
59-
t.Fatalf("expected %d reservation resource errors, got %d", expected.ResourceReservationErrors, status.ResourceReservationErrors)
60-
}
53+
require.Equal(t, expected, e.Status())
6154
}
6255

6356
func TestEcho(t *testing.T) {
6457
echos := createEchos(t, 2)
58+
defer closeEchos(echos)
6559

66-
err := echos[0].Host.Connect(context.TODO(), peer.AddrInfo{ID: echos[1].Host.ID()})
67-
if err != nil {
60+
if err := echos[0].Host.Connect(context.TODO(), peer.AddrInfo{ID: echos[1].Host.ID()}); err != nil {
6861
t.Fatal(err)
6962
}
7063

71-
defer func() {
72-
for _, e := range echos {
73-
e.Host.Close()
74-
}
75-
}()
76-
77-
if err = echos[0].Echo(echos[1].Host.ID(), "hello libp2p"); err != nil {
64+
if err := echos[0].Echo(echos[1].Host.ID(), "hello libp2p"); err != nil {
7865
t.Fatal(err)
7966
}
8067

0 commit comments

Comments
 (0)