ttl_cache icon indicating copy to clipboard operation
ttl_cache copied to clipboard

TtlCache and testing

Open vi opened this issue 6 years ago • 3 comments

As far as I undestand, TtlCache use std::time::{Duration, Instant} without any indirection layer.

This makes it trickier to [unit-]test the code without waiting.

Shall there be some additional testing version of TtlCache that has overridable timer?

vi avatar Jan 30 '20 18:01 vi

Great point. I'll look into doing this. Do you have any thoughts on what you would like the general API to look like?

stusmall avatar Feb 06 '20 02:02 stusmall

I was thinking about this. I think I might implement this as a generic type parameter. By default it will be an implementation that directly calls Duration/Instant but a user may optionally implement a struct that can mock it out.

Before I make my own trait, do you know of any good crates that define the abstraction? I'd rather just pull in something other people are already using.

stusmall avatar Feb 08 '20 15:02 stusmall

I usually find that fake_clock is a nice solution if you can apply it to your use-case.

This blog post is a good example of it.

lerouxrgd avatar Apr 27 '21 15:04 lerouxrgd