File size: 4,007 Bytes
f0f4f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use libp2p_core::transport::PortUse;
use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_core::{Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::handler::ConnectionEvent;
use libp2p_swarm::{
    ConnectionDenied, ConnectionHandler, ConnectionHandlerEvent, ConnectionId, FromSwarm,
    NetworkBehaviour, SubstreamProtocol, Swarm, SwarmEvent, THandler, THandlerInEvent,
    THandlerOutEvent, ToSwarm,
};
use libp2p_swarm_test::SwarmExt;
use std::task::{Context, Poll};
use void::Void;

#[async_std::test]
async fn sends_remaining_events_to_behaviour_on_connection_close() {
    let mut swarm1 = Swarm::new_ephemeral(|_| Behaviour::new(3));
    let mut swarm2 = Swarm::new_ephemeral(|_| Behaviour::new(3));

    swarm2.listen().with_memory_addr_external().await;
    swarm1.connect(&mut swarm2).await;

    swarm1.disconnect_peer_id(*swarm2.local_peer_id()).unwrap();

    match libp2p_swarm_test::drive(&mut swarm1, &mut swarm2).await {
        ([SwarmEvent::ConnectionClosed { .. }], [SwarmEvent::ConnectionClosed { .. }]) => {
            assert_eq!(swarm1.behaviour().state, 0);
            assert_eq!(swarm2.behaviour().state, 0);
        }
        (e1, e2) => panic!("Unexpected events: {:?} {:?}", e1, e2),
    }
}

struct HandlerWithState {
    precious_state: u64,
}

struct Behaviour {
    state: u64,
}

impl Behaviour {
    fn new(state: u64) -> Self {
        Behaviour { state }
    }
}

impl NetworkBehaviour for Behaviour {
    type ConnectionHandler = HandlerWithState;
    type ToSwarm = ();

    fn handle_established_inbound_connection(
        &mut self,
        _: ConnectionId,
        _: PeerId,
        _: &Multiaddr,
        _: &Multiaddr,
    ) -> Result<THandler<Self>, ConnectionDenied> {
        Ok(HandlerWithState {
            precious_state: self.state,
        })
    }

    fn handle_established_outbound_connection(
        &mut self,
        _: ConnectionId,
        _: PeerId,
        _: &Multiaddr,
        _: Endpoint,
        _: PortUse,
    ) -> Result<THandler<Self>, ConnectionDenied> {
        Ok(HandlerWithState {
            precious_state: self.state,
        })
    }

    fn on_swarm_event(&mut self, event: FromSwarm) {
        if let FromSwarm::ConnectionClosed(_) = event {
            assert_eq!(self.state, 0);
        }
    }

    fn on_connection_handler_event(
        &mut self,
        _peer_id: PeerId,
        _connection_id: ConnectionId,
        event: THandlerOutEvent<Self>,
    ) {
        assert_eq!(self.state, event);
        self.state -= 1;
    }

    fn poll(&mut self, _: &mut Context<'_>) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
        Poll::Pending
    }
}

impl ConnectionHandler for HandlerWithState {
    type FromBehaviour = Void;
    type ToBehaviour = u64;
    type InboundProtocol = DeniedUpgrade;
    type OutboundProtocol = DeniedUpgrade;
    type InboundOpenInfo = ();
    type OutboundOpenInfo = ();

    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
        SubstreamProtocol::new(DeniedUpgrade, ())
    }

    fn connection_keep_alive(&self) -> bool {
        true
    }

    fn poll(
        &mut self,
        _: &mut Context<'_>,
    ) -> Poll<
        ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>,
    > {
        Poll::Pending
    }

    fn poll_close(&mut self, _: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
        if self.precious_state > 0 {
            let state = self.precious_state;
            self.precious_state -= 1;

            return Poll::Ready(Some(state));
        }

        Poll::Ready(None)
    }

    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
        void::unreachable(event)
    }

    fn on_connection_event(
        &mut self,
        _: ConnectionEvent<
            Self::InboundProtocol,
            Self::OutboundProtocol,
            Self::InboundOpenInfo,
            Self::OutboundOpenInfo,
        >,
    ) {
    }
}