|
|
@ -28,17 +28,14 @@ import ( |
|
|
|
) |
|
|
|
|
|
|
|
func echoReactor(ctx context.Context, channel *p2p.Channel) { |
|
|
|
for { |
|
|
|
select { |
|
|
|
case envelope := <-channel.In: |
|
|
|
value := envelope.Message.(*p2ptest.Message).Value |
|
|
|
if err := channel.Send(ctx, p2p.Envelope{ |
|
|
|
To: envelope.From, |
|
|
|
Message: &p2ptest.Message{Value: value}, |
|
|
|
}); err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
case <-ctx.Done(): |
|
|
|
iter := channel.Receive(ctx) |
|
|
|
for iter.Next(ctx) { |
|
|
|
envelope := iter.Envelope() |
|
|
|
value := envelope.Message.(*p2ptest.Message).Value |
|
|
|
if err := channel.Send(ctx, p2p.Envelope{ |
|
|
|
To: envelope.From, |
|
|
|
Message: &p2ptest.Message{Value: value}, |
|
|
|
}); err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -76,14 +73,15 @@ func TestRouter_Network(t *testing.T) { |
|
|
|
Broadcast: true, |
|
|
|
Message: &p2ptest.Message{Value: "bar"}, |
|
|
|
}) |
|
|
|
expect := []p2p.Envelope{} |
|
|
|
expect := []*p2p.Envelope{} |
|
|
|
for _, peer := range peers { |
|
|
|
expect = append(expect, p2p.Envelope{ |
|
|
|
From: peer.NodeID, |
|
|
|
Message: &p2ptest.Message{Value: "bar"}, |
|
|
|
expect = append(expect, &p2p.Envelope{ |
|
|
|
From: peer.NodeID, |
|
|
|
ChannelID: 1, |
|
|
|
Message: &p2ptest.Message{Value: "bar"}, |
|
|
|
}) |
|
|
|
} |
|
|
|
p2ptest.RequireReceiveUnordered(t, channel, expect) |
|
|
|
p2ptest.RequireReceiveUnordered(ctx, t, channel, expect) |
|
|
|
|
|
|
|
// We then submit an error for a peer, and watch it get disconnected and
|
|
|
|
// then reconnected as the router retries it.
|
|
|
@ -162,7 +160,7 @@ func TestRouter_Channel_Basic(t *testing.T) { |
|
|
|
To: selfID, |
|
|
|
Message: &p2ptest.Message{Value: "self"}, |
|
|
|
}) |
|
|
|
p2ptest.RequireEmpty(t, channel) |
|
|
|
p2ptest.RequireEmpty(ctx, t, channel) |
|
|
|
} |
|
|
|
|
|
|
|
// Channel tests are hairy to mock, so we use an in-memory network instead.
|
|
|
@ -186,45 +184,45 @@ func TestRouter_Channel_SendReceive(t *testing.T) { |
|
|
|
// Sending a message a->b should work, and not send anything
|
|
|
|
// further to a, b, or c.
|
|
|
|
p2ptest.RequireSend(ctx, t, a, p2p.Envelope{To: bID, Message: &p2ptest.Message{Value: "foo"}}) |
|
|
|
p2ptest.RequireReceive(t, b, p2p.Envelope{From: aID, Message: &p2ptest.Message{Value: "foo"}}) |
|
|
|
p2ptest.RequireEmpty(t, a, b, c) |
|
|
|
p2ptest.RequireReceive(ctx, t, b, p2p.Envelope{From: aID, Message: &p2ptest.Message{Value: "foo"}}) |
|
|
|
p2ptest.RequireEmpty(ctx, t, a, b, c) |
|
|
|
|
|
|
|
// Sending a nil message a->b should be dropped.
|
|
|
|
p2ptest.RequireSend(ctx, t, a, p2p.Envelope{To: bID, Message: nil}) |
|
|
|
p2ptest.RequireEmpty(t, a, b, c) |
|
|
|
p2ptest.RequireEmpty(ctx, t, a, b, c) |
|
|
|
|
|
|
|
// Sending a different message type should be dropped.
|
|
|
|
p2ptest.RequireSend(ctx, t, a, p2p.Envelope{To: bID, Message: &gogotypes.BoolValue{Value: true}}) |
|
|
|
p2ptest.RequireEmpty(t, a, b, c) |
|
|
|
p2ptest.RequireEmpty(ctx, t, a, b, c) |
|
|
|
|
|
|
|
// Sending to an unknown peer should be dropped.
|
|
|
|
p2ptest.RequireSend(ctx, t, a, p2p.Envelope{ |
|
|
|
To: types.NodeID(strings.Repeat("a", 40)), |
|
|
|
Message: &p2ptest.Message{Value: "a"}, |
|
|
|
}) |
|
|
|
p2ptest.RequireEmpty(t, a, b, c) |
|
|
|
p2ptest.RequireEmpty(ctx, t, a, b, c) |
|
|
|
|
|
|
|
// Sending without a recipient should be dropped.
|
|
|
|
p2ptest.RequireSend(ctx, t, a, p2p.Envelope{Message: &p2ptest.Message{Value: "noto"}}) |
|
|
|
p2ptest.RequireEmpty(t, a, b, c) |
|
|
|
p2ptest.RequireEmpty(ctx, t, a, b, c) |
|
|
|
|
|
|
|
// Sending to self should be dropped.
|
|
|
|
p2ptest.RequireSend(ctx, t, a, p2p.Envelope{To: aID, Message: &p2ptest.Message{Value: "self"}}) |
|
|
|
p2ptest.RequireEmpty(t, a, b, c) |
|
|
|
p2ptest.RequireEmpty(ctx, t, a, b, c) |
|
|
|
|
|
|
|
// Removing b and sending to it should be dropped.
|
|
|
|
network.Remove(ctx, t, bID) |
|
|
|
p2ptest.RequireSend(ctx, t, a, p2p.Envelope{To: bID, Message: &p2ptest.Message{Value: "nob"}}) |
|
|
|
p2ptest.RequireEmpty(t, a, b, c) |
|
|
|
p2ptest.RequireEmpty(ctx, t, a, b, c) |
|
|
|
|
|
|
|
// After all this, sending a message c->a should work.
|
|
|
|
p2ptest.RequireSend(ctx, t, c, p2p.Envelope{To: aID, Message: &p2ptest.Message{Value: "bar"}}) |
|
|
|
p2ptest.RequireReceive(t, a, p2p.Envelope{From: cID, Message: &p2ptest.Message{Value: "bar"}}) |
|
|
|
p2ptest.RequireEmpty(t, a, b, c) |
|
|
|
p2ptest.RequireReceive(ctx, t, a, p2p.Envelope{From: cID, Message: &p2ptest.Message{Value: "bar"}}) |
|
|
|
p2ptest.RequireEmpty(ctx, t, a, b, c) |
|
|
|
|
|
|
|
// None of these messages should have made it onto the other channels.
|
|
|
|
for _, other := range otherChannels { |
|
|
|
p2ptest.RequireEmpty(t, other) |
|
|
|
p2ptest.RequireEmpty(ctx, t, other) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -246,17 +244,17 @@ func TestRouter_Channel_Broadcast(t *testing.T) { |
|
|
|
|
|
|
|
// Sending a broadcast from b should work.
|
|
|
|
p2ptest.RequireSend(ctx, t, b, p2p.Envelope{Broadcast: true, Message: &p2ptest.Message{Value: "foo"}}) |
|
|
|
p2ptest.RequireReceive(t, a, p2p.Envelope{From: bID, Message: &p2ptest.Message{Value: "foo"}}) |
|
|
|
p2ptest.RequireReceive(t, c, p2p.Envelope{From: bID, Message: &p2ptest.Message{Value: "foo"}}) |
|
|
|
p2ptest.RequireReceive(t, d, p2p.Envelope{From: bID, Message: &p2ptest.Message{Value: "foo"}}) |
|
|
|
p2ptest.RequireEmpty(t, a, b, c, d) |
|
|
|
p2ptest.RequireReceive(ctx, t, a, p2p.Envelope{From: bID, Message: &p2ptest.Message{Value: "foo"}}) |
|
|
|
p2ptest.RequireReceive(ctx, t, c, p2p.Envelope{From: bID, Message: &p2ptest.Message{Value: "foo"}}) |
|
|
|
p2ptest.RequireReceive(ctx, t, d, p2p.Envelope{From: bID, Message: &p2ptest.Message{Value: "foo"}}) |
|
|
|
p2ptest.RequireEmpty(ctx, t, a, b, c, d) |
|
|
|
|
|
|
|
// Removing one node from the network shouldn't prevent broadcasts from working.
|
|
|
|
network.Remove(ctx, t, dID) |
|
|
|
p2ptest.RequireSend(ctx, t, a, p2p.Envelope{Broadcast: true, Message: &p2ptest.Message{Value: "bar"}}) |
|
|
|
p2ptest.RequireReceive(t, b, p2p.Envelope{From: aID, Message: &p2ptest.Message{Value: "bar"}}) |
|
|
|
p2ptest.RequireReceive(t, c, p2p.Envelope{From: aID, Message: &p2ptest.Message{Value: "bar"}}) |
|
|
|
p2ptest.RequireEmpty(t, a, b, c, d) |
|
|
|
p2ptest.RequireReceive(ctx, t, b, p2p.Envelope{From: aID, Message: &p2ptest.Message{Value: "bar"}}) |
|
|
|
p2ptest.RequireReceive(ctx, t, c, p2p.Envelope{From: aID, Message: &p2ptest.Message{Value: "bar"}}) |
|
|
|
p2ptest.RequireEmpty(ctx, t, a, b, c, d) |
|
|
|
} |
|
|
|
|
|
|
|
func TestRouter_Channel_Wrapper(t *testing.T) { |
|
|
@ -287,11 +285,11 @@ func TestRouter_Channel_Wrapper(t *testing.T) { |
|
|
|
// should automatically wrap and unwrap sent messages -- we prepend the
|
|
|
|
// wrapper actions to the message value to signal this.
|
|
|
|
p2ptest.RequireSend(ctx, t, a, p2p.Envelope{To: bID, Message: &p2ptest.Message{Value: "foo"}}) |
|
|
|
p2ptest.RequireReceive(t, b, p2p.Envelope{From: aID, Message: &p2ptest.Message{Value: "unwrap:wrap:foo"}}) |
|
|
|
p2ptest.RequireReceive(ctx, t, b, p2p.Envelope{From: aID, Message: &p2ptest.Message{Value: "unwrap:wrap:foo"}}) |
|
|
|
|
|
|
|
// If we send a different message that can't be wrapped, it should be dropped.
|
|
|
|
p2ptest.RequireSend(ctx, t, a, p2p.Envelope{To: bID, Message: &gogotypes.BoolValue{Value: true}}) |
|
|
|
p2ptest.RequireEmpty(t, b) |
|
|
|
p2ptest.RequireEmpty(ctx, t, b) |
|
|
|
|
|
|
|
// If we send the wrapper message itself, it should also be passed through
|
|
|
|
// since WrapperMessage supports it, and should only be unwrapped at the receiver.
|
|
|
@ -299,7 +297,7 @@ func TestRouter_Channel_Wrapper(t *testing.T) { |
|
|
|
To: bID, |
|
|
|
Message: &wrapperMessage{Message: p2ptest.Message{Value: "foo"}}, |
|
|
|
}) |
|
|
|
p2ptest.RequireReceive(t, b, p2p.Envelope{ |
|
|
|
p2ptest.RequireReceive(ctx, t, b, p2p.Envelope{ |
|
|
|
From: aID, |
|
|
|
Message: &p2ptest.Message{Value: "unwrap:foo"}, |
|
|
|
}) |
|
|
|