| ... | ... | @@ -1045,3 +1045,101 @@ fn another_version_is_named_before_any_key() { |
| 1045 | 1045 | // The older end heard this one's opening, and with it its version. |
| 1046 | 1046 | assert!(!older.join().unwrap().is_empty()); |
| 1047 | 1047 | } |
| 1048 | |
| 1049 | /// A TCP proxy to `upstream` whose connections so far fall silent, as a network left behind |
| 1050 | /// does, when the flag it returns is raised; later ones pass. |
| 1051 | fn blackhole(upstream: SocketAddr) -> (String, Arc<AtomicU64>) { |
| 1052 | let listener = TcpListener::bind("127.0.0.1:0").unwrap(); |
| 1053 | let url = format!("ws://{}", listener.local_addr().unwrap()); |
| 1054 | let era = Arc::new(AtomicU64::new(0)); |
| 1055 | let current = Arc::clone(&era); |
| 1056 | thread::spawn(move || { |
| 1057 | for client in listener.incoming() { |
| 1058 | let (Ok(client), Ok(server)) = (client, TcpStream::connect(upstream)) else { |
| 1059 | return; |
| 1060 | }; |
| 1061 | let born = current.load(Ordering::Acquire); |
| 1062 | for (from, mut to) in [ |
| 1063 | (client.try_clone().unwrap(), server.try_clone().unwrap()), |
| 1064 | (server, client), |
| 1065 | ] { |
| 1066 | let current = Arc::clone(&current); |
| 1067 | thread::spawn(move || { |
| 1068 | let mut buffer = [0; 16 << 10]; |
| 1069 | while let Ok(read @ 1..) = from.read(&mut buffer) { |
| 1070 | if current.load(Ordering::Acquire) == born |
| 1071 | && to.write_all(&buffer[..read]).is_err() |
| 1072 | { |
| 1073 | return; |
| 1074 | } |
| 1075 | } |
| 1076 | }); |
| 1077 | } |
| 1078 | } |
| 1079 | }); |
| 1080 | (url, era) |
| 1081 | } |
| 1082 | |
| 1083 | /// A guest whose network went silent comes back through the relay in a new slot while the |
| 1084 | /// host still holds its old stream, which the relay has not yet seen go; the host takes the |
| 1085 | /// new stream. |
| 1086 | #[test] |
| 1087 | fn a_guest_back_through_the_relay_replaces_its_silent_stream() { |
| 1088 | let (url, upstream) = relay(Default::default()); |
| 1089 | let (proxied, era) = blackhole(upstream); |
| 1090 | let room = Room::Notebook([76; 16]); |
| 1091 | let mut host = hello("Host"); |
| 1092 | host.serves = Some([77; 16]); |
| 1093 | let (met, host_met) = mpsc::channel(); |
| 1094 | let (frame, frames) = mpsc::channel(); |
| 1095 | let host = Live::start(host, &room, None, Some(&url), move |event| match event { |
| 1096 | Event::Met(_, line) => { |
| 1097 | let _ = met.send(line.clone()); |
| 1098 | } |
| 1099 | Event::Frame { kind, body, .. } => { |
| 1100 | let _ = frame.send((kind, body.to_vec())); |
| 1101 | } |
| 1102 | _ => {} |
| 1103 | }) |
| 1104 | .unwrap(); |
| 1105 | let (met, guest_met) = mpsc::channel(); |
| 1106 | let (frame, guest_frames) = mpsc::channel(); |
| 1107 | let guest = Live::start( |
| 1108 | hello("Guest"), |
| 1109 | &room, |
| 1110 | None, |
| 1111 | Some(&proxied), |
| 1112 | move |event| match event { |
| 1113 | Event::Met(_, line) => { |
| 1114 | let _ = met.send(line.clone()); |
| 1115 | } |
| 1116 | Event::Frame { kind, .. } => { |
| 1117 | let _ = frame.send(kind); |
| 1118 | } |
| 1119 | _ => {} |
| 1120 | }, |
| 1121 | ) |
| 1122 | .unwrap(); |
| 1123 | let line = guest_met.recv_timeout(Duration::from_secs(10)).unwrap(); |
| 1124 | let host_line = host_met.recv_timeout(Duration::from_secs(10)).unwrap(); |
| 1125 | // The guest notices the silence first, as one whose network changed under it does. |
| 1126 | let state = guest.shared.state.lock().unwrap(); |
| 1127 | let link = state.peers.values().next().unwrap(); |
| 1128 | link.pipe.set_read_timeout(Duration::from_secs(1)).unwrap(); |
| 1129 | drop(state); |
| 1130 | host_line.send(kind::EDITS, &0_u64).unwrap(); |
| 1131 | assert_eq!( |
| 1132 | guest_frames.recv_timeout(Duration::from_secs(10)).unwrap(), |
| 1133 | kind::EDITS |
| 1134 | ); |
| 1135 | era.fetch_add(1, Ordering::AcqRel); |
| 1136 | line.send(kind::EDITS, &1_u64).unwrap(); |
| 1137 | let line = guest_met.recv_timeout(Duration::from_secs(15)).unwrap(); |
| 1138 | line.send(kind::EDITS, &2_u64).unwrap(); |
| 1139 | let (kind, body) = frames.recv_timeout(Duration::from_secs(10)).unwrap(); |
| 1140 | assert_eq!( |
| 1141 | (kind, minicbor::decode::<u64>(&body).unwrap()), |
| 1142 | (kind::EDITS, 2) |
| 1143 | ); |
| 1144 | assert_eq!(host.peers().len(), 1); |
| 1145 | } |