@@ -7,6 +7,7 @@ use tokio::time::sleep;
7
7
use crate :: metrics:: Wait ;
8
8
use crate :: metrics:: WaitError ;
9
9
use crate :: raft:: MembershipConfig ;
10
+ use crate :: LogId ;
10
11
use crate :: RaftMetrics ;
11
12
use crate :: State ;
12
13
@@ -99,6 +100,48 @@ async fn test_wait() -> anyhow::Result<()> {
99
100
assert_eq ! ( Some ( hashset![ 1 , 2 ] ) , got. membership_config. members_after_consensus) ;
100
101
}
101
102
103
+ tracing:: info!( "--- wait for snapshot, Ok" ) ;
104
+ {
105
+ let ( init, w, tx) = init_wait_test ( ) ;
106
+
107
+ let h = tokio:: spawn ( async move {
108
+ sleep ( Duration :: from_millis ( 10 ) ) . await ;
109
+ let mut update = init. clone ( ) ;
110
+ update. snapshot = LogId { term : 1 , index : 2 } ;
111
+ let rst = tx. send ( update) ;
112
+ assert ! ( rst. is_ok( ) ) ;
113
+ } ) ;
114
+ let got = w. snapshot ( LogId { term : 1 , index : 2 } , "snapshot" ) . await ?;
115
+ h. await ?;
116
+
117
+ assert_eq ! ( LogId { term: 1 , index: 2 } , got. snapshot) ;
118
+ }
119
+
120
+ tracing:: info!( "--- wait for snapshot, only index matches" ) ;
121
+ {
122
+ let ( init, w, tx) = init_wait_test ( ) ;
123
+
124
+ let h = tokio:: spawn ( async move {
125
+ sleep ( Duration :: from_millis ( 10 ) ) . await ;
126
+ let mut update = init. clone ( ) ;
127
+ update. snapshot = LogId { term : 3 , index : 2 } ;
128
+ let rst = tx. send ( update) ;
129
+ assert ! ( rst. is_ok( ) ) ;
130
+ // delay otherwise the channel will be closed thus the error is shutdown.
131
+ sleep ( Duration :: from_millis ( 200 ) ) . await ;
132
+ } ) ;
133
+ let got = w. snapshot ( LogId { term : 1 , index : 2 } , "snapshot" ) . await ;
134
+ h. await ?;
135
+ match got. unwrap_err ( ) {
136
+ WaitError :: Timeout ( t, _) => {
137
+ assert_eq ! ( Duration :: from_millis( 100 ) , t) ;
138
+ }
139
+ _ => {
140
+ panic ! ( "expect WaitError::Timeout" ) ;
141
+ }
142
+ }
143
+ }
144
+
102
145
{
103
146
// timeout
104
147
let ( _init, w, _tx) = init_wait_test ( ) ;
@@ -136,6 +179,7 @@ fn init_wait_test() -> (RaftMetrics, Wait, watch::Sender<RaftMetrics>) {
136
179
members : Default :: default ( ) ,
137
180
members_after_consensus : None ,
138
181
} ,
182
+ snapshot : LogId { term : 0 , index : 0 } ,
139
183
leader_metrics : None ,
140
184
} ;
141
185
let ( tx, rx) = watch:: channel ( init. clone ( ) ) ;
0 commit comments