Skip to content

Commit 567b3ac

Browse files
authored
Merge pull request #596 from Semisol/bugfix/fix-tests
Fix failing tests and test network
2 parents 776eaca + cb8ddef commit 567b3ac

File tree

3 files changed

+44
-56
lines changed

3 files changed

+44
-56
lines changed

rpc/internal/testnetwork/testnetwork.go

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ func (e edge) Flip() edge {
2525
}
2626
}
2727

28+
// This test network uses the same set of options for all
29+
// participants. The rpc.Options instance can be cloned
30+
// without issue.
2831
type network struct {
29-
myID PeerID
30-
global *Joiner
32+
myID PeerID
33+
options rpc.Options
34+
global *Joiner
3135
}
3236

3337
// A Joiner is a global view of a test network, which can be joined by a
@@ -51,12 +55,13 @@ func NewJoiner() *Joiner {
5155
}
5256
}
5357

54-
func (j *Joiner) Join() rpc.Network {
58+
func (j *Joiner) Join(opts *rpc.Options) rpc.Network {
5559
j.mu.Lock()
5660
defer j.mu.Unlock()
5761
ret := network{
58-
myID: j.nextID,
59-
global: j,
62+
myID: j.nextID,
63+
global: j,
64+
options: *opts,
6065
}
6166
j.nextID++
6267
return ret
@@ -72,13 +77,11 @@ func (j *Joiner) getAcceptQueue(id PeerID) spsc.Queue[PeerID] {
7277
}
7378

7479
func (n network) LocalID() rpc.PeerID {
75-
return rpc.PeerID{n.myID}
80+
return rpc.PeerID{Value: n.myID}
7681
}
7782

78-
func (n network) Dial(dst rpc.PeerID, opts *rpc.Options) (*rpc.Conn, error) {
79-
if opts == nil {
80-
opts = &rpc.Options{}
81-
}
83+
func (n network) Dial(dst rpc.PeerID) (*rpc.Conn, error) {
84+
opts := n.options
8285
opts.Network = n
8386
opts.RemotePeerID = dst
8487
dstID := dst.Value.(PeerID)
@@ -101,7 +104,7 @@ func (n network) Dial(dst rpc.PeerID, opts *rpc.Options) (*rpc.Conn, error) {
101104

102105
}
103106
if ent.Conn == nil {
104-
ent.Conn = rpc.NewConn(ent.Transport, opts)
107+
ent.Conn = rpc.NewConn(ent.Transport, &opts)
105108
} else {
106109
// There's already a connection, so we're not going to use this, but
107110
// we own it. So drop it:
@@ -110,30 +113,32 @@ func (n network) Dial(dst rpc.PeerID, opts *rpc.Options) (*rpc.Conn, error) {
110113
return ent.Conn, nil
111114
}
112115

113-
func (n network) Accept(ctx context.Context, opts *rpc.Options) (*rpc.Conn, error) {
116+
func (n network) Serve(ctx context.Context) error {
114117
n.global.mu.Lock()
115118
q := n.global.getAcceptQueue(n.myID)
116119
n.global.mu.Unlock()
117120

118-
incoming, err := q.Recv(ctx)
119-
if err != nil {
120-
return nil, err
121-
}
122-
opts.Network = n
123-
opts.RemotePeerID = rpc.PeerID{incoming}
124-
n.global.mu.Lock()
125-
defer n.global.mu.Unlock()
126-
edge := edge{
127-
From: n.myID,
128-
To: incoming,
121+
for {
122+
incoming, err := q.Recv(ctx)
123+
if err != nil {
124+
return err
125+
}
126+
opts := n.options
127+
opts.Network = n
128+
opts.RemotePeerID = rpc.PeerID{incoming}
129+
n.global.mu.Lock()
130+
defer n.global.mu.Unlock()
131+
edge := edge{
132+
From: n.myID,
133+
To: incoming,
134+
}
135+
ent := n.global.connections[edge]
136+
if ent.Conn == nil {
137+
ent.Conn = rpc.NewConn(ent.Transport, &opts)
138+
} else {
139+
opts.BootstrapClient.Release()
140+
}
129141
}
130-
ent := n.global.connections[edge]
131-
if ent.Conn == nil {
132-
ent.Conn = rpc.NewConn(ent.Transport, opts)
133-
} else {
134-
opts.BootstrapClient.Release()
135-
}
136-
return ent.Conn, nil
137142
}
138143

139144
func (n network) Introduce(provider, recipient *rpc.Conn) (rpc.IntroductionInfo, error) {
@@ -157,24 +162,7 @@ func (n network) Introduce(provider, recipient *rpc.Conn) (rpc.IntroductionInfo,
157162
sendToRecipient.SetNonce(nonce)
158163
sendToProvider.SetPeerId(uint64(recipientPeer.Value.(PeerID)))
159164
sendToProvider.SetNonce(nonce)
160-
ret.SendToRecipient = rpc.ThirdPartyCapID(sendToRecipient.ToPtr())
161-
ret.SendToProvider = rpc.RecipientID(sendToProvider.ToPtr())
165+
ret.SendToRecipient = rpc.ThirdPartyToContact(sendToRecipient.ToPtr())
166+
ret.SendToProvider = rpc.ThirdPartyToAwait(sendToProvider.ToPtr())
162167
return ret, nil
163168
}
164-
func (n network) DialIntroduced(capID rpc.ThirdPartyCapID, introducedBy *rpc.Conn) (*rpc.Conn, rpc.ProvisionID, error) {
165-
cid := PeerAndNonce(capnp.Ptr(capID).Struct())
166-
167-
_, seg := capnp.NewSingleSegmentMessage(nil)
168-
pid, err := NewPeerAndNonce(seg)
169-
if err != nil {
170-
return nil, rpc.ProvisionID{}, err
171-
}
172-
pid.SetPeerId(uint64(introducedBy.RemotePeerID().Value.(PeerID)))
173-
pid.SetNonce(cid.Nonce())
174-
175-
conn, err := n.Dial(rpc.PeerID{PeerID(cid.PeerId())}, nil)
176-
return conn, rpc.ProvisionID(pid.ToPtr()), err
177-
}
178-
func (n network) AcceptIntroduced(recipientID rpc.RecipientID, introducedBy *rpc.Conn) (*rpc.Conn, error) {
179-
panic("TODO")
180-
}

rpc/level1_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,5 +1053,5 @@ type rpcDisembargoContext struct {
10531053
Which rpccp.Disembargo_context_Which
10541054
SenderLoopback uint32
10551055
ReceiverLoopback uint32
1056-
Provide uint32
1056+
Accept capnp.Ptr
10571057
}

schemas/schemas_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ func TestDefaultFind(t *testing.T) {
1414
if s := schemas.Find(0xdeadbeef); s != nil {
1515
t.Errorf("schemas.Find(0xdeadbeef) = %d-byte slice; want nil", len(s))
1616
}
17-
s := schemas.Find(gocp.Package_)
17+
s := schemas.Find(gocp.Package)
1818
if s == nil {
19-
t.Fatalf("schemas.Find(%#x) = nil", gocp.Package_)
19+
t.Fatalf("schemas.Find(%#x) = nil", gocp.Package)
2020
}
2121
msg, err := capnp.Unmarshal(s)
2222
if err != nil {
23-
t.Fatalf("capnp.Unmarshal(schemas.Find(%#x)) error: %v", gocp.Package_, err)
23+
t.Fatalf("capnp.Unmarshal(schemas.Find(%#x)) error: %v", gocp.Package, err)
2424
}
2525
req, err := schema.ReadRootCodeGeneratorRequest(msg)
2626
if err != nil {
@@ -32,15 +32,15 @@ func TestDefaultFind(t *testing.T) {
3232
}
3333
for i := 0; i < nodes.Len(); i++ {
3434
n := nodes.At(i)
35-
if n.Id() == gocp.Package_ {
35+
if n.Id() == gocp.Package {
3636
// Found
3737
if n.Which() != schema.Node_Which_annotation {
38-
t.Errorf("found node %#x which = %v; want annotation", gocp.Package_, n.Which())
38+
t.Errorf("found node %#x which = %v; want annotation", gocp.Package, n.Which())
3939
}
4040
return
4141
}
4242
}
43-
t.Fatalf("could not find node %#x in registry", gocp.Package_)
43+
t.Fatalf("could not find node %#x in registry", gocp.Package)
4444
}
4545

4646
func TestNotFound(t *testing.T) {

0 commit comments

Comments
 (0)