crates/edge/proto/tests/session.rs 4.2 KiB 155 lines Raw
1 //! Protocol section 6: the phases, both ends.2 3 use inf3r_proto::{Kind, Phase, Role, Session};4 5 fn caller() -> Session {6     Session::new(Role::Caller)7 }8 9 fn server() -> Session {10     Session::new(Role::Server)11 }12 13 #[test]14 fn the_caller_walks_the_whole_lifecycle() {15     let mut s = caller();16     assert_eq!(s.phase(), Phase::New);17     s.send(Kind::Hello).unwrap();18     assert_eq!(s.phase(), Phase::Hello);19     s.recv(Kind::Welcome).unwrap();20     assert_eq!(s.phase(), Phase::Welcomed);21     s.recv(Kind::Catalogue).unwrap();22     s.send(Kind::Grant).unwrap();23     assert_eq!(s.phase(), Phase::Loading);24     s.recv(Kind::Ready).unwrap();25     assert_eq!(s.phase(), Phase::Ready);26     s.send(Kind::Open).unwrap();27     assert_eq!(s.phase(), Phase::Ready);28     assert_eq!(s.streams(), 1);29 }30 31 #[test]32 fn the_server_walks_the_same_lifecycle_from_the_other_side() {33     let mut s = server();34     s.recv(Kind::Hello).unwrap();35     s.send(Kind::Welcome).unwrap();36     s.send(Kind::Catalogue).unwrap();37     s.recv(Kind::Grant).unwrap();38     s.send(Kind::Ready).unwrap();39     assert_eq!(s.phase(), Phase::Ready);40 }41 42 #[test]43 fn a_server_with_no_dependencies_is_ready_without_a_grant() {44     let mut s = server();45     s.recv(Kind::Hello).unwrap();46     s.send(Kind::Welcome).unwrap();47     s.send(Kind::Catalogue).unwrap();48     s.send(Kind::Ready).unwrap();49     assert_eq!(s.phase(), Phase::Ready);50 }51 52 #[test]53 fn the_caller_accepts_ready_without_sending_a_grant() {54     let mut s = caller();55     s.send(Kind::Hello).unwrap();56     s.recv(Kind::Welcome).unwrap();57     s.recv(Kind::Catalogue).unwrap();58     s.recv(Kind::Ready).unwrap();59     assert_eq!(s.phase(), Phase::Ready);60 }61 62 #[test]63 fn open_before_ready_is_refused() {64     let mut s = caller();65     s.send(Kind::Hello).unwrap();66     let err = s.send(Kind::Open).unwrap_err();67     assert_eq!(err.phase, Phase::Hello);68     assert_eq!(err.kind, Kind::Open);69     assert!(err.sent);70     assert_eq!(s.phase(), Phase::Hello, "a refused frame changes nothing");71 }72 73 #[test]74 fn the_server_never_opens_a_stream() {75     let mut s = server();76     s.recv(Kind::Hello).unwrap();77     s.send(Kind::Welcome).unwrap();78     s.send(Kind::Catalogue).unwrap();79     s.send(Kind::Ready).unwrap();80     assert!(s.send(Kind::Open).is_err());81 }82 83 #[test]84 fn the_first_fatal_wins_and_the_second_is_ignored() {85     let mut s = caller();86     s.send(Kind::Fatal).unwrap();87     assert_eq!(s.phase(), Phase::Closed);88     s.send(Kind::Fatal).unwrap();89     assert_eq!(s.phase(), Phase::Closed);90     s.recv(Kind::Fatal).unwrap();91     assert_eq!(s.phase(), Phase::Closed);92 }93 94 #[test]95 fn bye_drains_the_open_streams_before_the_close() {96     let mut s = caller();97     s.send(Kind::Hello).unwrap();98     s.recv(Kind::Welcome).unwrap();99     s.recv(Kind::Catalogue).unwrap();100     s.recv(Kind::Ready).unwrap();101     s.send(Kind::Open).unwrap();102     s.send(Kind::Open).unwrap();103     s.send(Kind::Bye).unwrap();104     assert_eq!(s.phase(), Phase::Draining);105 106     s.finish_stream();107     assert_eq!(s.phase(), Phase::Draining);108     s.finish_stream();109     assert_eq!(s.phase(), Phase::Closed);110 }111 112 #[test]113 fn a_frame_after_the_close_is_refused() {114     let mut s = server();115     s.recv(Kind::Fatal).unwrap();116     assert!(s.send(Kind::Welcome).is_err());117 }118 119 #[test]120 fn a_failed_open_can_take_back_its_count() {121     let mut s = caller();122     s.send(Kind::Hello).unwrap();123     s.recv(Kind::Welcome).unwrap();124     s.recv(Kind::Catalogue).unwrap();125     s.recv(Kind::Ready).unwrap();126     s.send(Kind::Open).unwrap();127     assert_eq!(s.streams(), 1);128     s.withdraw_stream();129     assert_eq!(s.streams(), 0);130     // The phase is still READY, so the next open is legal.131     s.send(Kind::Open).unwrap();132     assert_eq!(s.streams(), 1);133 }134 135 #[test]136 fn bye_with_no_open_streams_closes_at_once() {137     let mut s = caller();138     s.send(Kind::Hello).unwrap();139     s.recv(Kind::Welcome).unwrap();140     s.recv(Kind::Catalogue).unwrap();141     s.recv(Kind::Ready).unwrap();142     s.send(Kind::Bye).unwrap();143     assert_eq!(s.phase(), Phase::Closed);144 }145 146 #[test]147 fn a_server_bye_with_no_open_streams_closes_at_once() {148     let mut s = server();149     s.recv(Kind::Hello).unwrap();150     s.send(Kind::Welcome).unwrap();151     s.send(Kind::Catalogue).unwrap();152     s.send(Kind::Ready).unwrap();153     s.recv(Kind::Bye).unwrap();154     assert_eq!(s.phase(), Phase::Closed);155 }