forked from rs-ipfs/rust-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub.rs
196 lines (166 loc) · 5.49 KB
/
pubsub.rs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use futures::future::pending;
use futures::stream::StreamExt;
use ipfs::Node;
use std::time::Duration;
use tokio::time::timeout;
mod common;
use common::{spawn_nodes, Topology};
#[tokio::test]
async fn subscribe_only_once() {
let a = Node::new("test_node").await;
let _stream = a.pubsub_subscribe("some_topic".into()).await.unwrap();
a.pubsub_subscribe("some_topic".into()).await.unwrap_err();
}
#[tokio::test]
async fn resubscribe_after_unsubscribe() {
let a = Node::new("test_node").await;
let mut stream = a.pubsub_subscribe("topic".into()).await.unwrap();
a.pubsub_unsubscribe("topic").await.unwrap();
// sender has been dropped
assert_eq!(stream.next().await, None);
drop(a.pubsub_subscribe("topic".into()).await.unwrap());
}
#[tokio::test]
async fn unsubscribe_via_drop() {
let a = Node::new("test_node").await;
let msgs = a.pubsub_subscribe("topic".into()).await.unwrap();
assert_eq!(a.pubsub_subscribed().await.unwrap(), &["topic"]);
drop(msgs);
let empty: &[&str] = &[];
assert_eq!(a.pubsub_subscribed().await.unwrap(), empty);
}
#[tokio::test]
async fn can_publish_without_subscribing() {
let a = Node::new("test_node").await;
a.pubsub_publish("topic".into(), b"foobar".to_vec())
.await
.unwrap()
}
#[tokio::test]
#[allow(clippy::mutable_key_type)] // clippy doesn't like Vec inside HashSet
async fn publish_between_two_nodes() {
use futures::stream::StreamExt;
use std::collections::HashSet;
let nodes = spawn_nodes(2, Topology::Line).await;
let topic = "shared".to_owned();
let mut a_msgs = nodes[0].pubsub_subscribe(topic.clone()).await.unwrap();
let mut b_msgs = nodes[1].pubsub_subscribe(topic.clone()).await.unwrap();
// need to wait to see both sides so that the messages will get through
let mut appeared = false;
for _ in 0..100usize {
if nodes[0]
.pubsub_peers(Some(topic.clone()))
.await
.unwrap()
.contains(&nodes[1].id)
&& nodes[1]
.pubsub_peers(Some(topic.clone()))
.await
.unwrap()
.contains(&nodes[0].id)
{
appeared = true;
break;
}
timeout(Duration::from_millis(100), pending::<()>())
.await
.unwrap_err();
}
assert!(
appeared,
"timed out before both nodes appeared as pubsub peers"
);
nodes[0]
.pubsub_publish(topic.clone(), b"foobar".to_vec())
.await
.unwrap();
nodes[1]
.pubsub_publish(topic.clone(), b"barfoo".to_vec())
.await
.unwrap();
// the order is not defined, but both should see the other's message and the message they sent
let expected = [
(&[topic.clone()], &nodes[0].id, b"foobar"),
(&[topic.clone()], &nodes[1].id, b"barfoo"),
]
.iter()
.cloned()
.map(|(topics, id, data)| (topics.to_vec(), *id, data.to_vec()))
.collect::<HashSet<_>>();
for st in &mut [b_msgs.by_ref(), a_msgs.by_ref()] {
let actual = timeout(
Duration::from_secs(2),
st.take(2)
// Arc::try_unwrap will fail sometimes here as the sender side in src/p2p/pubsub.rs:305
// can still be looping
.map(|msg| (*msg).clone())
.map(|msg| (msg.topics, msg.source, msg.data))
.collect::<HashSet<_>>(),
)
.await
.unwrap();
assert_eq!(expected, actual);
}
drop(b_msgs);
let mut disappeared = false;
for _ in 0..100usize {
if !nodes[0]
.pubsub_peers(Some(topic.clone()))
.await
.unwrap()
.contains(&nodes[1].id)
{
disappeared = true;
break;
}
timeout(Duration::from_millis(100), pending::<()>())
.await
.unwrap_err();
}
assert!(disappeared, "timed out before a saw b's unsubscription");
}
#[cfg(any(feature = "test_go_interop", feature = "test_js_interop"))]
#[tokio::test]
#[ignore = "doesn't work yet"]
async fn pubsub_interop() {
use common::interop::{api_call, ForeignNode};
use futures::{future, pin_mut};
let rust_node = Node::new("rusty_boi").await;
let foreign_node = ForeignNode::new();
let foreign_api_port = foreign_node.api_port;
rust_node
.connect(foreign_node.addrs[0].clone())
.await
.unwrap();
const TOPIC: &str = "shared";
let _rust_sub_stream = rust_node.pubsub_subscribe(TOPIC.to_string()).await.unwrap();
let foreign_sub_answer = future::maybe_done(api_call(
foreign_api_port,
format!("pubsub/sub?arg={}", TOPIC),
));
pin_mut!(foreign_sub_answer);
assert_eq!(foreign_sub_answer.as_mut().output_mut(), None);
// need to wait to see both sides so that the messages will get through
let mut appeared = false;
for _ in 0..100usize {
if rust_node
.pubsub_peers(Some(TOPIC.to_string()))
.await
.unwrap()
.contains(&foreign_node.id)
&& api_call(foreign_api_port, &format!("pubsub/peers?arg={}", TOPIC))
.await
.contains(&rust_node.id.to_string())
{
appeared = true;
break;
}
timeout(Duration::from_millis(200), pending::<()>())
.await
.unwrap_err();
}
assert!(
appeared,
"timed out before both nodes appeared as pubsub peers"
);
}