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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use interrupt_support::NeverInterrupts;
use log::*;
use serde_derive::*;
use std::cell::{Cell, RefCell};
use std::mem;
use sync15::{telemetry, MemoryCachedState};
use sync15_traits::{
CollectionRequest, IncomingChangeset, OutgoingChangeset, Payload, ServerTimestamp, Store,
StoreSyncAssociation,
};
use sync_guid::Guid;
use crate::auth::TestClient;
use crate::testing::TestGroup;
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
pub struct TestRecord {
pub id: Guid,
pub message: String,
}
pub struct TestStore {
pub name: &'static str,
pub test_records: RefCell<Vec<TestRecord>>,
pub store_sync_assoc: RefCell<StoreSyncAssociation>,
pub was_reset_called: Cell<bool>,
pub global_id: Option<Guid>,
pub coll_id: Option<Guid>,
}
impl Store for TestStore {
fn collection_name(&self) -> std::borrow::Cow<'static, str> {
"addresses".into()
}
fn apply_incoming(
&self,
inbound: Vec<IncomingChangeset>,
_telem: &mut telemetry::Engine,
) -> anyhow::Result<OutgoingChangeset> {
let temp: Vec<TestRecord> = mem::take(&mut *self.test_records.borrow_mut());
let inbound = inbound.into_iter().next().unwrap();
for (payload, _timestamp) in inbound.changes {
let incoming_record: TestRecord = payload.into_record()?;
info!("Got incoming record {:?}", incoming_record);
self.test_records.borrow_mut().push(incoming_record);
}
let mut outgoing = OutgoingChangeset::new(self.collection_name(), inbound.timestamp);
outgoing.changes = temp
.into_iter()
.map(Payload::from_record)
.collect::<Result<Vec<Payload>, serde_json::error::Error>>()?;
Ok(outgoing)
}
fn sync_finished(
&self,
_new_timestamp: ServerTimestamp,
records_synced: Vec<Guid>,
) -> anyhow::Result<()> {
info!("Uploaded records: {:?}", records_synced);
Ok(())
}
fn get_collection_requests(
&self,
_server_timestamp: ServerTimestamp,
) -> anyhow::Result<Vec<CollectionRequest>> {
Ok(vec![CollectionRequest::new(self.collection_name()).full()])
}
fn get_sync_assoc(&self) -> anyhow::Result<StoreSyncAssociation> {
let our_assoc = self.store_sync_assoc.borrow();
println!(
"TEST {}: get_sync_assoc called with {:?}",
self.name, *our_assoc
);
Ok(our_assoc.clone())
}
fn reset(&self, assoc: &StoreSyncAssociation) -> anyhow::Result<()> {
println!("TEST {}: Reset called", self.name);
self.was_reset_called.set(true);
*self.store_sync_assoc.borrow_mut() = assoc.clone();
Ok(())
}
fn wipe(&self) -> anyhow::Result<()> {
Ok(())
}
}
fn sync_first_client(c0: &mut TestClient, store: &dyn Store) {
let (init, key, _device_id) = c0
.data_for_sync()
.expect("Should have data for syncing first client");
let mut persisted_global_state = None;
let mut mem_cached_state = MemoryCachedState::default();
let result = sync15::sync_multiple(
&[store],
&mut persisted_global_state,
&mut mem_cached_state,
&init,
&key,
&NeverInterrupts,
None,
);
println!("Finished syncing first client: {:?}", result);
}
fn sync_second_client(c1: &mut TestClient, store: &dyn Store) {
let (init, key, _device_id) = c1
.data_for_sync()
.expect("Should have data for syncing second client");
let mut persisted_global_state = None;
let mut mem_cached_state = MemoryCachedState::default();
let result = sync15::sync_multiple(
&[store],
&mut persisted_global_state,
&mut mem_cached_state,
&init,
&key,
&NeverInterrupts,
None,
);
println!("Finished syncing second client: {:?}", result);
}
fn test_sync_multiple(c0: &mut TestClient, c1: &mut TestClient) {
let test_vec = vec![TestRecord {
id: Guid::random(),
message: "<3".to_string(),
}];
let first_client_store = TestStore {
name: "c0",
test_records: RefCell::new(test_vec.clone()),
store_sync_assoc: RefCell::new(StoreSyncAssociation::Disconnected),
was_reset_called: Cell::new(false),
global_id: Option::from(Guid::random()),
coll_id: Option::from(Guid::random()),
};
sync_first_client(c0, &first_client_store);
assert_eq!(
first_client_store.was_reset_called.get(),
true,
"Should have called first reset."
);
let second_client_store = TestStore {
name: "c1",
test_records: RefCell::default(),
store_sync_assoc: first_client_store.store_sync_assoc,
was_reset_called: Cell::new(false),
global_id: Option::from(Guid::random()),
coll_id: Option::from(Guid::random()),
};
sync_second_client(c1, &second_client_store);
assert_eq!(
second_client_store.was_reset_called.get(),
false,
"Second client shouldn't have called reset."
);
let vector1 = first_client_store.test_records.into_inner();
let vector2 = second_client_store.test_records.into_inner();
assert!(vector1.is_empty(), "The vector should be empty.");
assert_eq!(
test_vec, vector2,
"Both clients' messages should match after the two calls to sync_multiple()."
);
info!(
"Client {:?}'s test_records: {:?}",
first_client_store.name, vector1
);
info!(
"Client {:?}'s test_records: {:?}",
second_client_store.name, vector2
);
}
pub fn get_test_group() -> TestGroup {
TestGroup::new("sync15", vec![("test_sync_multiple", test_sync_multiple)])
}