Skip to content

Commit 242dcf5

Browse files
committed
Add test for replication methods
1 parent eaf2dc7 commit 242dcf5

File tree

1 file changed

+48
-56
lines changed

1 file changed

+48
-56
lines changed

src/replication/shared_core.rs

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,14 @@ impl CoreMethods for SharedCore {
125125
mod tests {
126126

127127
use super::*;
128-
use crate::replication::events::{Get, Have};
128+
129+
use crate::core::tests::{create_hypercore_with_data, create_hypercore_with_data_and_key_pair};
129130
#[async_std::test]
130131
async fn shared_core_methods() -> Result<(), CoreMethodsError> {
131132
let core = crate::core::tests::create_hypercore_with_data(0).await?;
132133
let core = SharedCore::from(core);
133134

135+
// check CoreInfo
134136
let info = core.info().await;
135137
assert_eq!(
136138
info,
@@ -146,6 +148,7 @@ mod tests {
146148
// key_pair is random, nothing to test here
147149
let _kp = core.key_pair().await;
148150

151+
// check CoreMethods
149152
assert_eq!(core.has(0).await, false);
150153
assert_eq!(core.get(0).await?, None);
151154
let res = core.append(b"foo").await?;
@@ -172,61 +175,50 @@ mod tests {
172175
}
173176

174177
#[async_std::test]
175-
async fn test_events() -> Result<(), CoreMethodsError> {
176-
let mut core = crate::core::tests::create_hypercore_with_data(0).await?;
177-
178-
// Check that appending data emits a DataUpgrade and Have event
179-
180-
let mut rx = core.event_subscribe();
181-
let handle = async_std::task::spawn(async move {
182-
let mut out = vec![];
183-
loop {
184-
if out.len() == 2 {
185-
return (out, rx);
186-
}
187-
if let Ok(evt) = rx.recv().await {
188-
out.push(evt);
189-
}
190-
}
191-
});
192-
core.append(b"foo").await?;
193-
let (res, mut rx) = handle.await;
194-
assert!(matches!(res[0], Event::DataUpgrade(_)));
195-
assert!(matches!(
196-
res[1],
197-
Event::Have(Have {
198-
start: 0,
199-
length: 1,
200-
drop: false
201-
})
202-
));
203-
// no messages in queue
204-
assert!(rx.is_empty());
205-
206-
// Check that Hypercore::get for missing data emits a Get event
207-
208-
let handle = async_std::task::spawn(async move {
209-
let mut out = vec![];
210-
loop {
211-
if out.len() == 1 {
212-
return (out, rx);
213-
}
214-
if let Ok(evt) = rx.recv().await {
215-
out.push(evt);
216-
}
217-
}
218-
});
219-
assert_eq!(core.get(1).await?, None);
220-
let (res, rx) = handle.await;
221-
assert!(matches!(
222-
res[0],
223-
Event::Get(Get {
224-
index: 1,
225-
get_result: _
226-
})
227-
));
228-
// no messages in queue
229-
assert!(rx.is_empty());
178+
async fn shared_core_replication_methods() -> Result<(), ReplicationMethodsError> {
179+
let main = create_hypercore_with_data(10).await?;
180+
let clone = create_hypercore_with_data_and_key_pair(
181+
0,
182+
PartialKeypair {
183+
public: main.key_pair.public,
184+
secret: None,
185+
},
186+
)
187+
.await?;
188+
189+
let main = SharedCore::from(main);
190+
let clone = SharedCore::from(clone);
191+
192+
let index = 6;
193+
let nodes = clone.missing_nodes(index).await?;
194+
let proof = main
195+
.create_proof(
196+
None,
197+
Some(RequestBlock { index, nodes }),
198+
None,
199+
Some(RequestUpgrade {
200+
start: 0,
201+
length: 10,
202+
}),
203+
)
204+
.await?
205+
.unwrap();
206+
assert!(clone.verify_and_apply_proof(&proof).await?);
207+
let main_info = main.info().await;
208+
let clone_info = clone.info().await;
209+
assert_eq!(main_info.byte_length, clone_info.byte_length);
210+
assert_eq!(main_info.length, clone_info.length);
211+
assert!(main.get(6).await?.is_some());
212+
assert!(clone.get(6).await?.is_none());
213+
214+
// Fetch data for index 6 and verify it is found
215+
let index = 6;
216+
let nodes = clone.missing_nodes(index).await?;
217+
let proof = main
218+
.create_proof(Some(RequestBlock { index, nodes }), None, None, None)
219+
.await?
220+
.unwrap();
221+
assert!(clone.verify_and_apply_proof(&proof).await?);
230222
Ok(())
231223
}
232224
}

0 commit comments

Comments
 (0)