Skip to main content

Crate lacodda_tear

Crate lacodda_tear 

Source
Expand description

A TCP proxy for tests that tears connections where you say.

Code that talks over a network has to survive the network breaking: a request torn halfway up, an answer lost on its way back, a connection that goes silent, a phone that drops off the network and comes back. A mock of the other side cannot show any of that - it never breaks the way a socket does. Tear stands between the code under test and the real server, on a real socket, passes every byte through, and breaks the connection on cue:

  • Cut::Up - after so many more bytes towards the server: a request torn partway;
  • Cut::Down - after so many more bytes back from it: an answer torn partway, or - with zero - lost before its first byte, after the server has done the work;
  • Cut::After - whatever is open once a while has passed;
  • Cut::Now - whatever is open now;
  • Tear::offline and Tear::online - the network gone: open connections torn, new ones reset at once, until it is back;
  • Tear::stall and Tear::flow - bytes held, the connection open and silent, as on a network that drops packets without saying so.

Byte counts run across every connection from the moment a cut is armed, not per connection: an HTTP client that reuses a pooled connection for the next request is cut just the same. A tear is a reset (RST), the way a dropped mobile connection looks to both ends - not a polite end of the stream.

The proxy knows nothing of what it carries: HTTP, a database’s protocol, anything over TCP.

use lacodda_tear::{Cut, Tear};

let tear = Tear::start(server).await?;
// Point the client at `tear.addr()` instead of the server.

// The next answer is lost: the server does the work, the client hears
// nothing back.
tear.cut(Cut::Down(0));
// ... make the request, see it fail, make it again ...
assert_eq!(tear.torn(), 1);

Structs§

Tear
A proxy on a port of its own, in front of one server.

Enums§

Cut
Where a connection breaks.
Direction
Which way bytes go.