1
+ use std:: collections:: VecDeque ;
2
+
1
3
use tarantool:: raft:: inner:: { NodeAction , NodeEvent , NodeInner } ;
2
4
use tarantool:: raft:: net:: ConnectionId ;
3
5
use tarantool:: raft:: rpc;
@@ -6,9 +8,12 @@ pub fn test_bootstrap_solo() {
6
8
let local_addrs = vec ! [ "127.0.0.1:3301" . parse( ) . unwrap( ) ] ;
7
9
let remote_addrs = vec ! [ "127.0.0.1:3302" . parse( ) . unwrap( ) ] ;
8
10
11
+ let mut events = VecDeque :: new ( ) ;
12
+ let mut actions = VecDeque :: new ( ) ;
13
+
9
14
let mut node = NodeInner :: new ( 1 , local_addrs. clone ( ) , vec ! [ remote_addrs. clone( ) ] ) ;
15
+ node. update ( & mut events, & mut actions) ;
10
16
11
- let actions = node. pending_actions ( ) ;
12
17
assert_eq ! ( actions. len( ) , 2 ) ;
13
18
assert ! ( matches!(
14
19
& actions[ 0 ] ,
@@ -21,13 +26,14 @@ pub fn test_bootstrap_solo() {
21
26
nodes: vec![ ( 1 , local_addrs. clone( ) ) ] ,
22
27
}
23
28
) ) ;
29
+ actions. clear ( ) ;
24
30
25
- node . handle_event ( NodeEvent :: Response ( rpc:: BootstrapMsg {
31
+ events . push_back ( NodeEvent :: Response ( rpc:: BootstrapMsg {
26
32
from_id : 2 ,
27
33
nodes : vec ! [ ( 2 , remote_addrs. clone( ) ) ] ,
28
34
} ) ) ;
35
+ node. update ( & mut events, & mut actions) ;
29
36
30
- let actions = node. pending_actions ( ) ;
31
37
assert_eq ! ( actions. len( ) , 2 ) ;
32
38
assert ! ( matches!(
33
39
& actions[ 0 ] ,
@@ -43,13 +49,14 @@ pub fn test_bootstrap_solo() {
43
49
] ,
44
50
}
45
51
) ) ;
52
+ actions. clear ( ) ;
46
53
47
- node . handle_event ( NodeEvent :: Response ( rpc:: BootstrapMsg {
54
+ events . push_back ( NodeEvent :: Response ( rpc:: BootstrapMsg {
48
55
from_id : 2 ,
49
56
nodes : vec ! [ ( 1 , local_addrs. clone( ) ) , ( 2 , remote_addrs. clone( ) ) ] ,
50
57
} ) ) ;
58
+ node. update ( & mut events, & mut actions) ;
51
59
52
- let actions = node. pending_actions ( ) ;
53
60
assert_eq ! ( actions. len( ) , 1 ) ;
54
61
assert ! ( matches!( & actions[ 0 ] , NodeAction :: Completed ) )
55
62
}
@@ -58,44 +65,50 @@ pub fn test_bootstrap_2n() {
58
65
let n1_addrs = vec ! [ "127.0.0.1:3301" . parse( ) . unwrap( ) ] ;
59
66
let n2_addrs = vec ! [ "127.0.0.1:3302" . parse( ) . unwrap( ) ] ;
60
67
68
+ let mut n1_events = VecDeque :: new ( ) ;
69
+ let mut n1_actions = VecDeque :: new ( ) ;
61
70
let mut n1_ctrl = NodeInner :: new ( 1 , n1_addrs. clone ( ) , vec ! [ n2_addrs. clone( ) ] ) ;
71
+
72
+ let mut n2_events = VecDeque :: new ( ) ;
73
+ let mut n2_actions = VecDeque :: new ( ) ;
62
74
let mut n2_ctrl = NodeInner :: new ( 2 , n2_addrs. clone ( ) , vec ! [ n1_addrs. clone( ) ] ) ;
63
75
64
- assert_eq ! ( communicate( & mut n1_ctrl, & mut n2_ctrl) , ( false , false ) ) ;
65
- assert_eq ! ( communicate( & mut n1_ctrl, & mut n2_ctrl) , ( false , false ) ) ;
66
- assert_eq ! ( communicate( & mut n1_ctrl, & mut n2_ctrl) , ( true , true ) ) ;
67
- }
76
+ let mut n1_is_completed = false ;
77
+ let mut n2_is_completed = false ;
68
78
69
- fn communicate ( n1_ctrl : & mut NodeInner , n2_ctrl : & mut NodeInner ) -> ( bool , bool ) {
70
- let n1_actions = n1_ctrl. pending_actions ( ) ;
71
- let n2_actions = n2_ctrl. pending_actions ( ) ;
79
+ for _ in 0 .. 3 {
80
+ n1_ctrl. update ( & mut n1_events , & mut n1_actions ) ;
81
+ n2_ctrl. update ( & mut n2_events , & mut n2_actions ) ;
72
82
73
- let mut n1_is_completed = false ;
74
- for action in n1_actions {
75
- if let NodeAction :: Completed = action {
76
- n1_is_completed = true ;
77
- }
78
- forward_action ( action, n2_ctrl) ;
83
+ n1_is_completed = n1_is_completed || communicate ( & mut n1_actions, & mut n2_events) ;
84
+ n2_is_completed = n2_is_completed || communicate ( & mut n2_actions, & mut n1_events) ;
79
85
}
80
86
81
- let mut n2_is_completed = false ;
82
- for action in n2_actions {
87
+ assert ! ( n1_is_completed) ;
88
+ assert ! ( n2_is_completed) ;
89
+ }
90
+
91
+ fn communicate ( from : & mut VecDeque < NodeAction > , to : & mut VecDeque < NodeEvent > ) -> bool {
92
+ let mut is_completed = false ;
93
+ for action in from. drain ( ..) {
83
94
if let NodeAction :: Completed = action {
84
- n2_is_completed = true ;
95
+ is_completed = true ;
85
96
}
86
- forward_action ( action, n1_ctrl) ;
87
- }
88
97
89
- ( n1_is_completed, n2_is_completed)
98
+ if let Some ( event) = forward_action ( action) {
99
+ to. push_back ( event) ;
100
+ }
101
+ }
102
+ is_completed
90
103
}
91
104
92
- fn forward_action ( action : NodeAction , node_ctrl : & mut NodeInner ) {
105
+ fn forward_action ( action : NodeAction ) -> Option < NodeEvent > {
93
106
match action {
94
- NodeAction :: Request ( _, msg) => node_ctrl . handle_event ( NodeEvent :: Request ( msg) ) ,
107
+ NodeAction :: Request ( _, msg) => Some ( NodeEvent :: Request ( msg) ) ,
95
108
NodeAction :: Response ( resp) => match resp. unwrap ( ) {
96
- rpc:: Response :: Bootstrap ( msg) => node_ctrl . handle_event ( NodeEvent :: Response ( msg) ) ,
97
- _ => { }
109
+ rpc:: Response :: Bootstrap ( msg) => Some ( NodeEvent :: Response ( msg) ) ,
110
+ _ => None ,
98
111
} ,
99
- _ => { }
100
- } ;
112
+ _ => None ,
113
+ }
101
114
}
0 commit comments