junk_server.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use crate::Server;
  2. pub const TEST_SERVER: &str = &"test-server";
  3. pub const NON_SERVER: &str = &"non-server";
  4. pub const TEST_CLIENT: &str = &"test-client";
  5. pub const NON_CLIENT: &str = &"non-client";
  6. pub enum JunkRpcs {
  7. Echo,
  8. Aborting,
  9. Woods,
  10. }
  11. impl JunkRpcs {
  12. pub fn name(&self) -> String {
  13. match *self {
  14. Self::Echo => "echo",
  15. Self::Aborting => "aborting",
  16. Self::Woods => "woods",
  17. }
  18. .into()
  19. }
  20. }
  21. pub fn make_test_server() -> Server {
  22. let mut server = Server::make_server(TEST_SERVER);
  23. server
  24. .register_rpc_handler(
  25. JunkRpcs::Echo.name(),
  26. Box::new(move |request| {
  27. let mut reply = bytes::BytesMut::from(request.as_ref());
  28. reply.reverse();
  29. reply.freeze()
  30. }),
  31. )
  32. .expect("Registering the first RPC handler should not fail");
  33. server
  34. .register_rpc_handler(
  35. JunkRpcs::Aborting.name(),
  36. Box::new(move |_| panic!("Aborting rpc...")),
  37. )
  38. .expect("Registering the second RPC handler should not fail");
  39. server
  40. }