-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtree.rs
139 lines (117 loc) · 4.01 KB
/
tree.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
// Copyright (c) 2022, MaidSafe.
// All rights reserved.
//
// This SAFE Network Software is licensed under the BSD-3-Clause license.
// Please see the LICENSE file for more details.
/// tests for crdt-tree
use crdt_tree::{Clock, OpMove, State};
// Define some "real" types for use in the tests.
type TypeId = u8;
type TypeActor = u8;
type TypeMetaStr<'a> = &'a str;
// helper: generate a new random id
fn new_id() -> TypeId {
rand::random::<TypeId>()
}
// helper: generate a new random actor
fn new_actor() -> TypeActor {
rand::random::<TypeActor>()
}
// Tests case 1 in the paper. Concurrent moves of the same node.
//
// Initial State:
// root
// - A
// - B
// - C
//
// Replica 1 moves A to be a child of B, while concurrently
// replica 2 moves the same node A to be a child of C.
// a child of B. This could potentially result in A being
// duplicated under B and C, or A having 2 parents, B and C.
//
// The only valid result is for one operation
// to succeed and the other to be ignored, but both replica's
// must pick the same success case.
//
// See paper for diagram.
#[test]
fn concurrent_moves_lww() {
let mut r1: State<TypeId, TypeMetaStr, TypeActor> = State::new();
let mut r2: State<TypeId, TypeMetaStr, TypeActor> = State::new();
let (r1_id, r2_id) = (new_actor(), new_actor());
let mut r1t = Clock::<TypeActor>::new(r1_id, None);
let mut r2t = Clock::<TypeActor>::new(r2_id, None);
let (root_id, a_id, b_id, c_id) = (new_id(), new_id(), new_id(), new_id());
// Create ops for initial tree state.
let ops = vec![
OpMove::new(r1t.tick(), 0, "root", root_id),
OpMove::new(r1t.tick(), root_id, "a", a_id),
OpMove::new(r1t.tick(), root_id, "b", b_id),
OpMove::new(r1t.tick(), root_id, "c", c_id),
];
// Apply initial ops to both replicas
for op in ops {
r1.apply_op(op.clone());
r2.apply_op(op);
}
// replica_1 moves /root/a to /root/b
let r1_op = OpMove::new(r1t.tick(), b_id, "a", a_id);
// replica_2 "simultaneously" moves /root/a to /root/c
let r2_op = OpMove::new(r2t.tick(), c_id, "a", a_id);
// apply both ops to r1
r1.apply_op(r1_op.clone());
r1.apply_op(r2_op.clone());
// apply both ops to r2
r2.apply_op(r2_op);
r2.apply_op(r1_op);
assert_eq!(r1, r2);
}
// Tests case 2 in the paper. Moving a node to be a descendant of itself.
//
// Initial State:
// root
// - A
// - C
// - B
//
// Initially, nodes A and B are siblings. Replica 1 moves B
// to be a child of A, while concurrently replica 2 moves A to be
// a child of B. This could potentially result in a cyle, or
// duplication. The only valid result is for one operation
// to succeed and the other to be ignored, but both replica's
// must pick the same success case.
//
// See paper for diagram.
#[test]
fn concurrent_moves_cycle() {
let mut r1: State<TypeId, TypeMetaStr, TypeActor> = State::new();
let mut r2: State<TypeId, TypeMetaStr, TypeActor> = State::new();
let (r1_id, r2_id) = (new_actor(), new_actor());
let mut r1t = Clock::<TypeActor>::new(r1_id, None);
let mut r2t = Clock::<TypeActor>::new(r2_id, None);
let (root_id, a_id, b_id, c_id) = (new_id(), new_id(), new_id(), new_id());
// Create ops for initial tree state.
let ops = vec![
OpMove::new(r1t.tick(), 0, "root", root_id),
OpMove::new(r1t.tick(), root_id, "a", a_id),
OpMove::new(r1t.tick(), root_id, "b", b_id),
OpMove::new(r1t.tick(), a_id, "c", c_id),
];
// Apply initial ops to both replicas
for op in ops {
r1.apply_op(op.clone());
r2.apply_op(op);
}
// replica_1 moves /root/b to /root/a
let r1_op = OpMove::new(r1t.tick(), a_id, "b", b_id);
// replica_2 "simultaneously" moves /root/a to /root/b
let r2_op = OpMove::new(r2t.tick(), b_id, "a", a_id);
// apply both ops to r1
r1.apply_op(r1_op.clone());
r1.apply_op(r2_op.clone());
// apply both ops to r2
r2.apply_op(r2_op);
r2.apply_op(r1_op);
assert_eq!(r1, r2);
}