faker-rs icon indicating copy to clipboard operation
faker-rs copied to clipboard

Feature request: guaranteed unique values over the whole test run?

Open carols10cents opened this issue 8 years ago • 3 comments

Hi, I'm not sure if this is easily possible in Rust, or if the performance would be horrible, but the Ruby version of faker has an option to request a unique value.

This would be great to have in Rust, since the tests run in multiple threads by default-- it's possible that two tests that run at the same time using the same data could cause intermittent failures.

carols10cents avatar Dec 29 '16 19:12 carols10cents

@carols10cents just looking at this crate again after all this time... What do you mean exactly by "unique".

If all you need is a thread local unique value (which is what it sounds like), then we could add a Uuid type. It's not exactly "fake" data, but it would be very useful in this context, so maybe it makes sense to add it. Hopefully by now you've solved your problem with uuid, which would be how I imagine this working.

If you need multiple threads to agree to a unique value, this becomes more difficult, and I'd argue not worth adding to this crate. Which itself is evidence that even the UUID shouldn't be in here, since why have an ID that works in one situation, but not the other?

So, I'm tempted to close this issue as wont-fix, unless anyone has a comment that changes my mind.

nixpulvis avatar Mar 19 '20 20:03 nixpulvis

@nixpulvis I mean multiple threads agreeing to a unique value. The use case I have is in crates.io tests: crate names must be unique, all the tests use the same database, so currently we're manually assigning crate names in each test but it would be better if we didn't have to do that.

In other words in case my explanation above wasn't clear:

  • There is one test database that all the tests are inserting into from the various test threads
  • The database has a constraint that crates.name must be unique
  • Each test that inserts a crate record must have a unique crate name; currently we hardcode literal crate names usually based on what the test is doing, but contributors have to know each test needs unique crate names and can't, for example, copy-paste an existing test to create a new test

carols10cents avatar Mar 20 '20 12:03 carols10cents

@carols10cents I'm sorry if I'm still missing something, but can you explain why a UUID would not satisfy your issue?

If it's a matter of formatting, then perhaps we could have an atomically incremented index into a list of formatted names? A more involved solution could be to generated a random formatted name, and then atomically insert iff unique, otherwise retry. I believe this is how services like Heroku generate names for their applications/DBs.

nixpulvis avatar Mar 20 '20 14:03 nixpulvis